commands Flashcards
What is the command to print ‘Hello, World!’ in Python?
print(‘Hello, World!’)
True or False: In Python, indentation is used to define blocks of code.
True
Fill in the blank: The command to create a list in Python is ________.
list()
What is the syntax for defining a function in Python?
def function_name(parameters):
Which command is used to read a file in Python?
open()
What will the following command return? len([1, 2, 3])
3
What is the command to generate a random integer between 1 and 10?
import random; random.randint(1, 10)
True or False: Lists in Python are immutable.
False
What is the purpose of the ‘import’ statement in Python?
To include external modules and libraries in your code.
Fill in the blank: The command to create a dictionary in Python is ________.
dict()
Which keyword is used to create a loop that iterates over a sequence in Python?
for
What is the output of the command: print(type(5.0))?
<class ‘float’>
What command is used to handle exceptions in Python?
try…except
Multiple choice: Which of the following is a valid variable name in Python? A) 1st_variable B) first-variable C) first_variable
C) first_variable
What is the command to convert a string to an integer in Python?
int()
True or False: The ‘while’ loop will execute at least once.
False
What is the command to remove an item from a list in Python?
list.remove(item)
Fill in the blank: The command to sort a list in Python is ________.
list.sort()
What is the function used to find the maximum value in a list?
max()
What will the following command return? ‘Python’.lower()
‘python’
Multiple choice: Which of the following methods can be used to add an item to a list? A) append() B) add() C) insert()
A) append() and C) insert()
What is the command to create a tuple in Python?
tuple()
What is the purpose of the ‘return’ statement in a function?
To exit the function and optionally pass an expression back to the caller.
True or False: Strings in Python are mutable.
False
What is the command to get the current working directory in Python?
import os; os.getcwd()
Fill in the blank: The command to create a set in Python is ________.
set()
What command is used to print output in Python?
The ‘print()’ function.
Fill in the blank: To create a list in Python, you use ________.
square brackets []
True or False: The ‘len()’ function returns the number of items in an object.
True
What is the command to create a new dictionary in Python?
‘my_dict = {}’ or ‘my_dict = dict()’
Which command is used to read a file in Python?
‘open()’ function.
What is the output of ‘print(type(5))’?
<class ‘int’>
Choose the correct syntax to create a function in Python.
def function_name(parameters):
What command would you use to iterate over a list?
‘for item in list:’
Fill in the blank: To convert a string to an integer, use ________.
‘int()’
True or False: Lists in Python are immutable.
False
What is the command to import a module in Python?
‘import module_name’
What is the purpose of the ‘if __name__ == “__main__”:’ statement?
To allow or prevent parts of code from being run when the modules are imported.
Which command is used to add an item to a list?
‘list.append(item)’
What does the ‘range()’ function do?
Generates a sequence of numbers.
Fill in the blank: The command to handle exceptions is ________.
‘try-except’
True or False: A tuple can be modified after creation.
False
What command would you use to remove an item from a dictionary?
‘del my_dict[key]’
What is the syntax to define a class in Python?
class ClassName:
Choose the correct command to sort a list in Python.
‘list.sort()’
What is the command to get the current working directory?
‘os.getcwd()’
Fill in the blank: To create a set in Python, you use ________.
curly braces {} or ‘set()’
What does the ‘map()’ function do?
Applies a function to all items in an iterable.
True or False: The ‘filter()’ function returns an iterator.
True
What command would you use to create a virtual environment?
‘python -m venv env_name’
Fill in the blank: To concatenate two strings, use ________.
’+’ operator.
What is the command to check if a key exists in a dictionary?
‘key in my_dict’
What is the output of ‘print(3 ** 2)’?
9