Python Flashcards
declare a list object
my_list = [1, 2, 3, 4, 5]
Declare a dictionary object
my_dict = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
Pull a value from a dictionary by key
value_from_dict = my_dict[‘key2’]
Pull a value from a list
value_from_list = my_list[2]
What do class decorators do
they are functions that modify the behavior of a class or methods within a class. They allow you to add functionality to classes or methods without modifying their underlying code.
Explain the difference between == and is in Python
The == operator is used to compare the values of two objects, while the is operator is used to check if two variables refer to the same object in memory.
What is a decorator in Python?
A decorator in Python is a design pattern that allows you to modify or extend the behavior of functions or classes without directly modifying their code. Decorators are implemented as callable objects that take a function or class as input and return a new function or class with additional functionality.
What is a generator in Python?
A generator in Python is a special type of iterator that generates values on the fly rather than storing them in memory. It allows you to iterate over a sequence of values one at a time, which is memory-efficient and suitable for handling large datasets or infinite sequences.
What is the purpose of the __init__ method in Python classes?
a special method in Python classes that is called automatically when a new instance of the class is created. It is used to initialize the object’s attributes or perform any necessary setup operations.
Explain the difference between lists and tuples in Python
lists are mutable (modifiable) while tuples are immutable (unchangeable). Lists are defined using square brackets [ ], and tuples are defined using parentheses ( ).
Declare a tuple object
my_tuple = (1, 2, 3, 4, 5)
What is the difference between var= (42) and var= (42,)
The first is the value 42 and the second is a tuple because of the comma
of dictionaries, tuples and lists – which can not be changed
Tuples
List the 3 types of “list” objects with the characters that declare them
1 - Dictionaries: Declared using curly braces {} with key-value pairs separated by colons : ({‘key’: ‘value’}).
2 - Tuples: Declared using parentheses () with comma-separated values ((1, 2, 3)).
3 - Lists: Declared using square brackets [] with comma-separated values ([1, 2, 3])
of dictionaries, tuples and lists – which is not ordered
Dictionary