What is the output of list1[1:2:4]?
Click to see answer
[2]
Click to see question
What is the output of list1[1:2:4]?
[2]
What does list1[1:8:2] return?
[2, 4, 6, 8]
What is iteration in programming?
Iteration is the repeated execution of a set of statements using loops as long as the condition is true.
How can users define custom exceptions in Python?
Users can define custom exceptions by creating a new class that is derived from the Exception class.
What is printed when iterating over the values of the dictionary 'college'?
Values are: block1, block2, block3
What is the purpose of the 'pass' statement in Python?
'pass' is a null statement used as a placeholder for functionality to be added later; it is not ignored by the interpreter.
What is the output of the dictionary dict1 = {'brand':'mrcet', 'model':'college', 'year':2004}?
The output is {'brand': 'mrcet', 'model': 'college', 'year': 2004}.
What symbol is used for single-line comments in Python?
Can you change the type of a variable after initialization in Python?
Yes, you can change the type of a variable after initialization, for example, x = 5 (int) and then x = 'kve' (str).
What is the syntax structure of if-elif-else in Python?
If : Body of if statements elif : Body of elif statements else: Body of else statements
Which of the following is not a keyword?
a) Eval
What does Python allow regarding multiple assignments?
Python allows you to assign a single value to several variables simultaneously, such as a = b = c = 1.
What is the output of e = a + (b * c) / d when a=20, b=10, c=15, d=5?
50.0
How is a string defined in Python?
A string is a sequence of characters enclosed within quotes, and every string object is of the type 'str'.
How does Python output variables?
Python uses the print statement to output variables, and variables do not need to be declared with any particular type.
What will be the output if the input number is 2 in the if-else example?
The output will be 'a is smaller than the input given'.
What is the result of a tuple comprehension?
It produces a generator object, not a tuple.
What is the purpose of the elif statement in Python?
The elif statement allows checking multiple expressions for TRUE and executing a block of code as soon as one condition is true.
What does the expression str * 2 do?
It prints the string two times.
What is the output of the following code: i=1 while i<=6: print('KV School') i=i+1?
KV School KV School KV School KV School KV School KV School
What is a set comprehension in Python?
A set comprehension is a concise way to create a set using a single line of code, similar to list comprehensions, and is enclosed within curly brackets.
What are the two basic types of statements in Python?
The assignment statement and the print statement.
How can you assign multiple objects to multiple variables in Python?
You can assign multiple objects to multiple variables using syntax like a, b, c = 1, 2, 'kvsch'.
How does Python handle comments in the code?
Comments are discarded by the Python interpreter and can be single-line (starting with #) or multi-line (using triple quotes).
How do you find the number of items in a tuple?
You use the len() function.
How do you print the first character of a string in Python?
You can print the first character using str[0].
What does the syntax [Start: stop: steps] represent in string slicing?
It represents the starting index, stopping index, and step size for slicing a string.
What will the expression str[2:5] return if str = 'Hello World!'?
It will return the characters starting from the 3rd to the 5th character, which is 'llo'.
How do you create a list of 2-tuples like (number, square)?
You can use a list comprehension, for example: [(x, x**2) for x in range(6)].
How do you concatenate a string with another string in Python?
You can concatenate using the + operator, e.g., str + 'TEST'.
What is the output of print(str[2:]) if str = 'Hello World!'?
It prints the string starting from the 3rd character, which is 'llo World!'.
What is the syntax for a while loop in Python?
while(expression): Statement(s)
What is the syntax for a nested for loop?
The syntax is: for val in sequence1: for val in sequence2: statements.
What is the output of the set comprehension a = {x for x in 'abracadabra' if x not in 'abc'}?
The output is {'r', 'd'}.
How do you import a module in Python?
You can import a module using the syntax: import .
What is the purpose of the input() function in Python?
It receives input from the user through the keyboard.
What is the output of the following code: a = 3; if a > 2: print(a, 'is greater'); print('done')?
3 is greater done
Is Python an open source programming language?
Yes, Python is an open source programming language that can be freely downloaded and installed.
What does a ZeroDivisionError indicate in Python?
A ZeroDivisionError indicates that the second argument used in a division or modulo operation was zero.
What does an OverflowError signify in Python?
An OverflowError signifies that an arithmetic operation has exceeded the limits of the current Python runtime, typically due to excessively large float values.
When is an ImportError raised in Python?
An ImportError is raised when trying to import a module that does not exist, often due to a typo in the module name.
What is the syntax of an if-else statement?
if test expression: Body of if statements else: Body of else
What feature allows users to interact directly with Python while writing programs?
Python is an interactive programming language, allowing users to interact with the interpreter directly.
What is the output of x[::-2] for the string 'computer'?
'rtpo'
What does the letter 'd' do in a sequence?
It generates numbers up to a specified value.
How do you iterate over a string in Python?
You can iterate over a string using a for loop, like this: for name in college: print(name).
What will be the output if the inputs are 5, 2, and 9 in the if-elif-else example?
a is greater
What is the output of the code 'list1=[] for x in range(10): list1.append(x**2)'?
The output is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].
What is the purpose of the index in string operations?
The index indicates which character in the sequence we want to select.
What happens when you execute a, b, c = 5, 10, 20 in Python?
This creates three variables named a, b, c and initializes them with values 5, 10, and 20 respectively.
What is the default value of 'start' in string slicing?
The default value of 'start' is 0.
What is the output of len((1,2,3,4,5,6,2,10,2,11,12,2))?
The output is 12.
What is the result of the expression 3 + 4 * 2?
The result is 11 because multiplication is evaluated before addition.
Can you iterate over a generator object multiple times?
No, you can only iterate over it once.
What is the output of the following code? A = 0 while A<10: print(A, ' , ') A = A+1 print('\n', A)
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
What is the output of the following code? T = (5) T = T*2 print(T)
TypeError: 'int' object is not subscriptable
What are the standard numeric types in Python?
int, float, complex
What is notable about Python's syntax?
Python's syntax is simple and straightforward, contributing to its popularity.
What are syntax errors in Python?
Syntax errors are basic errors that arise when the Python parser cannot understand a line of code, and they are almost always fatal.
What are the characteristics of list items in Python?
List items are ordered, changeable, and can be repeated.
When does a KeyError occur in Python?
A KeyError occurs when a dictionary object is accessed with a key that does not exist in the dictionary.
What is the output of the given code when var is set to 100?
3 - Got a true expression value 100
What does the dictionary 'college' contain?
ces: block1, it: block2, ece: block3
What is the order of precedence in python?
a) i,ii,iii,iv,v,vi
What does the statement 'StudentDict = dict()' do?
It initializes an empty dictionary named StudentDict.
What is a slice in the context of strings?
A slice is a segment or part of a string that can be selected using the slice operator.
How does operator precedence affect expression evaluation in Python?
Operator precedence determines the order in which operators are evaluated in an expression.
What will be printed when a = -1 and the condition if a < 0 is true?
-1 a is smaller Finish
What is the output of the code: a = 10; if a > 9: print('A is Greater than 9')?
A is Greater than 9
What does an else statement do in an if-else structure?
It contains the block of code that executes if the conditional expression in the if statement resolves to false.
What is the maximum number of else statements that can follow an if statement?
At most only one else statement.
What triggers an IndexError in Python?
An IndexError is triggered when you refer to a sequence that is out of range, such as accessing an index that does not exist in a list.
What will be printed if y is 10?
10
What is an IndentationError in Python?
An IndentationError occurs when there is an unexpected indent or inconsistent indentation in the code.
What is the purpose of the copy() method in a dictionary?
It returns a shallow copy of the dictionary.
What is the output of the following code: Sum = 0 for k in range(5): Sum = Sum+k print(Sum)?
The output is 10.
What is the output of the following code: Sum = 0 for k in range(10, 1, -2): Sum = Sum+k print(Sum)?
The output is 30.
What does it mean that strings are immutable?
We can’t change an existing string; we can only create a new variation.
What are the characteristics of tuples in Python?
Supports all operations for sequences, is immutable, but member objects may be mutable.
What is an example of using if-elif-else to compare three numbers?
a=int(input('enter the number')) b=int(input('enter the number')) c=int(input('enter the number')) if a>b: print('a is greater') elif b>c: print('b is greater') else: print('c is greater')
What is the output of the nested for loop example provided?
The output is: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5.
How are dictionaries created in Python?
Dictionaries are created using curly brackets and consist of key-value pairs.
How can you create an empty tuple in Python?
You can create an empty tuple using X = ().
What will be the value of X in the following Python expression? X = 2+9*((3*12)-8)/10
b) 30.8
How do you access the first and last elements of a tuple?
The first element can be accessed with T[0] and the last element with T[-1].
What is the purpose of a for loop in Python?
A for loop is used for repeated execution of a group of statements for a desired number of times, iterating over items of lists, tuples, strings, dictionaries, and other iterable objects.
What is the syntax of a for loop in Python?
The syntax is: for in : Statement(s)
What does the loop variable hold during each iteration of a for loop?
During each iteration, the loop variable holds a value from the sequence.
What is a list comprehension?
A way to create a new list using an existing sequence.
What does the sys.version_info function return?
It returns the version information of Python, including major, minor, micro, release level, and serial.
What is the purpose of a 'for' loop in Python?
To iterate over a sequence when the number of repetitions is known.
In the example 'list = ['K','V','S','C','H']; i = 1; for item in list:', what is being iterated over?
The loop iterates over the items in the list ['K','V','S','C','H'].
What does the isdigit() method check for?
Returns true if the string contains only digits; false otherwise.
How do you enter n number of students' data in a loop in Python?
for i in range(n): rollno = input('Enter Roll No:'); name = input('Enter Name:'); physicsMarks = int(input('Enter Physics Marks:')); chemistryMarks = int(input('Enter Chemistry Marks:')); mathMarks = int(input('Enter Maths Marks:')); studentDict[rollno] = {'name': name, 'physics': physicsMarks, 'chemistry': chemistryMarks, 'math': mathMarks}
What does the following code output: i=1 while i<=3: print('KV School',end=' ') j=1 while j<=1: print('CS DEPT',end='') j=j+1 i=i+1 print()?
KV School KV School KV School CS DEPT
What is printed when iterating over the keys of the dictionary 'college'?
Keys are: ces, it, ece
How does the 'continue' statement function in a loop?
The 'continue' statement skips the current iteration and continues with the next iteration of the loop.
Find the output of the function example(a) where a = 'hello'.
b) cannot perform mathematical operation on strings
What is the syntax to access an element in a tuple?
The syntax is [index].
What are tokens in Python?
Tokens are the smallest identifiable units in Python, including keywords, identifiers, literals, operators, and delimiters.
What types of data can be stored in a dictionary?
Any type, but keys must be of an immutable type.
What operator is used for integer division in Python?
The operator for integer division is /.
What is the token for the remainder operation in Python?
The token for the remainder operation is %.
What does the binary left shift operator do in Python?
The binary left shift operator is represented by << and shifts the bits of a number to the left.
What types of loops are available in Python?
While loop and for loop.
What is the output of this code: for k in range(1, 10): if k % 2 == 0: print(k, end=', ')?
The output is: 2, 4, 6, 8
What is the purpose of the isalpha() method?
Returns true if the string has at least 1 character and all characters are alphabetic; false otherwise.
What is the first step to create a dictionary to store student details in Python?
studentDict = {}
What does the expression ['Hi!'] * 4 return?
['Hi!', 'Hi!', 'Hi!', 'Hi!']
What does the expression 3 in [1, 2, 3] evaluate to?
True
What does the keys() method return?
It returns a new view of the dictionary's keys.
What is the output of the following code? x=[4,5,66,9] y=tuple(x)
(4, 5, 66, 9)
What is the purpose of the setdefault(key[,d]) method?
If key is in the dictionary, it returns its value. If not, it inserts key with a value of d and returns d (defaults to None).
What are the three types of loop statements in Python?
While Loop, For Loop, Nested For Loops
What does the 'break' statement do in Python?
The 'break' statement terminates the loop containing it and control flows to the statement immediately after the body of the loop.
What are tuples and how do they compare to lists in terms of efficiency?
Tuples are more efficient than lists due to Python's implementation.
What type of error occurs when Python encounters a syntax issue in the code?
A syntax error or parsing error occurs when the proper structure of the language is not followed.
What does x[3:] return for the string 'computer'?
'puter'
What is the output of x[:5] for the string 'computer'?
'compu'
What does x[-1] return for the string 'computer'?
'r'
What is the result of x[-3:] for the string 'computer'?
'ter'
What causes a TypeError in Python?
A TypeError occurs when two unrelated types of objects are combined, such as adding an integer and a string.
What does the letter 'c' indicate in a sequence?
It indicates the difference between every two consecutive numbers in the sequence.
What are lists in Python used for?
Lists are used to store different types of items in a variable, allowing for the collection of data.
What are docstrings in Python?
Strings enclosed within triple quotes that can span multiple lines.
What is the output of the following code: for k in range(4): for j in range(k): print('*', end=' ')?
The output is:
Write python code to sort the list, L, in descending order.
L.sort(reverse=True)
What does the isupper() method do?
Returns true if the string has at least one cased character and all cased characters are in uppercase; false otherwise.
What does L[1:] return if L = ['kvsch', 'school', 'KVSCH!']?
['school', 'KVSCH!']
What does the pop() method do?
The pop() method removes the element at the specified position.
How can you create a list in Python?
You can create a list using square brackets, e.g., list1=[1,2,3,'A','B',7,8,[10,11]].
If l=[11,22,33,44], then output of print(len(l)) will be?
a) 4
What is a nested for loop?
A nested for loop is when one loop is defined within another loop.
How do you search for an item in a list in Python?
You can iterate through the list and compare each element to the item being searched for.
What type of expression does a while loop contain?
A Boolean expression.
What are the characteristics of keys in a Python dictionary?
Keys must be unique and of immutable types.
What are some examples of expressions in Python?
Examples include simple expressions like a value by itself or a variable, and more complex expressions like z=x+20.
What is the output of the dictionary comprehension z={x: x**2 for x in (2,4,6)}?
{2: 4, 4: 16, 6: 36}
What are the symbols used for multi-line comments in Python?
Triple double quotes (""" ) or triple single quotes (''' )
How do you combine text and a variable in Python?
You can combine text and a variable using the '+' character, for example, print('Python is ' + x).
What is the output of list1[2:5] if list1 is [1, 2, 3, 4, 5, 6, 7, 8]?
[3, 4, 5]
What does list1[:6] return if list1 is [1, 2, 3, 4, 5, 6, 7, 8]?
[1, 2, 3, 4, 5, 6]
What is the Python code to create and add items to a dictionary based on user input?
D = {} while True: K = input('type a key'); V = int(input('type the value')); D[K] = V; C = input('type 'y' to add more')
How can you check if a given item is present in a list using a for loop?
for item in list: if item == given_item: print('Item found')
What does the expression len([1, 2, 3]) return?
3
What is the output of the list comprehension: list1=[x**2 for x in range(10)]?
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
What are sequences in Python?
An ordered collection of elements, including Strings, Lists, and Tuples.
What is a set in Python?
An unordered collection of any type without duplicate entries.
What does the isspace() method return?
Returns true if the string contains only whitespace characters; false otherwise.
What is the purpose of the clear() method in a list?
The clear() method removes all the elements from the list.
What does L[-2] return if L = ['kvsch', 'school', 'KVSCH!']?
'school'
What does the count() method return?
Returns the occurrence of a substring in another string.
What does a ValueError in Python indicate?
A ValueError indicates a problem with the content of the object you tried to assign a value to.
What does a while loop do in Python?
A while loop keeps iterating a block of code until the desired condition is met.
What is an expression in Python?
An expression is a combination of values, variables, and operators that is evaluated using an assignment operator.
What command can you use in Python to get help on modules?
You can use the help() command followed by the name of any module, keyword, or topic.
What does the slice x[1:4] return for the string 'computer'?
'omp'
Select all options that print. hello-how-are-you
Options that use print() function with the string.
What are keywords in Python?
Keywords are reserved words that have special meanings, such as int, print, input, for, and while.
What are the rules for naming identifiers in Python?
Identifiers cannot use keywords or operators, must start with an alphabet or underscore, cannot contain spaces, cannot start with a number, and are case sensitive.
What is a variable in Python?
A variable is used to store data and is an object in Python, representing a reserved memory location to store values.
What is the output of the following code: L = [1, 2, 3, 4, 5]; for var in L: print(var, end=' ')?
The output is: 1 2 3 4 5
What does the letter 'b' indicate in a sequence?
It indicates the end of the sequence.
What does the clear() method do in a dictionary?
It removes all items from the dictionary.
What are examples of immutable types?
int, float, string, tuple.
What is sequential execution?
It means executing the statements one by one starting from the first instruction onwards.
What does the islower() method determine?
Returns true if the string has at least 1 cased character and all cased characters are in lowercase; false otherwise.
What is the syntax of an if statement in Python?
if : statement(s)
What is the output of the following code: for k in range(5, 0, -1): for j in range(k): print('*', end=' ')?
The output is:
How does Python handle variable declaration?
Python variables do not need explicit declaration; memory is reserved automatically upon assignment.
What does the count() method do in a list?
The count() method returns the number of elements with the specified value.
What are the items in the list if the output is ['b', 'c', 'd']?
['b', 'c', 'd']
What is the function of the insert() method in a list?
The insert() method adds an element at the specified position.
What will be the output of print('hello' + '-' + 'how' + '-' + 'are' + 'you')?
hello-how-areyou
What does the code 'if C!=’y’: break' do?
It breaks out of a loop if the variable C is not equal to 'y'.
What is the result of (10+10)*2?
40
What error occurs when you execute the following Python code snippet? apple = mango
b) NameError
What is the purpose of 'studentDict[rollno]=[name, physicsMarks, chemistryMarks, mathMarks]'?
It assigns a list of student details to a key 'rollno' in the dictionary studentDict.
What is the output of print(sys.version) in Python?
It outputs the version of Python currently being used, such as '3.8.0'.
What is the result of the slice x[1:6:2] for the string 'computer'?
'opt'
Can you modify the elements of a tuple after it is created?
No, tuples are immutable and cannot be modified after creation.
What error is raised if you try to modify a tuple?
A TypeError is raised if you try to modify a tuple.
What does the count() function do when used with tuples?
The count() function returns the number of times a specified value occurs in a tuple.
How do you find the index of a specified value in a tuple?
You can use the index() function to find the position of a specified value in a tuple.
What does x[:-2] return for the string 'computer'?
'comput'
What does the calendar.isleap function determine?
It determines whether a given year is a leap year or not.
What are the three types of flow of execution in Python?
What is a string in Python?
A sequence of characters, enclosed in single or double quotes.
What is the purpose of selection or conditional statements?
They help execute statements based on whether a condition is evaluated to True or False.
What is the function of the items() method in a dictionary?
It returns a new view of the dictionary's items (key, value).
What is the value of T if T is an integer and equals 10?
10
What does L[2] return if L = ['kvsch', 'school', 'KVSCH!']?
'KVSCH'
Provide an example of assigning an integer to a variable in Python.
a = 100
What is the purpose of the reverse() method?
The reverse() method reverses the order of the list.
Which point can be considered as difference between string and list?
b. Mutability
What are the first four prime numbers printed from the tuple?
2, 3, 5, 7
What is the purpose of a statement in Python?
A statement is an instruction that the Python interpreter can execute.
What does the expression e = (a + b) * c / d evaluate to when a=20, b=10, c=15, d=5?
90.0
What does the assignment operator do in Python?
The assignment operator assigns a value to a variable and evaluates expressions.
What is the character set recognized by Python?
The character set includes letters (A-Z, a-z), digits (0-9), special symbols (+, -, /, %, **, *, [], {}, #, $), white spaces (blank space, tabs, carriage return, newline, form feed), and all ASCII and UNICODE characters.
What types of operators does Python support?
Python supports Arithmetic Operators, Relational Operators, Logical Operators, and more.
What is the token for addition in Python?
The token for addition is +.
What are the three programming constructs supported by Python?
Sequence, Selection, and Iteration.
What is the difference between mutable and immutable types?
Immutable types cannot be modified, while mutable types can be modified.
How do you create a list of odd numbers within a given range m and n in Python?
L = [x for x in range(m, n + 1) if x % 2 != 0]
What does the following code output: for k in range(10): print(k, end=', ')?
The output is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
What does the isalnum() method do?
Returns true if the string has at least 1 character and all characters are alphanumeric; false otherwise.
How do you create a list of squares of numbers from 0 to 10 in Python?
L = [x**2 for x in range(11)]
What is the result of the expression [1, 2, 3] + [4, 5, 6]?
[1, 2, 3, 4, 5, 6]
What does the list comprehension x=[z**2 for z in range(10) if z>4] output?
[25, 36, 49, 64, 81]
What is the output of x=[x ** 2 for x in range(1, 11) if x % 2 == 1]?
[1, 9, 25, 49, 81]
What happens if the boolean expression in an if statement evaluates to TRUE?
The block of statement(s) inside the if statement is executed.
What is the value of T if T is a tuple and equals (5, 5)?
(5, 5)
What is the purpose of the replace() method?
Replaces all occurrences of old in the string with new, or at most max occurrences if max is given.
What does the values() method return?
It returns a new view of the dictionary's values.
What does the remove() method do in a dictionary?
It removes or pops the specific item from the dictionary.
What does the remove() method do in a list?
The remove() method removes the first item with the specified value.
Which of the following can be used as valid variable identifier(s) in Python? a. total b. 7Salute c. Que$tion d. global
a. total
What is the output of the print statement in the first example?
School 1 is K, School 2 is V, School 3 is S, School 4 is C, School 5 is H
What is the output of the nested for loop example provided?
The output is: 1 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
What is a Python module?
A Python module is a program file that contains Python code, including functions, classes, or variables, and is saved with the .py extension.
What does the print() function do in Python?
It displays something on the screen/monitor.
What does the index() method do in a tuple?
It returns the index of the first occurrence of a specified value in the tuple.
What does the if-else statement do in Python?
It allows the program to execute one block of code if a condition is true and another block if it is false.
In the expression x = 7 + 3 * 2, what value is assigned to x and why?
x is assigned 13 because multiplication has higher precedence than addition.
What are some features that make Python powerful?
Dynamic typing, built-in types and tools, library utilities, third-party utilities (e.g., NumPy, SciPy), and automatic memory management.
What does it mean that Python is portable and platform independent?
Python runs on virtually every major platform, and programs will run the same way as long as a compatible Python interpreter is installed.
What makes Python easy to use and learn?
Python does not require an intermediate compiler, programs are compiled to byte code automatically, and its structure and syntax are intuitive.
How is Python processed?
Python is processed at runtime by the Python interpreter.
What is the output of the following code? T = (5, ) T = T*2 print(T)
(5, 5)
What will be printed if y is 30?
30
What does x[::-1] return for the string 'computer'?
'retupmoc'
What does the '+' operator do when used with strings?
It concatenates the strings.
How are items accessed in a list?
Items in a list are accessed using indexes.
What is the output of the range from 0 to 8?
0,1,2,3,4,5,6,7,8
What does list1[:1] return when list1 = [1,2,3,4,5,6,7,8,9,10]?
[1]
What is the output of the following code? A = 0 while A < 10:
The loop will execute 10 times, printing the value of A from 0 to 9.
What does the update([other]) method do?
It updates the dictionary with the key/value pairs from other, overwriting existing keys.
Provide an example of assigning a string to a variable in Python.
c = 'John'
What is the function of the del() method?
It deletes a particular item from the dictionary.
How to remove an element from the list L as entered by the user?
Write Python code to remove an element as entered by the user from the list, L.
The statement del l[1:3] does which of the following task?
b) delete 2nd and 3rd element from the list
In the second example with a=10 and b=20, what will be printed?
The output will be 'B is Greater than A'.
What is the output of the code: x = 'Python is '; y = 'awesome'; z = x + y; print(z)?
The output is 'Python is awesome'.
Can there be multiple elif statements following an if statement?
Yes, there can be an arbitrary number of elif statements following an if.
What is the output of the expression list(Name) when Name = 'abcdefg'?
['a', 'b', 'c', 'd', 'e', 'f', 'g']
What do the + and * operators do when used with lists in Python?
They perform concatenation and repetition, resulting in a new list.
What is list comprehension in Python?
A way to create a new list using an existing list with the syntax: NewList = [ expression for variable in iterableobject if condition == True ]
What does the None type represent in Python?
A special type with an unidentified value or absence of value.
What is the output of the following code? L = [1,2,3,4,5,6,7,8,9] print(L[:-1])
[1, 2, 3, 4, 5, 6, 7, 8]
What is the output of the following code? S = 'abcdefgh' L = list(S) print(L[1:4])
['b', 'c', 'd']
What is the output of list1[1:] if list1 = [1,2,3,4,5,6,7,8,9,10]?
[2, 3, 4, 5, 6, 7, 8, 9, 10]
How many times will the following loop execute? A = 0 while True: print(A) A = A + 1
The loop will execute infinitely.
What operator is used to assign values to variables in Python?
The assignment operator (=).
Provide an example of assigning a floating point value to a variable in Python.
b = 1000.0
What is the function of the find() method?
Finds the index of the first occurrence of a substring in another string.
What does the startswith() method determine?
Determines if the string or a substring starts with a specified substring; returns true if so, false otherwise.
What will be the output of print('hello-' + 'how-are-you')?
hello-how-are-you
What kind of error message will be generated if the following code is executed? A = 5 B = 'hi' d = A+B print(D)
NameError: name 'D' is not defined
What will be printed if y is not defined?
What is a run-time error in Python?
A run-time error happens when Python understands the code but encounters trouble when executing it.
What is the function of the '*' operator when used with strings?
It causes repetition of the characters the specified number of times.
What are mappings in Python?
Dictionaries, which consist of key-value pairs where keys are used to access values.
What does the istitle() method check?
Returns true if the string is properly 'titlecased'; false otherwise.
What error occurs if an operation is performed on incompatible types?
TypeError
What does the extend() method do?
The extend() method adds the elements of a list (or any iterable) to the end of the current list.
What is the Python code to sort a list L in reverse order?
L.sort(reverse=True)
What is the output of print(list1) if list1 is defined as [1,2,3,'A','B',7,8,[10,11]]?
The output will be [1, 2, 3, 'A', 'B', 7, 8, [10, 11]].
Which of the following method is used to delete element from the list?
c) pop()
What is the purpose of Boolean data type in Python?
To represent True or False values, typically used in comparisons.
What is the output of the following code? L = [1,2,3,4,5,6,7,8,9] print(L[:])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
What will be printed if y is an infinite loop?
infinite loop. Condition / test expression is always True.
What is the output of the range from 0 to 9?
0,1,2,3,4,5,6,7,8,9
What is a tuple in Python?
A collection that is ordered and unchangeable, created by enclosing items within round brackets.
What happens when you use the pop(key[,d]) method?
It removes the item with key and returns its value or d if key is not found. Raises KeyError if d is not provided and key is not found.
What is the output of the list [1,2,3,4,5,6,7,8]?
[1,2,3,4,5,6,7,8]
What will be the output of print('hello', 'how', 'are', 'you' + '-' * 4)?
hello how are you----
What does the fromkeys(seq[, v]) method return?
It returns a new dictionary with keys from seq and value equal to v (defaults to None).
What does the get(key[,d]) method do?
It returns the value of key. If key does not exist, it returns d (defaults to None).
What does it mean that lists are mutable?
It means that the contents of a list can be changed after it is created.
What is the difference between mutable and immutable types?
Mutable types can be changed after creation, while immutable types cannot.
What does the popitem() method do?
It removes and returns an arbitrary item (key, value). Raises KeyError if the dictionary is empty.
Provide an example of a multi-line string assignment in Python.
d = '''Hello world good morning'''
How to display all items of the dictionary D as individual tuples?
D = {'A': 20, 'B': 30, 'C': 40, 'D': 50}
What is the function of the isnumeric() method?
Returns true if the string contains only numeric characters; false otherwise.
What does the append() method do in a list?
The append() method adds an element at the end of the list.
When should you use a tuple instead of a list?
When the contents shouldn't change, to prevent items from being accidentally added, changed, or deleted.
What is the output of the list [1,2,3,4,5,6,7,8,9]?
[1,2,3,4,5,6,7,8,9]
What is the output of the expression 1 + 1?
1 1
What is the syntax for creating a dictionary comprehension?
The syntax is = { expression for in sequence }.
Which of the following forces an expression to be converted into specific type?
d) Explicit type casting
What is the output of the following code? L = [1,2,3,4,5,6,7,8,9] print(L.count(2)) print(L.index(2))
1 1
What happens if the boolean expression in an if statement evaluates to FALSE?
The first set of code after the end of the if statement(s) is executed.
What does the copy() method return?
The copy() method returns a copy of the list.
What will be the output of print(x) if x is defined as list()?
The output will be x [], indicating an empty list.
What will be the output of print('hello', 'how', 'are', 'you')?
hello how are you
What does the step argument in range() function indicate?
It indicates the increment between each number in the sequence.
What does the split() method do?
Splits the string according to a delimiter (space if not provided) and returns a list of substrings.
What does the swapcase() method do?
Converts lowercase letters in a string to uppercase and vice versa.
What does the sort() method do?
The sort() method sorts the list.
Which of the following statement is true for extend() list method?
c) adds multiple elements at last
What is one of the key characteristics of Python related to its programming paradigm?
Python is Object-Oriented and supports concepts such as polymorphism, operator overloading, and multiple inheritance.
What does the index() method return?
The index() method returns the index of the first element with the specified value.
What is the tuple if the output is (4, 5, 66, 9)?
(4, 5, 66, 9)
Which of the following statement is correct for an AND operator?
a) Python only evaluates the second argument if the first one is False
How do you get the length of a dictionary?
You use the len() method to get the length of the dictionary.
How does Python identify blocks of code?
Python uses indentation to identify blocks of code.