Variables
Loops
Variable Functions
Code Snippets
Objects
100

The fundamental variables in python

int, float, string, list

100

How does the 'for' loop work?

It loops through all the items in an iterable

100

What does the split() function do to a string?

Returns an array of strings where the value passed into the split() function defines the separation of strings.
100
What does this code do?

print("Hello World!")

Prints Hello World! to the console

100

How do you declare a new object definition? Provide an example

class my_class():

    Class goes here

200

How do you select every other item in an array? (in one line)

array[::2]

200

How do you use the "while" loop?

Pass in a statement that will be true while you want it to repeat

200

How do you return the last item of a string?

string[-1] is prefered but string[len(string)-1] works too

200

What will this code print?

array = [1,2,3]

for item in array:

    print(item)

1

2

3

200

How do you create a class able to be given values during initialisation?

Use the __init__() function

300

What array-similar objects cannot store duplicate values?

sets and dictionaries

300

What does the break statement do in a loop

It exits the loop without executing any code remaining in the loop

300

What list function returns nothing and reverses a list?

array.reverse()

300

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

300

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

400

How do you add a custom object to an array?

array.append(object)

400

What does the continue statement do?

It skips any remaining code in the current iteration of the loop and continues to the next iteration

400

How do you sort an array of dual tuples by its first value?

array.sort()

400

What does this code print?

try:

    file = open('text.txt')

    print(file)

except:

    print("That file does not exist")

The contents of the file if the file exists and "That file does not exist if the file does not exist
400

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

500

How do you access a value in a dictionary using the key?

dictionary[key] will return it's value

500

How do you get the index and value of an iterable in a for loop?

Use enumerate() and unpack into index, value

500

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()

500

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')])

500

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

M
e
n
u