What’s the difference between a list and a tuple?
Lists are mutable, tuples are not.
What is the purpose of the init() function in a class?
Initializes new object and its attributes.
What keyword starts a function definition in Python?
"def"
What are the four most common data types?
String, Int, Boolean, Float
Use a for loop to print each item in this list:
["apple", "banana", "cherry"]
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Write a list that holds three values with three different data types
[<string>, <integer>, <float>, <boolean>]
In this line, which is the class and which is the object? truck = Vehicle("UHaul")
Vehicle is class; truck is object.
What is a parameter?
A variable used inside a function to accept input
What keyword is used to check a condition?
"if"
Write a function that takes a list of numbers and prints each one doubled using a loop.
def double_numbers(nums):
for num in nums:
print(num * 2)
How do you access the last element in a list called ordered_list?
ordered_list[-1]
What will this function return?
def mystery(x):
if x % 2 == 0: return x // 2
else: return x * 3
mystery(5)
15
DAILY DOUBLE: What does this return?
!(5 > 2 and 3 > 1)
False
Given a tuple of fruits, use a for loop to print only the fruits that start with the letter "b".
fruits = ("banana", "apple", "blueberry", "orange")
for fruit in fruits:
if fruit.startswith("b"):
print(fruit)
What does this code print?
nums = [3, 8, 5]
nums[1] = 5
print(nums)
[3, 5, 5]
Write a method inside a class that returns the 4th power of an attribute called side. Using the exponent operator.
def area(self):
return self.side ** 4
What does this code print and why?
def f(x): x += 1
return x
y = 27
f(y)
print(y)
27 (integers are immutable; x is a copy)
Name 3 boolean logical operators
AND (&&), OR (||), NOT (!)
DAILY DOUBLE: Define a class Book with a title and pages. Create a list of 3 books and use a loop to print the titles of books with more than 100 pages.
class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages
books = [
Book("Book A", 120),
Book("Book B", 95),
Book("Book C", 200)
]
for book in books:
if book.pages > 100:
print(book.title)
DAILY DOUBLE: What will this print?
nums = [37, 20, 30, 50]
nums.insert(2, 15)
nums.pop()
print(nums)
[37, 20, 15, 30]
Inserts 15 at index 2 -- in between 20 and 30.
pop() will remove the last element(50)
Create a class Triangle that stores base and height and has a method area() returning the area.
class Rectangle:
self.base = base
self.height = height
def area(self): return self.base * self.height * 1/2
What does this return?
def func(x):
if x > 0: return x + func(x - 1)
else: return 0
print(func(5))
15 --- (5+4+3+2+1+0)
How would I get the data type of the variable mystery?
type(mystery)
Write a function that takes a list of names and returns a new list containing only the names that are longer than 4 letters.
def long_names(names):
result = []
for name in names:
if len(name) > 4:
result.append(name)
return result