What are the four most common data types?
String, Int, Boolean, Float
What’s the difference between a list and a tuple?
Lists are mutable, tuples are not.
What keyword starts a function definition in Python?
"def"
What is the purpose of the init() function in a class?
Initializes new object and its attributes.
my_list = [1, 2, 3]; my_list.pop(3)
Index 3 out of range (valid 0–2).
What keyword is used to check a condition?
"if"
Write a list with three fruits
["apple", "banana", "cherry"]
What is a parameter?
A variable used inside a function to accept input
In this line, which is the class and which is the object? player1 = Gamer("Alex")
Gamer is class; player1 is object.
print("My age is " + 15)
Can't concatenate str and int.
What does this return?
5 > 2 and 3 < 1
False
How do you access the first element in a list called items?
items[0]
What will this function return?
def mystery(x):
if x % 2 == 0: return x // 2
else: return x * 3
mystery(4)
2
numbers = [1, 2, 3]
numbers.append = 4
You’re trying to assign a value to append, which is a built-in method.
Name 3 boolean comparison operators
==, !=, >=, <, <=, >
What does this code print?
nums = [1, 2, 3]
nums[1] = 5
print(nums)
[1, 5, 3]
What does this code print and why?
def f(x): x += 1
return x
y = 5
f(y)
print(y)
5 (integers are immutable; x is a copy)
Write a method inside a class that returns the square of an attribute called side.
def area(self):
return self.side ** 2
x = input("Enter a number: ")
if x > 10:
print("Big number!")
input() returns a string, so comparing it to an integer causes an error.
What built-in function do you use to get the data type of a variable?
type()
DAILY DOUBLE: What will this print?
nums = [10, 20, 30]
nums.insert(1, 15)
nums.pop()
print(nums)
[10, 15, 20]
Inserts 15 at index 1 -- in between 10 and 20.
pop() will remove the last element(30)
What does this return?
def func(x):
if x > 0: return x + func(x - 1)
else: return 0
print(func(2))
3 --- (2+1+0)
Create a class Rectangle that stores width and height and has a method area() returning the area.
class Rectangle:
self.width = width
self.height = height
def area(self): return self.width * self.height
count = 0
def increase():
count += 1
print(count)
increase()
count cannot be accessed within the function, it has not been defined locally.
use global count to do so