The fundamental variables in python
int, float, string, list
How does the 'for' loop work?
It loops through all the items in an iterable
What does the split() function do to a string?
print("Hello World!")
Prints Hello World! to the console
How do you declare a new object definition? Provide an example
class my_class():
Class goes here
How do you select every other item in an array? (in one line)
array[::2]
How do you use the "while" loop?
Pass in a statement that will be true while you want it to repeat
How do you return the last item of a string?
string[-1] is prefered but string[len(string)-1] works too
What will this code print?
array = [1,2,3]
for item in array:
print(item)
1
2
3
How do you create a class able to be given values during initialisation?
Use the __init__() function
What array-similar objects cannot store duplicate values?
sets and dictionaries
What does the break statement do in a loop
It exits the loop without executing any code remaining in the loop
What list function returns nothing and reverses a list?
array.reverse()
What will this code print?
my_set = set()
for i in range(10):
my_set.add(i//2)
for item in set:
print(item)
1, 2, 3, 4, and 5 each printed only once in any order
What does this class do on initialisation?
class my_class():
def __init__(self, value):
self.value = value
print(self.value)
Assigns an object variable the passed in value and prints it
How do you add a custom object to an array?
array.append(object)
What does the continue statement do?
It skips any remaining code in the current iteration of the loop and continues to the next iteration
How do you sort an array of dual tuples by its first value?
array.sort()
What does this code print?
try:
file = open('text.txt')
print(file)
except:
print("That file does not exist")
What does this class do on calling its update() function?
class my_class():
def __init(self):
self.i = 0
def update(self):
self.i += 1
print(self.i)
Increases its i value by one and prints it
How do you access a value in a dictionary using the key?
dictionary[key] will return it's value
How do you get the index and value of an iterable in a for loop?
Use enumerate() and unpack into index, value
What function do you call to return the value inside a file object assuming it has been opened in read and text mode?
file.read()
What does this code print?
dictionary = {'test':True, 'cat':'good', 'dog':'also good'}
print(dictionary.items())
dict_items([('test', True), ('cat', 'good'), ('dog', 'also good')])
What does this class do when its stringify() function is called?
class my_class():
def __init__(self, array):
self.array = array
def stringify(self):
return f'a{",".join(self.array)}'
It returns string versions of all objects in an array separated by a comma