NEW
Font size
WorksheetsPython Programming Basics
Total questions: 20
Worksheet time: 10mins
What is a variable in Python?
A variable in Python is a type of function.
A variable in Python is a named reference to a value stored in memory.
A variable in Python is a fixed value that cannot change.
A variable in Python is a command used to execute code.
Which of the following is a valid data type in Python?
string
boolean
float
int
What is the output of the expression 5 + 3 * 2?
16
11
8
10
How do you create a function in Python?
function function_name(parameters) {}
function_name = (parameters) => {}
create function_name(parameters):
def function_name(parameters): # function body
What keyword is used to define a function?
procedure
def
function
method
What is the purpose of the 'if' statement in Python?
To execute code conditionally based on a boolean expression.
To define a function in Python.
To import modules in Python.
To create a loop in Python.
How do you create a list in Python?
You create a list in Python by using the list() function, e.g., my_list = list(1, 2, 3).
You create a list in Python using square brackets, e.g., my_list = [1, 2, 3].
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).
What is the difference between a list and a tuple?
A list can only contain numbers, while a tuple can contain any data type.
A list is immutable and defined with [], while a tuple is mutable and defined with ().
A list is defined with {}, while a tuple is defined with []
A list is mutable and defined with [], while a tuple is immutable and defined with ().
How do you access the first element of a list?
my_list.first()
my_list[0]
my_list(1)
first(my_list)
What operator is used for exponentiation in Python?
^
**-
**/
**
How do you concatenate two strings in Python?
Use the '*' operator to concatenate strings.
Use the 'concat()' method to join strings.
Use the '+' operator to concatenate strings.
Use the '&' operator to combine strings.
What method would you use to add a key-value pair to a dictionary?
dictionary.insert(key, value)
dictionary.put(key, value)
dictionary.add(key, value)
dictionary[key] = value
How do you create a set in Python?
You can create a set using square brackets []
You can create a set in Python using curly braces {} or the set() function.
Sets can only be created with the list() function
You must use the dict() function to create a set
What is the output of len('Hello')?
5
4
Hello
6
How do you import a module in Python?
include module_name
require module_name
import module_name
load module_name
What is the purpose of the 'return' statement in a function?
To create a loop within the function.
To end the function execution immediately.
To define the function's parameters.
The purpose of the 'return' statement is to provide a value from a function to its caller.
How can you check if a key exists in a dictionary?
Check the length of the dictionary to see if the key exists.
Use 'dictionary.key' to access the value directly.
Iterate through the dictionary and compare each key.
Use 'key in dictionary' or 'dictionary.get(key)'.
What is the result of the expression 'Hello' * 3?
Hello3
HelloHelloHello
3Hello
HelloHello
How do you remove an item from a list?
list.clear() to remove all items from a list.
list.delete(index) removes an item from a list.
Use list.add(value) to insert an item into a list.
Use list.remove(value) or list.pop(index) to remove an item from a list.
What is the difference between mutable and immutable types in Python?
Mutable types include only strings and tuples, while immutable types include lists and dictionaries.
Mutable types can be changed (e.g., lists, dictionaries), while immutable types cannot be changed (e.g., strings, tuples).
Both mutable and immutable types can be changed at any time.
Mutable types cannot be changed, while immutable types can be changed.
