What type is the value 10?
int
What type is 3.14?
float
What type is "hello"?
str
What type is True?
bool
What type is [1, 2, 3]?
list
What type is (1, 2)?
tuple
What type is {"a": 1}?
dict
How do I get the type of a variable x in python?
type(x)
We have a variable x="7". Convert x to an integer.
x = int(x)
What is the value of the unsigned binary string 1011?
1*8 + 0*4 + 1*2 + 1*1 = 11
What range of values can a 32-bit signed integer represent?
1 bit for sign and 31 bits for the number, so $\pm 2^{31}$
What is the result of 1/2/2?
1/8
What is the result of 8/2*3?
12
What is the result of 7 % 2?
1
How do you raise x to the 3rd power?
x**3
give an expression using base python that converts a float x to the nearest integer by rounding (not truncation)
y = int(x + 0.5)
7 ("# 5" is a comment)
If x is a float and y is an int, what type is x*y?
float
What is overflow in computer arithmetic? What happens in that case?
Overflow is when a value is too large to be represented by the precision of the variable. You get an error/warning and (if code continues) the value wraps around to be small or negative due to truncation of the top bit(s).
What is underflow in computer arithmetic? What happens in that case?
When a value is too small to be represented by the precision of the variable. It gets approximated by zero.
Write an if statement that prints "Yes" if x is greater than 5.
if x > 5:
print("Yes")
Write an if-else that prints "Positive" if x > 0, otherwise "Not positive".
if x > 0:
print("Positive")
else:
print("Not positive")
How do you test whether x is equal to 10?
x == 10
What is the result of True and False?
False
What is the result of True or False?
True
Write an if statement that checks if x is zero.
if x == 0:
print("Zero")
What symbol is used for “not equal”?
!=
Write a condition that checks if x is less than or equal to 10.
x <= 10
What is the result of 5 > 3?
True
What is the result of 5 < 3?
False
why doesn't == work well for floats and how can this be addressed?
Finite-precision in computers means a float computation cannot be guaranteed to be exactly equal to desired value. Address it by checking for approximate equality such as with a function like isclose()
Write a test that checks whether variables x and y are within $10^5$ of each other and prints True if so
if abs(x-y) <= 1e-5:
print(True)
Define a function hello() that prints "Hi".
def hello():
print("Hi")
Write a function add(a, b) that returns their sum.
def add(a, b):
return a + b
Write a function that takes input x and returns x * 3.
def triple(x):
return x * 3
Write a function that takes input x and returns x * 3, which defaults to returning 3 when there is no input given.
def triple(x=1):
return x * 3
What is returned if a function has no return statement?
It returns None.
Write a function that takes no arguments and returns 10.
def give_ten():
return 10
What does the following code print?
def make_it_zero():
x = 0
print(x) # print x
x = 5
make_zero()
print(x) # print x
0
5
Write a for loop that prints numbers from 0 to 2.
for i in range(3):
print(i)
What does range(4) produce?
0, 1, 2, 3
Write a while loop that prints numbers 1 to 3.
i = 1
while i <= 3:
print(i)
i += 1
What happens if a while condition never becomes False?
It creates an infinite loop.
Write a for loop that prints "Hello" twice.
for i in range(2):
print("Hello")
What keyword stops a loop immediately?
break
What keyword skips to the next iteration?
continue
Write a for loop that prints each item in [1, 2, 3].
for x in [1, 2, 3]:
print(x)
What does range(1, 4) produce?
1, 2, 3
Write a while loop that prints every number from zero to N.
x = 0
while x<N+1:
print(x)
x = x+1
Create a list containing 4, 5, and 6.
my_list = [4, 5, 6]
Change the second element of my_list to zero.
my_list[1] = 0
What does len(my_list) return? where my_list = [4, 5, 6]
The number of elements in the list, in this case 3.
Add 7 to the end of my_list (making the lost longer by one element).
my_list.append(7)
Remove the last element.
my_list.pop()
What does [1, 2] + [3,4] produce?
[1, 2, 3, 4]
Write a loop to print all elements in my_list.
for x in my_list:
print(x)
Extract the elements from index i to index j (inclusive) from my_list into a new list
newlist = my_list[i:j+1]
Extract the first N elements from my_list into a new list
newlist = my_list[:N]
Extract the last element from my_list
Create two new lists from my_list, one containing only the even-numbered elements (i.e., indexes 0,2,4,...) and the other containing the odd-numbered elements.
evens = my_list[::2]
odds = my_list[1::2]
Create a string s containing the word "Code" in python
s = "Code"
What does len("Code") return?
4
Access the first character of s.
s[0]
What does "Hi" + "There" produce?
"HiThere"
How do I convert a string s to all uppercase?
S = s.upper()
How do I convert a string S to all lowercase?
?
s = S.lower()
Write a loop to print each character in s.
for c in s:
print(c)
What does "hello"[1] return?
"e"
What does "test" * 2 produce?
"testtest"
An error. Strings are not mutable.
You are given the string "apple;banana;cherry;date". Write code that creates a list of the fruit names.
s = "apple;banana;cherry;date"
fruits = s.split(";")
You are given the list ["2026", "02", "17"]. Write code that constructs the date string "2026/02/17".
parts = ["2026", "02", "17"]
date_string = "/".join(parts)
You are given the string "The experiment failed due to error code 404."
Write code that finds the position where "404" first appears in the original string.
s = "The experiment failed due to error code 404."
position = s.find("404")
You are given a string s such as s = "I like Java programming."
Write code that converts all occurences of "Java" to "Python" using string functions.
s = "I like Java programming."
new_string = s.replace("Java", "Python")