Advanced Python Flashcards

1
Q

\d

A

matches a digit (0-9)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

?

A

A: The preceding character or group is optional (0 or 1 time).
Example: Breon?a matches Breona, Breoa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: What does + mean in regex?

A

A: The preceding character or group must occur 1 or more times.
Example: fre+ matches fre, free, freeee

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: What does * mean in regex?

A

A: The preceding character or group can occur 0 or more times.
Example: fre* matches fr, fre, free, freeee

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

[]

A

matches any character inside of the brackets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Visualize tuple unpacking

A

you can use tuple unpacking
mylist = [(1,2),(3,4),(5,6),(7,8)]
for item in mylist:
print(item)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Visualize printing out the items of a dictionary

A

d = {‘k1’:1, ‘k2’:2,’K3’:3}
# for default you iterate through the keys
for item in d:
print(item)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does this print?
d = {‘k1’:1, ‘k2’:2,’K3’:3}
for item in d.items():
print(item)

A

(‘k1’, 1)
(‘k2’, 2)
(‘K3’, 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the impact of continue?
for letter in mystring:
if letter == ‘a’:
continue #go back to the loop
print(letter)

A

It continues pass a, and prints the next characters
Smmy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Enumerate()

A

allows you to iterate over an iterable (like a list, string, or tuple) while keeping track of the index and value of each element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the output?

words = [‘apple’, ‘banana’, ‘cherry’]
for index, value in enumerate(words):
print(f”Index: {index}, Value: {value}”)

A

Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: cherry

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the output?

words = [‘apple’, ‘banana’, ‘cherry’]
indexed_words = list(enumerate(words))

print(indexed_words)

A

[(0, ‘apple’), (1, ‘banana’), (2, ‘cherry’)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the output?

index_count = 0
for letter in “abcde”:
print(‘At index {} the letter is {}.’.format(index_count,letter))
index_count +=1

A

At index 0 the letter is a.
At index 1 the letter is b.
At index 2 the letter is c.
At index 3 the letter is d.
At index 4 the letter is e.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

zip()

A

Combines multiple iterables (like lists, tuples, or strings) element-wise into pairs (tuples

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the output of this?
names = [“Alice”, “Bob”, “Charlie”]
ages = [25, 30, 35]

zipped = zip(names, ages)
print(list(zipped))

A

[(‘Alice’, 25), (‘Bob’, 30), (‘Charlie’, 35)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the output of this?
for name, age in zip(names, ages):
print(f”{name} is {age} years old.”)

A

Alice is 25 years old.
Bob is 30 years old.
Charlie is 35 years old.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How would you unzip this?
zipped_data = [(‘Alice’, 25), (‘Bob’, 30), (‘Charlie’, 35)]

A

names, ages = zip(*zipped_data)

print(list(names)) # [‘Alice’, ‘Bob’, ‘Charlie’]
print(list(ages)) # [25, 30, 35]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

List Comprehensions

A

allow you to generate a new list by applying an expression to each item in an existing iterable (like a list, range, or tuple)

new_list = [expression for item in iterable if condition]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Use a list comprehension to: Create a list of squares of numbers from 1 to 5.

A

squares = [x**2 for x in range(1, 6)]
print(squares) # Output: [1, 4, 9, 16, 25]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Use a list comprehension to generate a list of all combinations of numbers and letters:

A

combinations = [(x, y) for x in range(2) for y in ‘AB’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Use a function with tuple unpacking to increase the stock prices by 10 percent.
stock_prices = [(‘APPL’, 200), (‘GOOG’,400), (‘MSFT’, 100)]

A

for ticker,price in stock_prices:
print(price+(0.1*price))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

*args

A

args allows you to pass a tuple of parameters coming in.
arg is an arbitrary choice as long as followed by *.
But you should always use *args for style

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

**kwargs

A

**kwargs returns a dictionary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Visualize how to use *args

A

def myfunc(*args): #the user can pass in as many arguments as wanted
return sum(args) * 0.05

myfunc(40,60,100,1,34)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Visualize how to use **kwargs
def myfruit(**kwargs): if 'fruit' in kwargs: print('My fruit of choice is {}'.format(kwargs['fruit'])) else: print("No fruit here") myfruit(fruit='apple', veggie='lettuce')
26
Visualize how to use both *args and **kwargs
def use_both(*args,**kwargs): #this has to go in the order print(args) print(kwargs) print('I would like {} {}'.format(args[0],kwargs['food'])) use_both(10,20,30, fruit='orange',food='eggs')
27
What are four general formulas for exponents?
r**3 = r³ r**2 = r² r**0.5 = √r (square root) r**-1 = 1/r (reciprocal)
28
map()
Takes a function and an iterable (like a list). Applies the function to each element in the iterable. Returns a map object (convert it to a list using list()).
29
Apply map to the below and tell the output: numbers = [1, 2, 3, 4, 5] # Using map() with a function def square(x): return x**2
squared = list(map(square, numbers)) print(squared) Output: [1, 4, 9, 16, 25] Alternate is: squared = list(map(lambda x: x**2, numbers))
30
T or F: map() does not need a function to execute.
False. map() requires a function. If you already have a function (def my_function()), you can just pass it to map(). If you don’t want to define a separate function, lambda lets you write a function inline for quick, one-time use.
31
Use filter() to extract only even numbers from this list: nums = [5, 10, 15, 20, 25, 30]
nums = [5, 10, 15, 20, 25, 30] evens = list(filter(lambda x: x % 2 == 0, nums)) print(evens) # Output: [10, 20, 30]
32
Use map() to capitalize the first letter of each word: fruits = ["mango", "orange", "peach"]
fruits = ["mango", "orange", "peach"] capitalized = list(map(str.capitalize, fruits)) print(capitalized) # Output: ['Mango', 'Orange', 'Peach']
33
filter()
Takes a function that returns True or False. Filters the iterable, keeping only the elements where the function returns True.
34
lambda: Anonymous Inline Functions
A small one-line function with no name (anonymous). Used when you need a function just once (e.g., inside map() or filter()).
35
Visualize using a lambda to add two numbers
add = lambda x, y: x + y print(add(3, 5))
36
Explain the LEGB Rule: Understanding Scope in Python
L - Local: Variables defined inside the current function. E - Enclosing: Variables in the enclosing (outer) function, used in nested functions. G - Global: Variables defined at the top level of a script or module. B - Built-in: Names from Python's built-in functions and libraries (like len, print, range).
37
What are key concepts in Object Oriented Programming?
Classes → A blueprint for creating objects. Objects → An instance of a class. Attributes → Variables inside a class that store data. Methods → Functions inside a class that operate on the object's data. self → Refers to the current instance of the class.
38
what are class object attributes?
Class Object Attributes are defined outside of __init__(). All instances of the class share this attribute. In this case, every Dog instance will have species = "mammal".
39
How do you create a class in python?
class Name():
40
What is the method constructor?
__init__() is a constructor method that runs automatically when a new object is created. self refers to the instance being created. The attributes breed, name, and spots are passed as arguments.
41
Why is the self keyword important to an object?
Passed inside the __init__() along with the class attributes, the self keyword is assigned to attributes. The self keyword allows each instance to have its own unique values
42
Visualize passing attributes to a class
def __init__(self, attr1, attr2, attr3) self.attr1 = value self.attr2 = value self.attr3
43
What are methods
Methods are functions inside a class that operate on instances. self must always be the first parameter in instance methods. any additional parameters do not.
44
T or F: you must reference attributes and methods with self in a Class
False. you don’t need self for parameters because they exist only inside the method while it runs
45
Method
identifies the behaviors and actions that an object created from the class can perform with its data.
46
Classes
Defines a function and acts like a blueprint to create user-defined data structures.
47
Instance
An object that’s built from a class and contains real data.
48
Encapsulation
Allows you to bundle data (attributes) and behaviors (methods) within a class to create a cohesive unit.
49
Inheritance
enables the creation of hierarchical relationships between classes, allowing a subclass to inherit attributes and methods from a parent class.
50
Abstraction
focuses on hiding implementation details and exposing only the essential functionality of an object.
51
Polymorphism
allows you to treat objects of different types as instances of the same base type, as long as they implement a common interface or behavior.
52
__init__()
Sets the initial state of the object by assigning the values of the object’s properties. That is, .__init__() initializes each new instance of the class.
53
parameter self
the self parameter is passed in .__init__() so that Python can define the new attributes on the object.
54
instantiating
Creating a new object from a class
55
class attributes
attributes that have the same value for all class instances. You can define a class attribute by assigning a value to a variable name outside of .__init__(). class Dog: species = "Canis familiaris" def __init__(self, name, age): self.name = name self.age = age
56
Instance methods
functions that you define inside a class and can only call on an instance of that class.
57
Visualize an instance method
class Dog: species = "Canis familiaris" def __init__(self, name, age): self.name = name self.age = age # Instance method def description(self): return f"{self.name} is {self.age} years old" # Another instance method def speak(self, sound): return f"{self.name} says {sound}"
58
How do you use inheritance in Python
You inherit from a parent class by creating a new class and putting the name of the parent class into parentheses: class Parent: hair_color = "brown" class Child(Parent): pass
59
Visualize using super()
... class JackRussellTerrier(Dog): def speak(self, sound="Arf"): return super().speak(sound) ...
60
Visualize unpacking the items iterable inside an f-string (*items), separating the elements with a default space.
print("Items are: ", *items, sep="\n")
61
What is an instance variable?
Defined using self.variable_name Unique to each object Exists as long as the object exists
62
When do you use an instance variable?
✅ When each object needs to store unique data ✅ When the value needs to persist across multiple method calls ✅ When multiple methods in the class need to use the same data
63
When do you use a local variable in OOP?
✅ When you only need the value temporarily inside a method ✅ When the variable does not need to be used in other methods ✅ When you want to avoid storing unnecessary data in the object
64
What is a local variable in OOP?
It only exists inside the method where it is defined It does not persist between method calls It cannot be accessed outside the method
65
What is a class variable?
Defined outside __init__ but inside the class Same for every object of the class Accessed using ClassName.variable_name or self.variable_name
66
When to Use a Class Variable
✅ When you want to share data across all instances ✅ When you want a default value for all objects ✅ When the variable is not supposed to change per instance
67
What is traversing a list
Accessing each list element in sequence
68
Suppose you have a long list and want to know if it contains the value 22, how would you do it
count = 0 list = [x,x,x,x,22,xx,x,x,x] for element in numbers: if element == 22: count +=1
69
Visualize the logic of a tic-tac-toe board
board = [] if board[0][0] != " " and board[0][0] == board[0][1] == board[0][02]: print("Winner") else: print("No win")
70
visualize using the .keys method
Allows you to access the keys as an iterative sequence. dictionary = {"a": 1, "b":2, "c":3} store = dictionary.keys() print(store) OUTPUT: 1,2,3
71
abstract base class
You can do this using an **abstract base class**, which contains a method that **raises an error** if not implemented.
72
Dunder Methods
allow classes to have **built-in behaviors** similar to Python's native types. __len__() __del__()
73
Modules
a **Python file (`.py`)** that contains **functions, classes, or variables** that you can reuse in other scripts.
74
Packages
a **folder** that contains multiple modules and an **`__init__.py`** file
75
Subpackages
A package inside another package
76
__name__ = "__main_"
If a Python file is **imported as a module**, Python sets `__name__` to the module’s filename (without `.py`)
77
How is it useful?
Sometimes, you want a script to **run certain code only when executed directly**, but not when it’s imported. To do this, use an `if` statement to check whether `__name__ == "__main__"`.
78
Visualize how to use try-except
try: # Code that may cause an error result = 10 / 0 except: # Runs if an exception occurs print("An error occurred!")
79
What is try-except
- Prevents your program from **crashing** when an error occurs. - Allows you to **handle** errors gracefully. - Lets you **log, debug, or recover** from exceptions.
80
Visualize how to specify which error type to catch
try: num = int("hello") # Trying to convert a string to an integer except ValueError: print("Oops! That was not a valid number.")
81
__str__()
This method defines what gets printed when you use print(object). class Book: def __init__(self, title, author, pages): self.title = title self.author = author self.pages = pages def __str__(self): return f"{self.title} by {self.author}"
82
__len__()
Defines the behavior of len(object). class Book: def __init__(self, title, author, pages): self.title = title self.author = author self.pages = pages def __len__(self): return self.pages
83
Importing Modules from a Package
from MyMainPackage import someMainScript
84
Visualize importing a subpackage
from MyMainPackage.SubPackage import mySubScript
85
Visualize using else with try-except
try: num = int(input("Enter a number: ")) # User input except ValueError: print("Invalid input! Please enter a number.") else: print(f"Great! You entered {num}.") # Runs if no errors occur
86
finally block always runs whether an error occurs or not.
try: f = open("example.txt", "r") # Try opening a file except FileNotFoundError: print("File not found!") finally: print("Execution completed.") # Runs no matter what
87
Visualize how to keep prompting a user until getting correct output with try/except
while True: try: num = int(input("Enter a number: ")) break # Exit loop if input is valid except ValueError: print("Invalid input! Please enter a number.")
88
pylint and unitest
is Python's built-in testing framework, and it's great for writing test cases to validate your code.
89
Visualize passing a function to another function.
def greet(name): return f"Hello, {name}!" def function_caller(func, argument): return func(argument) print(function_caller(greet, "Alice")) # Output: Hello, Alice!
90
What is a python decorator? @new_decorator
A decorator is a special function that wraps another function to change its behavior without modifying the original function.
91
Visualize using a decorator
def order_pizza(): return "Here is your pizza! 🍕" def extra_cheese_decorator(original_function): def wrapper(): return original_function() + " + extra cheese 🧀" return wrapper @extra_cheese_decorator def order_pizza(): return "Here is your pizza! 🍕"
92
Generators
Generators are a special type of iterator that generate values on the fly instead of storing them in memory. This makes them more memory-efficient than regular lists.
93
yield
is what makes a function a generator. Instead of returning a value and ending the function, yield pauses the function and allows it to continue from where it left off when called again.
94
return vs. yield
return → Ends the function and returns a value once yield → Pauses the function and remembers its state, so it can keep producing values
95
Visualize using a generator
def gen_fibon(n): a, b = 1, 1 for _ in range(n): yield a # Instead of storing in a list, we "pause" and return a a, b = b, a + b # Update Fibonacci numbers # Example: Generating first 5 Fibonacci numbers fib = gen_fibon(5) print(list(fib)) # Output: [1, 1, 2, 3, 5]
96
How do you pull values one at a time in a generator?
With next() fib = gen_fbon(5) print(next(fib)) # 1
97
iterator
is an object that can be iterated one value at a time (like generators)
98
iter()
function turns an iterable into an iterator.
99
Visualize using iter() with next()
my_list = [10, 20, 30] my_iterator = iter(my_list) # Convert list to an iterator print(next(my_iterator)) # 10 print(next(my_iterator)) # 20 print(next(my_iterator)) # 30 print(next(my_iterator)) # ERROR! StopIteration
100
Visualize reading large files with generators
def read_file_generator(filename): with open(filename, "r") as file: for line in file: yield line # Yields one line at a time instead of storing everything lines = read_file_generator("big_data.txt") print(next(lines)) # Reads only the first line print(next(lines)) # Reads the next line
101
Opening a file
open("file.txt", access_mode)
102
T/F: You can open files that are already opened.
False. An error will occur, so use a try-except try: file_var = open("file.txt.", "r") except: print("Already opened.")
103
Visualize how to create a readline() method
file_var = open("file.txt.", "r") contents = [] line = file_var.readline() while line != " ": contents.append(line) line = file_var.readline()
104
visualize closing a file
file_var.close()
105
Visualize turning a text file that is seperated by commas into tables
file_var = open("file.txt", "r") artist_idx = 0 song_idx=1 rating_idx = 2 contents = file_var.read() split_by_line = contents.split(\n") split_by_comma = [x.split(",") for x in split_by_line if x != " "] print("Artist:", split_by_comma[0] [artist_idx]) print("Song:", split_by_comma[1] [song_idx]) print("Rating:", split_by_comma[2] [rating_idx])
106
Visualize using with and as to open and close files
with open("file.txt","r") as file_var: contents = file_var.read()
107
Visualize reading from one file and writing to a new file
with open("file.txt", "r") as input_file, open("fav_file.txt", "w") as output_file artist_idx = 0 song_idx=1 rating_idx = 2 contents = input_file.read() split_by_line = contents.split(\n") split_by_comma = [x.split(",") for x in split_by_line if x != " "] final_tab = [[line[artist_idx],line[song_idx], int(line[artist_idx]) for line in split_by_comma]] for record in final_tab: if record[rating_idx] >4: output_file.write(record[artist_idx] + "," + record[song_idx] + "," + str(record[rating_idx] + "\n"))
108
Recursion
the problem-solving approach that divides large problems into smaller, identical problems.
109
Recursive function
calls itself at least once. At each function call, recursive functions use modified parameters to shrink the size of the problem.
110
Questions to ask when using recursion?
Can this problem be broken down into smaller problems? Are the smaller problems identical to the larger problems? Is there a way to divide the problem into smaller subproblems that are easy to solve?
111
What is the difference def sum_num(current_num): if current_num == 0: return 0 else: sum_tot = sum_nums (current_num-1) return current_num + sum_tot sum_num(10) def sum_num(sum_tot, current_num): if current_num == 0: return 0 else: sum_tot = sum_tot + current_num return sum_num(csum_tot, current_num -1) sum_num(0,10)
Both sets of code sum the numbers from 10 to 1 . The tail recursive code passes the sum_total variable to the recursive function call instead of using a local variable. With this approach, the computer does not need to remember anything, so it does not use additional memory.
112
Q: What does \d match in regular expressions?
A: Any single digit from 0 to 9. Example: \d\d\d matches 123, 555, 202
113
Q: What does \w match in regular expressions?
A: Any single letter (a–z, A–Z) or digit (0–9). Example: \w\w\w matches abc, 313, Ax3
114
Q: What does [a-z] mean in regex?
A: A character set: matches any one character between a and z. Example: Ta[iy]lor matches Tailor, Taylor
115
Q: What does [0-9] mean in regex?
A: A digit set: matches any single number 0–9. Example: 1[028] matches 10, 12, 18
116
Q: What does | do in regex?
A: Logical OR — matches either the pattern on the left or right. Example: Ste(ph|f)en matches Stephen, Stefen
117
What does . mean in regex?
A: Matches any single character (letter, digit, or symbol). Example: fre. matches free, fred, fre#
118
Q: What does {n} mean in regex?
A: Matches exactly n repetitions of the preceding element. Example: \d{4} matches 1234, 0525, 2945
119
Q: What does {n,m} mean in regex?
A: Matches between n and m repetitions (inclusive). Example: \d{2,4} matches 12, 123, 6789
120
Q: What is a stack in programming?
A: A stack is a linear, limited-access data structure where the last element added is the first removed. It follows a LIFO (Last In, First Out) order.
121
Q: What are the main operations used with a stack?
push(): adds an element to the top pop(): removes the top element peek(): views the top element without removing it
122
Q: What is a queue in programming?
A: A queue is a linear, limited-access data structure where elements are removed from the front and added at the rear. It follows FIFO (First In, First Out) order.
123
Q: What are the main operations used with a queue?
enqueue(): adds an element to the rear dequeue(): removes the front element peek(): views the front element without removing it
124
.writerow()
a method from that little helper. It writes ONE row into your CSV file.
125
.DictWriter()
You can use csv.DictWriter() to handle dictionaries without having to write the values in the right order
126
Visualize using python methods to write fieldnames=["user_id", "steps", "workout_type", "duration"] to a csv
writer = csv.DictWriter(file, fieldnames=["user_id", "steps", "workout_type", "duration"]) writer.writeheader() # writes the column names as the first row writer.writerow(log) # writes one row of dictionary values