Advanced Python Flashcards
\d
matches a digit (0-9)
?
A: The preceding character or group is optional (0 or 1 time).
Example: Breon?a matches Breona, Breoa
Q: What does + mean in regex?
A: The preceding character or group must occur 1 or more times.
Example: fre+ matches fre, free, freeee
Q: What does * mean in regex?
A: The preceding character or group can occur 0 or more times.
Example: fre* matches fr, fre, free, freeee
[]
matches any character inside of the brackets
Visualize tuple unpacking
you can use tuple unpacking
mylist = [(1,2),(3,4),(5,6),(7,8)]
for item in mylist:
print(item)
Visualize printing out the items of a dictionary
d = {‘k1’:1, ‘k2’:2,’K3’:3}
# for default you iterate through the keys
for item in d:
print(item)
What does this print?
d = {‘k1’:1, ‘k2’:2,’K3’:3}
for item in d.items():
print(item)
(‘k1’, 1)
(‘k2’, 2)
(‘K3’, 3)
What is the impact of continue?
for letter in mystring:
if letter == ‘a’:
continue #go back to the loop
print(letter)
It continues pass a, and prints the next characters
Smmy
Enumerate()
allows you to iterate over an iterable (like a list, string, or tuple) while keeping track of the index and value of each element.
What is the output?
words = [‘apple’, ‘banana’, ‘cherry’]
for index, value in enumerate(words):
print(f”Index: {index}, Value: {value}”)
Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: cherry
What is the output?
words = [‘apple’, ‘banana’, ‘cherry’]
indexed_words = list(enumerate(words))
print(indexed_words)
[(0, ‘apple’), (1, ‘banana’), (2, ‘cherry’)]
What is the output?
index_count = 0
for letter in “abcde”:
print(‘At index {} the letter is {}.’.format(index_count,letter))
index_count +=1
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.
zip()
Combines multiple iterables (like lists, tuples, or strings) element-wise into pairs (tuples
What is the output of this?
names = [“Alice”, “Bob”, “Charlie”]
ages = [25, 30, 35]
zipped = zip(names, ages)
print(list(zipped))
[(‘Alice’, 25), (‘Bob’, 30), (‘Charlie’, 35)]
What is the output of this?
for name, age in zip(names, ages):
print(f”{name} is {age} years old.”)
Alice is 25 years old.
Bob is 30 years old.
Charlie is 35 years old.
How would you unzip this?
zipped_data = [(‘Alice’, 25), (‘Bob’, 30), (‘Charlie’, 35)]
names, ages = zip(*zipped_data)
print(list(names)) # [‘Alice’, ‘Bob’, ‘Charlie’]
print(list(ages)) # [25, 30, 35]
List Comprehensions
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]
Use a list comprehension to: Create a list of squares of numbers from 1 to 5.
squares = [x**2 for x in range(1, 6)]
print(squares) # Output: [1, 4, 9, 16, 25]
Use a list comprehension to generate a list of all combinations of numbers and letters:
combinations = [(x, y) for x in range(2) for y in ‘AB’]
Use a function with tuple unpacking to increase the stock prices by 10 percent.
stock_prices = [(‘APPL’, 200), (‘GOOG’,400), (‘MSFT’, 100)]
for ticker,price in stock_prices:
print(price+(0.1*price))
*args
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
**kwargs
**kwargs returns a dictionary.
Visualize how to use *args
def myfunc(*args): #the user can pass in as many arguments as wanted
return sum(args) * 0.05
myfunc(40,60,100,1,34)