What does the % operator do? Give an example
Finds the remainder of an equation. Eg: 107 % 10 → 7
Are the following expressions true or false? (answer individually)
num < 2 && num == 5
num < 2 || num > 2
False true
How do you define a list in Python?
Answer:
A list is defined using square brackets []. Elements are separated by commas.
Example:
myList = [1, 2, 3, 4, 5]
What will be printed?
for i in range(1,3):
print(i*2)
2 4
Fix the code so that from menu it prints 12:
menu = [[“pasta”, 12], [“steak”, 16]]
print[1[2]]
print(menu[0][1])
What will the following code print?
a = "231"
b = "369"
print(a + b)
231369
What will be printed in the following piece of code? (Pay close attention)
a = 6969
b = 6970
if b > a:
print(‘a is greater than b’)
if b < a:
print(b is greater than a’)
a is greater than b
What will be printed by the following code?
fruits = ["apple", "banana", "cherry"]
print(fruits[1])
Banana
What will be printed?
x = 0
y = 10
while(y>=0):
print(x)
x+=1
y-=2
0 1 2 3 4 5
Fix the loop so it prints “repeat” 5 times
x = 5
y = “repeat”
while(x>0):
print(y)
print(y)
x-=1
What will be printed to the screen?
x = 3
y = 4
print(not(3 >= 4));
True
What will be printed in the following piece of code?
number = 0
number = -2
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
Negative number
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]
What will be printed?
nums = [3,6,17,8,44]
for num in nums:
print(num+1)
4 7 18 9 45
Fix this code so the if statement doesn’t run an error:
y = 2
num = “true”
if(num):
print(y)
ans: num = True
OR
if(num==”true”)
Find the output:
x = 5
x += 10
if(not(x / 5 == 3) and x % 5 + 4 < 10):
print("yay!")
(No output)
Write a piece of code that sets integer "num" to 3. then write an if/else statement that states if num is an even number print "even" and if num is an odd number print "odd."
Then answer: what will print?
if (num%2==0):
print('Even')
Else:
print ('Odd')
Odd is printed.
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]).
True or false: these code segments will print the same thing
for i in range(10):
print(i*2)
for i in range(10):
if(i%2==0):
print(i)
False
Fix the code so that the code prints positive, negative, or 0 depending on num
num = -16;
if(num<0):
num = "negative"
elif(num>0):
num = "positive"
print(num)
No error
Write code that gives the variable x a value of a random number through -5 to 5 (integers only). Make another variable y and give it the value of a random number through -10 to 10 (integers only). Then, write an if statement asking if x is greater than y, print “x is greater than y.” Else, print “y is less than or equal to x”. Assume the random module is already imported. Syntax hint: Use random.randint(a, b)
x = random.randint(-5, 5)
y = random.randint(-10, 10)
if(x > y):
print("x is greater than y")
else:
print("y is less than or equal to x")
Write a piece of code that sets integer "myAge" to 17. Write if/else statements that states if myAge is greater or equal to 18, print "Adult." If myAge is between 0 and 18 print "Minor." Otherwise, print "Not real age."
(Hint: use elif and else)
Then answer: what will print?
myAge = 17
if myAge >= 18:
print(‘Adult’)
elif (myAge < 18) and (myAge > 0):
print(‘Minor’)
else:
print(‘Not real age’)
Minor is printed
Write a Python function that takes a list of numbers and returns a new list with only the even numbers.
def evens(myList):
return [num for num in myList if num % 2 == 0]
# Example:
print(evens([1, 2, 3, 4, 5, 6])) # Output: [2, 4, 6]
Create a loop that prints out the number of times 2 appears in nums:
nums = [1,2,2,8,2,5]
x = 0
–your code here–
print(x)
for n in nums:
if n==2:
x+=1
I want “transcript” to be a nested list with both my classes and my grades. Find a better way to do this, that can be applied to lists of different lengths. (Hint: loops!)
classes = ["Biology","Algebra","English"]
grades = [86, 92, 79]
transcript = []
transcript.append([“Biology”,86])
transcript.append([“Algebra”,92])
transcript.append([“English”,79])
for x in classes:
transcript.append([x,grades[classes.index(x)]])