This flow control statement loops through a set of statements if a test statement continues to evaluate to True.
What is an "if" statement?
Loops are used to execute code repeatedly. What is another word for this?
What is iteration?
What do dictionary pairs consist of?
Keys and Values
What is the keyword that always precedes a function definition?
def
This operator "glues" two strings together. Give an example.
What is the concatenation (+) operator?
What does the += operation do?
Write an equivalent statement to: x += 4
It adds assigns the variable to its original value plus the value to the right of the operation.
x += 4 ---> x = x + 4
What is a list?
A list is a collection of values which are ordered and mutable.
How do we iterate through the keys of a dictionary? How about the values?
Use dict_name.keys() or dict_name to iterate through the keys, and dict_name.values() to iterate through the values.
What is the difference between a print statement and a return statement?
You use print statements to display something on the console, while you use return statements in functions to output and store the result of the function call in a variable.
This character denotes the floor division operator.
What is the double forward slash (//) ?
What does the != operator mean? Give an example using numbers and its output.
It means "not equal".
Ex: 5 != 4 (TRUE)
When you know the number (or steps) of times you want to iterate which type of loop should you use?
What is a for loop?
What is displayed on the console?
dict = {"a": 1, "b":2, "c":3, "d":4}
res = ""
for v in dict:
res += (v + str(dict[v]))
print(res)
a1b2c3d4
What is displayed on the console?
def mystery2(x):
return x%2
def mystery3(x):
return x%3
def mystery(x):
return mystery2(x) + mystery3(x)
print(mystery(100))
1
How do you generate a random number? What purpose does it have?
You use the random library - random.randint(start, stop)
You use it so that you can simulate real life where you won't know what values you will be given.
What is the result of int("2") ** 3 ?
What is 8?
What does it mean for lists to be mutable? Give one example of how you can mutate a list.
Mutable means that list values can be changed after they are created.
list_name.append(value)
list_name[index] = value
list1 += list2
list_name.pop()
What is stored in dict?
dict = {"a": 1, "b":2, "c":3, "d":4}
for x in "abcd":
dict[x] = dict[x] + 75%3 + 100%7
{'a': 3, 'b': 4, 'c': 5, 'd': 6}
What is displayed on the console?
def mystery(a):
mystery_val = 0
for i in range(len(a)):
mystery_val = a[i]*(i+1)
return mystery_val
print(mystery([10, 20, 30, 40, 50]))
250
How do you take in user input in Python. What do you do if the input is a numerical value?
Give a real life example of using a if - else if - else statement.
Any valid example.
What is the value of 'x' after the following Python code has completed executing?
x = 100
for n in range(1,5):
x = x*n
print (x)
What is 2400?
What is stored in dict?
dict = {"a": 1, "b":2, "c":3, "d":4}
dict["pineapple"] = 20
dict["a"] = dict["a"]*100 + dict["pineapple"]
for x in "SWE":
dict[x] = dict["b"]*dict["a"]
{'a': 120, 'b': 2, 'c': 3, 'd': 4, 'pineapple': 20, 'S': 240, 'W': 240, 'E': 240}
What is displayed on the console?
def mystery(x):
mystery_val = {}
count = 0
for i in x:
mystery_val[i] = count
count += 1
return mystery_val
print(mystery("SWE++"))
{'S': 0, 'W': 1, 'E': 2, '+': 4}
Use a loop to count the number of times an "o" appears in the word "voodoo".
word = "voodoo"
count = 0
for i in _______:
if ________:
count _______
print(count)
word = "voodoo"
count = 0
for i in word:
if i == "o":
count += 1