What does the % operator do?
Finds the remainder of an equation.
Ex: 107 % 10 = 7
What will be printed?
x = 10
while(x > 0):
print(x)
x = x - 2
10
8
6
4
2
How do you define a list in Python?
A list is defined using square brackets []. Elements are separated by commas.
Example:
myList = [1, 2, 3, 4, 5]
Fix the code so that from list it prints 12:
nums = [7, 12, 30]
print[2]
print(nums[1])
What will the following code print?
a = "231"
b = "369"
print(a + b)
231369
What will be printed?
x = 0
y = 10
while(y>=0):
print(x)
x = x + 1
y = y - 2
0
1
2
3
4
5
What will be printed by the following code?
fruits = ["apple", "banana", "cherry"]
print(fruits[1])
banana
Fix the loop so it prints “repeat” 5 times
x = 5
y = “repeat”
while(x>0):
print(y)
while(x>0):
print(y)
x = x - 1
What will be printed to the screen?
x = 3
y = 4
print(not(3 >= 4))
What will be printed?
text = ["hello", "wow", "cool", "bye"]
for word in text:
print(word+"!")
hello!
wow!
cool!
bye!
What does the following code output?
nums = [5, 10, 15]
nums.insert(1, 20)
nums.append(25)
nums.remove(10)
print(nums)
[5, 20, 15, 25]
Fix this code so the if statement doesn’t run an error:
y = 2
num = “true”
if(num):
print(y)
num = True
OR
if(num==”true”)
Find the output:
x = 5
x = x + 10
if(not(x / 5 == 3) and x % 5 + 4 < 10):
print("yay!")
No output
True or false: these two code segments print the same thing.
x = 0
while(x <= 5):
print(x * 2)
x = x + 1
y = 0
while(y <= 10):
if(y % 2 == 0):
print(y)
y = y + 1
True
What is wrong with this code? How do you fix it?
numbers = [1, 2, 3, 4, 5]
print(numbers[5])
The list index is out of range because Python lists are zero-indexed (i.e., valid indices are 0-4). Fix it by using print(numbers[4]).
Fix the code so that the code prints positive, negative, or 0 depending on an integer num:
if(num<0):
num = "negative"
elif(num>0):
num = "positive"
print(num)
No error
Create a function that takes a variable containing an integer and prints "even" or "odd" depending on its value. Here's the catch: you CANNOT use the % function!
Assume math is imported and math.floor() is available to use (hint hint)
def parity(num):
if((num/2)==(math.floor(num/2))):
print("even")
else:
print("odd")
Using loops, create a function that takes a list of integers and returns the number of times 2 appears in the list.
ex. For the list:
nums = [1,2,2,8,2,5]
your function should return 3.
def numTwo(numList):
x = 0
for num in numList:
if(num==2):
x = x + 1
return x
Write a Python function that takes a list of integers and returns a new list with only the positive numbers.
def pos(myList):
newList = []
for x in myList:
if(x>0):
newList.append(x)
return newList
I want “transcript” to be a dictionary with both my classes and my grades. Find a better way to do this, that can be applied to lists of different lengths.
classes = ["Biology","Algebra","English"]
grades = [86, 92, 79]
transcript = {}
transcript["Biology"] = 86
transcript["Algebra"] = 92
transcript["English"] = 79
transcript = {key:value for key, value in zip(classes, grades)}