What type is True in Python?
Boolean
What's a parameter in a function?
A named input variable in the function signature.
What does this check?
if x == 5:
print("Yes")
Checks if x equals 5
Write a loop that prints every letter in "code".
for c in "code":
print(c)
if name = "Alex":
print("Hi")
Use == not = in the if
What does str(5) return?
"5"
True or False: All functions need at LEAST one parameter to compile.
False. Ex: greet()
What is the difference between = and ==?
= assigns; == compares
Write a function that returns a boolean for whether a number is even using modulo.
def is_even(n):
return n % 2 == 0
name == input('Name?')
print("Hi" + name)
name = input("Name?")
print("Hi", name)
==, +, ' '
What's the difference between a list and a string?
A list can be changed and holds items; a string is text that cannot be changed.
Write a function that takes a name and prints
"Hi, [name]!"
def welcome(name):
print(f"Hi, {name}!")
What statement would you use to chain multiple checks?
"elif"
Write code that prints only the even numbers from 1–10
for i in range(1, 11):
if i % 2 == 0:
print(i)
def greet:
print("Hello")
Need parentheses: def greet():
What will type(3.0+4) return?
<class 'float'>
What does this print?
def square(n):
for i in range(4):
n *= 2
return n
print(square(5))
80
Write an if/else block that prints “Even” or “Odd” based on a number.
if n % 2 == 0:
print("Even")
else:
print("Odd")
Write code that takes this list:
fruits = ["apple", "banana", "cherry"]
…and adds "orange" after "banana" using a list function.
fruits.insert(2, "orange")
print(fruits)
x = input("Enter a number: ")
print(x * 2)
input() returns str, so "3"*2 yields "33"—need int(x)
What will return from these lines of code?
datatype = ("abc", 34, True, 40, "male")
type(datatype)
<class 'tuple'>
Write a recursive function factorial(n) that returns the factorial of n.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
What does this print if x = 4 and y = 10?
if x > 3 and y < 10:
print("A")
elif x == 4 or y == 4:
print("B")
elif not x == 4:
print("C")
else:
print("D")
B
Write code to check if a user-entered password has at least 8 characters and includes the digit 1.
pwd = input("Pass:")
if len(pwd) >= 8 and "1" in pwd:
print("OK")
else:
print("Not OK")
nums = (1, 2, 3, 4, 5)
for n in nums:
if n % 2 == 0:
nums.remove(n)
print(nums)
nums is a tuple and CANNOT be changed.
nums = [1, 2, 3, 4, 5] is a list.