Print the integers from 0 - 10 using a for
loop
Print the integers from 0 - 10 using a while
loop
Write a function that takes x
as input and prints it
Write a function that takes x
and prints every number between 0 and x-1
using a for
loop
Write a function that takes x
and prints every number between 0 and x-1
using a while
loop
Use code to verify that 4 is even
Write a function that takes x
and returns True
if x
is even
Write a function that takes x
and prints every number between 0 and x-1
using a for
loop
Write a function that takes x
and prints every EVEN number between 0 and x-1
using a for
loop
Write a function that takes x
and prints every EVEN number between 0 and x-1
using a while
loop
Using while True:
, print the integers from 0 - 10
Write a function that takes x
and prints every EVEN number between 0 and x-1
using while True:
Write a function that takes x
and prints every EVEN number between 0 and x-1
using a for
loop, without control flow (i.e. no if
statements)
Create a list called my_list
with 3 integers
Print the first element in my_list
Print the length of my_list
Print every number in my_list
using a for x in y
loop
Write a function that takes a list and prints all items in it using a for x in y
loop
Print the sum of all the elements in my_list
using a for x in y
loop
Write a function that takes a list and prints the sum of all items in it using a for x in y
loop
Write a function that takes a list and prints the sum of all items in it using a while
loop
Write a function that takes a list and prints the sum of all items in it that does NOT use a for x in y
loop or a while
loop. (Can you think of a different kind of for
loop?)
Create a string called my_string
with the value "racecar"
Print the first character of my_string
Print the last character of my_string
using len
Print True
if the first and last characters of my_string
are the same
Write a function that takes a string and returns True
if the first and last characters of the string are the same and False
otherwise
Write a function that takes a string and returns True
if the second and second to last characters of the string are the same and False
otherwise
Note: A palindrome is a word that is spelt the same forward as it is backwards.
For each of the following strings, say whether they are palindromes (in words):
Fill this in.
Write a function that takes a string and returns True
if the string is a palindrome and False
otherwise
def isPalindrome(str1):
# Fill in your code here!
# Test your code here.
print(isPalindrome('noon'))
print(isPalindrome('Hello'))
print(isPalindrome('kayak'))
print(isPalindrome('123123'))
def isPalindrome(string):
# Fill in your code here!
# Test your code here.
isPalindrome('aza')
Run the code below to print the numbers from 0 - 20
Add to the code in the block above (without deleting anything) to break out of the loop after printing the numbers 0 - 5
Now, use the code from 5.1 to print all the numbers between 0 and 20 that are not multiples of 3.
Write code that will print all the numbers between 0 and 20 that are not multiples of 3 using continue
Try running the code below and see what happens (hint: you might need to use the stop button at the top of the page).
i = 0
while True:
i += 1
print(i)
Now, edit the code above to only print the numbers up to 20, using break
.
# Copy in the code from 5.5 and edit it.
Now, using continue
, use the same code from 5.6 to print the numbers up to 20 that are not multiples of 3.
# Copy in the code from 5.6 and edit it.
Write a program that adds integers from 1 to 20 (inclusive) until sum is greater than or equal to 100.
Write a program that adds all the integers from 1 to 20 except 10 and 11.
Write a program that displays the first fifty prime numbers.
Output: FILL IN YOUR ANSWER HERE
What is the value of i
after the above code is executed?
# FILL IN YOUR ANSWER HERE
Output: FILL IN YOUR ANSWER HERE
What is the value of i
after the above code is executed?
FILL IN YOUR ANSWER HERE
What will j
be when printed in the code below?
def func1(x):
j = 10
print(j, x)
def func2(x):
j = 5
func1(x)
print(x)
func2(3)
print(j)
FILL IN YOUR ANSWER HERE
Now, we are going to create a FortuneTeller
function that will print out a fortune depending on the value of the input given. Use the code comments below to help you complete this task.
# Let's pretend we are taking in user input. Make a variable `input` with the value 1.
# Function header that creates a function called `FortuneTeller` that takes no arguments.
def FortuneTeller():
# Add a local variable to `FortuneTeller` called `input` that has the value 3.
# Add a conditional that checks when input is equal to 1. Print out "You will be lucky" if input equals 1.
# Add a conditional that checks when input is equal to 2. Print out "You will be unlucky" if input equals 2.
# Add a conditional that checks when input is equal to 3. Print out "You will get money" if input equals 3.
# Add a conditional that checks when input is equal to 4. Print out "You will lose money" if input equals 4.
# Add a conditional that checks when input is equal to 5. Print out your own statement if input equals 5.
# Call the FortuneTeller function and observe the output value
What value did you get when you ran the code above? Was it the value you expected?
FILL IN YOUR ANSWER HERE
How can you change the code in 6.4 so that the output given is "You will lose money"?
# FILL IN YOUR ANSWER HERE
In the code block below, what will be the output? Was it what you expected?
i = 100
for i in range(3):
print(i)
FILL IN YOUR ANSWER HERE
Will (a) and (b) output the same thing? If not, explain why.
(a)
k = 0
for i in range(3):
print(k)
(b)
k = 0
for i in range(3):
k = 6
print(k)
# FILL IN YOUR ANSWERS HERE