WorksheetsBasics of Python Programming
Total questions: 35
Worksheet time: 20mins
What is a data type in Python?
A data type in Python is a classification that defines the type of value a variable can hold.
A data type in Python is a method for executing functions.
A data type in Python is a way to store files on disk.
A data type in Python is a framework for building applications.
Name the four basic data types in Python.
int, float, list, set
int, float, str, bool
char, double, list, dict
str, bool, tuple, array
What keyword is used to define a function in Python?
def
method
procedure
function
What is the purpose of the 'if' statement in Python?
To define a function in Python.
To execute code conditionally based on a boolean expression.
To import modules into a Python script.
To create a loop that iterates over a list.
How do you create a list in Python?
You create a list in Python using curly braces, e.g., my_list = {1, 2, 3}.
You create a list in Python using parentheses, e.g., my_list = (1, 2, 3).
You create a list in Python using angle brackets, e.g., my_list = <1, 2, 3>.
You create a list in Python using square brackets, e.g., my_list = [1, 2, 3].
What is the difference between a list and a tuple in Python?
Tuples are created using square brackets, while lists use parentheses.
Lists can contain only numbers, while tuples can contain any data type.
The main difference is that lists are mutable and tuples are immutable.
Lists are faster than tuples in all operations.
What keyword is used to start a loop in Python?
iterate
loop
for, while
repeat
What does the 'break' statement do in a loop?
The 'break' statement pauses a loop.
The 'break' statement restarts the loop from the beginning.
The 'break' statement exits a loop.
The 'break' statement skips the current iteration of a loop.
How do you read input from the user in Python?
Use the 'read()' function.
Use the 'get_input()' function.
Use the 'scan()' function.
Use the 'input()' function.
What is the purpose of the 'return' statement in a function?
The purpose of the 'return' statement is to provide a value from a function to its caller.
To define the function's parameters.
To create a loop within the function.
To end the function execution immediately.
What is a dictionary in Python?
A dictionary is a type of list in Python.
A dictionary in Python is a single value.
A dictionary in Python is a collection of key-value pairs.
A dictionary is a collection of ordered items in Python.
How do you handle exceptions in Python?
Use switch-case statements to handle exceptions in Python.
Use if-else statements to handle exceptions in Python.
Ignore exceptions and continue execution.
Use try-except blocks to handle exceptions in Python.
What is the purpose of the 'for' loop in Python?
To iterate over a sequence or iterable object.
To handle exceptions in Python.
To create a new variable in Python.
To define a function in Python.
What is the difference between '==' and 'is' in Python?
'==' is faster than 'is' in execution.
'==' checks data types, 'is' checks values.
'==' is used for assignment, 'is' for comparison.
'==' compares values, 'is' compares identities.
How do you define a method in a class in Python?
def method_name(self):
def method_name():
method_name(self):
def method(self):
What is the purpose of the 'while' loop in Python?
To execute a block of code only once if the condition is true.
To define a function that runs when called.
To create a static loop that does not depend on any condition.
The purpose of the 'while' loop is to execute a block of code repeatedly as long as a condition is true.
How do you convert a string to an integer in Python?
int('string')
string.to_int()
str('string')
convert('string')
What is the significance of indentation in Python?
Indentation is used to define variable types in Python.
Indentation is significant in Python as it defines code blocks and enforces readability.
Indentation is optional in Python and has no effect on code execution.
Indentation is only used for comments in Python code.
How do you write a comment in Python?
Use '/*' to start a comment in Python.
Use '/* comment */' for comments in Python.
Use '#' to write a comment in Python.
Use '//' to write a comment in Python.
How do you create a dictionary in Python?
You create a dictionary in Python using angle brackets, e.g., my_dict =
You create a dictionary in Python using parentheses, e.g., my_dict = (key: value).
You create a dictionary in Python using curly braces, e.g., my_dict = {key: value}.
You create a dictionary in Python using square brackets, e.g., my_dict = [key: value].
What is the purpose of the 'elif' statement in Python?
To end the execution of a conditional block.
To create a loop that iterates over multiple conditions.
To execute a block of code if the previous conditions are false and a new condition is true.
To define a function within a conditional block.
How do you remove an item from a list in Python?
list.clear(item)
list.remove(item)
list.delete(item)
list.pop(item)
How do you concatenate two strings in Python?
Use the 'concat()' method.
Use the 'append()' method.
Use the '&' operator, e.g., str1 & str2.
Use the '+' operator, e.g., str1 + str2.
Which of the following correctly assigns a list in Python?
my_list = [1, 2, 3, 4]
my_list = {1, 2, 3}
my_list = [1, 2, 3)
my_list = (1, 2, 3)
What does the append() method do in Python lists?
Concatenates two lists together.
Adds a single element to the end of the list.
Inserts an element at a specified index in the list.
Removes the last element from the list.
How can you find the index of a specific element in a Python list?
mylist.locate('item')
mylist.find('item')
mylist.get('item')
mylist.index('item')
How does the sort() method arrange elements in a Python list?
Descending order by default
Random order
Alphabetical order by default
Ascending order by default
Code Trace: Given the list [3, 7, 2, 8], what will be the output of list.index(7)?
4
2
5
1
Code Trace: If you have a list [5, 2, 9, 1] and you execute list.remove(9), what will the list look like after this operation?
[2, 9, 1]
[5, 9, 1]
[5, 2, 1]
[5, 2, 9]
What will be the output of list.insert(2, 'hello') if the original list is ['apple', 'banana', 'cherry']?
['apple', 'hello', 'cherry', 'banana']
['apple', 'banana', 'cherry', 'hello']
['apple', 'banana', 'hello', 'cherry']
['apple', 'hello', 'banana', 'cherry']
If you have a list [4, 1, 6, 3], what will the list look like after executing list.sort()?
[6, 4, 3, 1]
[3, 6, 1, 4]
[1, 4, 3, 6]
[1, 3, 4, 6]
What will be the output of list.count(5) if the list is [5, 2, 5, 8, 5]?
3
2
4
1
Explain the difference between append() and insert() methods in Python lists.
append() inserts an element at a specified index in the list
append() adds an element to the beginning of the list
insert() adds an element to the end of the list
The main difference between append() and insert() methods in Python lists is that append() adds an element to the end of the list, whereas insert() inserts an element at a specified index in the list.
In Python, how can you access the last element of a list using negative indexing?
list[-len(list)]
list[-2]
list[-1]
list[0]
What is the output of program below?
mariana_islands = ['Guam', 'Saipan', 'Tinian', 'Rota']
print(mariana_islands[1])
Guam
Saipan
Tinian
Rota
