Basic Python Flashcards
Are Python lists mutable (can they be changed after creation)?
Yes
Is everything in Python an Object?
Yes
Do Python Collection indexes start at 0?
Yes
Is Python case sensitive?
Yes
How do you do a 1 line comment in Python?
Use the ‘#’ symbol either at start of line, or inline
Can you have more than one statement on a line in Python?
Yes. Use the semi-colon to separate statements
What does dynamic typing mean?
Variable names can point to objects of any type e.g.
x = 1 .. x is integer
x = ‘hello’ … x is string
x = [1, 2, 3] … x is list
In Python are variables containers or pointers?
Pointers e.g. x = 4, the variable x is a pointer that points to some other bucket containing 4
Give a comparison of a mutable type v an immutable type assignment
x = [1, 2, 3] #List a = 1 # int
y = x # y is a List {1, 2, 3] b = a # b is an int 1
x.append(4)
print(y) … y = [1, 2, 3, 4] # because Lists are mutable, variable y is still a pointer to the same bucket that x points to, and so y’s value is updated
a = 2
print (b) … b = 1 # because ints are immutable, and so the assignment of a =2, forces a to become pointer to a new bucket. The previous bucket containing 1 cannot be updated, and so because b points there, the value of b still = 1
Is a Dictionary mutable?
Yes
Is a Set mutable?
Yes
Is a String mutable?
No, it is immutable
Is a Tuple mutable?
No, it is immutable e.g.
t = (1, 2, 3)
t[1] = 4 # not valid
t.append(4) # not valid
Is an Int or Float mutable?
No, there are immutable
What are the standard Python Boolean operators?
and, or , not
What are identity operators?
is, is not … used to identify identical objects e.g.
a = [1, 2, 3] b = [1, 2, 3]
a == b # True
a is b # False … a and b are not the same objects even though they contain the same value.
a is b returns True if we do …
a = [1, 2, 3] b = a
What are membership operators?
Membership operators check for membership within compound objects e.g.
1 in [1, 2, 3] #True
2 not in [1, 2, 3] # False
What is deep and shallow copying?
In Python assignment statements do not copy objects they create bindings between a target and an object. With mutable objects sometimes users want to create ‘real’ copies of the object, so that they can be changed without affecting the original.
Deep Copying ( copy.deepcopy(object) ) … a copy of the object is copied to another object. Any changes made to the copy do not reflect in the original object. Slower than shallow copy.
Shallow Copying ( copy.copy(object) ) … a copy of the object is made but it is populated with references to the child object, rather than actual copies. Shallow copying is also what happens when using factory functions e.g. new_list = list(orig_list).
What terminates a statement in Python?
The end of the line
How can you continue a statement over 2 lines in Python?
Use the ‘' marker or enclose statement within brackets e.g.
x = (1 + 2 + 3 + 4 +
5 + 6 + 7 + 8) # this second type is recommended
What is indentation used for in Python?
To denote code blocks
What symbol is used to denote an indented code block?
A colon
How can you check the type of a Python object?
type(x)
What is exponential notation?
Where numbers are stored used the exponential format e.g. 5e-6, which equals 0.000005 or 1.4e6 which equals 1400000.00
What is one method of concatenating strings?
string1 + string2
How can you access individual elements of a string?
string1[x] where x is an element index. Strings are Lists, and so can be iterated through.
What is the None type?
None type is a special Python type which has a single possible value - None.
Used as the default return type for functions e.g.
return_value = print(‘abc’)
print(return_value)
… value returned is None
Not to be confused with NaN # a Numpy/Pandas construct
Are Lists ordered?
Yes
How do you append values to a list?
list1 = [1, 2, 3]
list1.append(4)
Can also concatenate to a list via …
list1 + [4, 5, 6]
What is a compound object?
List, Dictionary, Tuple, Set
Can compound objects e.g. Lists contain objects of mixed type?
Yes e.g.
list1 = [1, ‘hello’, 3.4, [‘another’, ‘list’]]
Does python used zero-based indexing?
Yes e.g.
string1[0] returns the first element of the string object
How do you access the last element of a list?
list1[-1]
What is the difference between indexing and slicing for compound objects?
Indexing is a means of fetching a single value from the list, slicing is a means of accessing multiple values in sub-lists
How can you traverse a list backwards?
a[::-1]