Operators/Variables
Loops
Lists
Debug
100

What does the % operator do?

Finds the remainder of an equation. 

Ex: 107 % 10 = 7 

100

What will be printed?

x = 10

while(x > 0):

 print(x)

 x = x - 2

10
8
6
4
2

100

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]

100

Fix the code so that from list it prints 12:


nums = [7, 12, 30]

print[2]

print(nums[1])

200

What will the following code print?

a = "231"

b = "369"

print(a + b)

231369

200

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

200

What will be printed by the following code?

fruits = ["apple", "banana", "cherry"]

print(fruits[1])

banana

200

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

300

What will be printed to the screen?

x = 3

y = 4

print(not(3 >= 4))

True
300

What will be printed?

text = ["hello", "wow", "cool", "bye"]

for word in text:

  print(word+"!")


hello!
wow!
cool!
bye!

300

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]

300

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

400

Find the output:

x = 5

x = x + 10

if(not(x / 5 == 3) and x % 5 + 4 < 10):

    print("yay!")

No output

400

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

400

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

400

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

500

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


500

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

500

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

500

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