Python General Flashcards
(95 cards)
What is Python’s memory model primarily based on?
Reference counting and garbage collection.
True or False: Tuples are mutable in Python.
False.
What is the syntax to create a list in Python?
Use square brackets, e.g., my_list = [].
Fill in the blank: In Python, a ______ is an ordered collection of items.
list
What is the main difference between lists and tuples in Python?
Lists are mutable; tuples are immutable.
How do you define a string in Python?
Enclose characters in single or double quotes, e.g., ‘hello’ or “hello”.
What is a class in Python?
A blueprint for creating objects that encapsulate data and functionality.
True or False: Meta classes in Python are classes of classes.
True.
What keyword is used to define a function in Python?
def
What does the ‘len()’ function do?
Returns the number of items in an object.
What will be the output of ‘str(123)’?
‘123’
Which method is used to add an item to the end of a list?
append()
Fill in the blank: A ______ is a collection of key-value pairs in Python.
dictionary
What is the purpose of the ‘self’ parameter in class methods?
To refer to the instance of the class.
True or False: Strings in Python are mutable.
False.
What is the output of ‘5 in [1, 2, 3, 4, 5]’?
True.
How can you create a tuple with one item?
Use a comma, e.g., my_tuple = (1,).
What built-in function can convert a list to a tuple?
tuple()
What is the difference between ‘==’ and ‘is’ in Python?
’==’ checks for value equality; ‘is’ checks for object identity.
Fill in the blank: A ______ allows you to iterate over a sequence in Python.
for loop
What does the ‘pop()’ method do in list?
Removes and returns the last item from the list.
How do you define a meta class in Python?
By inheriting from type.
True or False: You can concatenate two strings using the ‘+’ operator.
True.
What is the primary use of the ‘with’ statement?
To wrap the execution of a block with methods defined by a context manager.