1What is a major difference between Python 2 and 3?
In python 2, print is not a function and is written as,
print "text"
2Tuple
Created with (), immutable, ordered list
3List
Created with [], mutable
4What is the syntax for slicing?
list[begin: end: skip] = list[1: 5: 2]
5How does list slicing work?
Begin: Index started at
End: Index ended at, this index is not included in the slice
Skip: Index to skip up to, using 2 would skip the next one and include the one after
6Strings are immutable, what does this mean?
They cannot be changed, modifications to string create entirely new strings.
The old one is automatically destroyed by python.
7Which objects are mutable?
Dictionaries, lists
8Which objects are immutable?
ints, floats, tuples, strings
9Special methods include
__getitem__()
__len__()
10For loop syntax
for i in list:
print(i)
11flush()
Write to the file immediately, do not buffer
13The with statement,
Opens a file without needing to close it
14With statement syntax
with open("output.dat", "w") as f:
for x in dictsList:
print("${:<10.2f} ${:<10.2f} ${:<10.2f} ${:<10.2f}".format(x["SubTotal"], x["Tax"], x["Tip"], x["Total"]), file = f)
15Function syntax
def multiplyByTwo(num):
'''
Documentation goes here!
'''
return num * 2
16Lambda or anonymous function syntax
is_2020 = lambda x: x["Year"] == "2020"
17What is try / except
Code will attempt to run, and if an error is ran into, the error is handeled in the except block
18Raise syntax
if x < 0
raise Exception("No numbers less than 0")
19What will i and entry do here?
for i, entry in enumerate(list):
i will act as a counter for how many iterations of the loop have been ran.
entry is the data for that specific index (i)
20What will i do here?
I = iter([1,2,3,4])
Create an iterator that allows you to iterate through the list.
You can use next to make it go from 1 to 2.
21What does this do?
L= [1, 2, 3]
R= ['a', 'b', 'c']
for lval, rval in zip(L,R):
print(lval, rval)
Iterates over multiple iterables in parallel.
22What does map do here?
square = lambda x: x ** 2
for val in map(square, range(10)):
print(val, end=' ')
Applys a function to every element in the iterable.
23What does filter do here?
is_even = lambda x: x % 2 == 0
for val in filter(is_even, range(10)):
print(val, end=' ')
Applys a function to every element in an iterable, but returns true or false.
24Is the range function an iterable? How about an iterator?
Yes.
No.
25What list will
L = [n ** 2 for n in range(6)]
create?
[0, 1, 4, 9, 16, 25]
26Syntax for a conditional list comphrehension:
[val for val in range(20) if val % 3 > 0]
27What is a generator?
An iterable object that produces values in sequence.
The entire object is not stored.
28Generator syntax:
G = (n**2 for n in range(10))
29What does yield do?
Allows you to continue where you left off in a function, replaces return.
30What is the difference between the split and strip functions?
Strip removes a substring or specified characters from the beginning or end of an input string.
Split breaks the string based on a specified delimiter.