Flashcards Index¶

  • Python Types
  • Expressions
  • Conditional Statements
  • Functions
  • Loops
  • Lists
  • Strings
  • String Functions

Python Types¶

Question 1¶

What type is the value 10?

Answer 1¶

int

Question 2¶

What type is 3.14?

Answer 2¶

float

Question 3¶

What type is "hello"?

Answer 3¶

str

Question 4¶

What type is True?

Answer 4¶

bool

Question 5¶

What type is [1, 2, 3]?

Answer 5¶

list

Question 6¶

What type is (1, 2)?

Answer 6¶

tuple

Question 7¶

What type is {"a": 1}?

Answer 7¶

dict

Question 8¶

How do I get the type of a variable x in python?

Answer 8¶

type(x)

Question 9¶

We have a variable x="7". Convert x to an integer.

Answer 9¶

x = int(x)

Question 10¶

What is the value of the unsigned binary string 1011?

Answer 10¶

1*8 + 0*4 + 1*2 + 1*1 = 11

Question 11¶

What range of values can a 32-bit signed integer represent?

Answer 11¶

1 bit for sign and 31 bits for the number, so $\pm 2^{31}$

Expressions¶

Question 1¶

What is the result of 1/2/2?

Answer 1¶

1/8

Question 2¶

What is the result of 8/2*3?

Answer 2¶

12

Question 3¶

What is the result of 7 % 2?

Answer 3¶

1

Question 4¶

How do you raise x to the 3rd power?

Answer 4¶

x**3

Question 5¶

give an expression using base python that converts a float x to the nearest integer by rounding (not truncation)

Answer 5¶

y = int(x + 0.5)

Question 6¶

What is the result of the following computation:

2 * 3 + 1 # 5

Answer 6¶

7 ("# 5" is a comment)

Question 7¶

If x is a float and y is an int, what type is x*y?

Answer 7¶

float

Question 8¶

What is overflow in computer arithmetic? What happens in that case?

Answer 8¶

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).

Question 9¶

What is underflow in computer arithmetic? What happens in that case?

Answer 9¶

When a value is too small to be represented by the precision of the variable. It gets approximated by zero.

Conditional Statements¶

Question 1¶

Write an if statement that prints "Yes" if x is greater than 5.

Answer 1¶

if x > 5:
    print("Yes")

Question 2¶

Write an if-else that prints "Positive" if x > 0, otherwise "Not positive".

Answer 2¶

if x > 0:
    print("Positive")
else:
    print("Not positive")

Question 3¶

How do you test whether x is equal to 10?

Answer 3¶

x == 10

Question 4¶

What is the result of True and False?

Answer 4¶

False

Question 5¶

What is the result of True or False?

Answer 5¶

True

Question 6¶

Write an if statement that checks if x is zero.

Answer 6¶

if x == 0:
    print("Zero")

Question 7¶

What symbol is used for “not equal”?

Answer 7¶

!=

Question 8¶

Write a condition that checks if x is less than or equal to 10.

Answer 8¶

x <= 10

Question 9¶

What is the result of 5 > 3?

Answer 9¶

True

Question 10¶

What is the result of 5 < 3?

Answer 10¶

False

Question 11¶

why doesn't == work well for floats and how can this be addressed?

Answer 11¶

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()

Question 12¶

Write a test that checks whether variables x and y are within $10^5$ of each other and prints True if so

Answer 12¶

if abs(x-y) <= 1e-5:
    print(True)

Functions¶

Question 1¶

Define a function hello() that prints "Hi".

Answer 1¶

def hello():
    print("Hi")

Question 2¶

Write a function add(a, b) that returns their sum.

Answer 2¶

def add(a, b):
    return a + b

Question 3¶

Write a function that takes input x and returns x * 3.

Answer 3¶

def triple(x):
    return x * 3

Question 4¶

Write a function that takes input x and returns x * 3, which defaults to returning 3 when there is no input given.

Answer 4¶

def triple(x=1):
    return x * 3

Question 5¶

What is returned if a function has no return statement?

Answer 5¶

It returns None.

Question 6¶

Write a function that takes no arguments and returns 10.

Answer 6¶

def give_ten():
    return 10

Question 7¶

What does the following code print?

def make_it_zero():
    x = 0
    print(x) # print x

x = 5
make_zero()
print(x)  # print x

Answer 7¶

0
5

Loops¶

Question 1¶

Write a for loop that prints numbers from 0 to 2.

Answer 1¶

for i in range(3):
    print(i)

Question 2¶

What does range(4) produce?

Answer 2¶

0, 1, 2, 3

Question 3¶

Write a while loop that prints numbers 1 to 3.

Answer 3¶

i = 1
while i <= 3:
    print(i)
    i += 1

Question 4¶

What happens if a while condition never becomes False?

Answer 4¶

It creates an infinite loop.

Question 5¶

Write a for loop that prints "Hello" twice.

Answer 5¶

for i in range(2):
    print("Hello")

Question 6¶

What keyword stops a loop immediately?

Answer 6¶

break

Question 7¶

What keyword skips to the next iteration?

Answer 7¶

continue

Question 8¶

Write a for loop that prints each item in [1, 2, 3].

Answer 8¶

for x in [1, 2, 3]:
    print(x)

Question 9¶

What does range(1, 4) produce?

Answer 9¶

1, 2, 3

Question 10¶

Write a while loop that prints every number from zero to N.

Answer 10¶

x = 0
while x<N+1:
    print(x)
    x = x+1

Lists¶

Question 1¶

Create a list containing 4, 5, and 6.

Answer 1¶

my_list = [4, 5, 6]

Question 2¶

Change the second element of my_list to zero.

Answer 2¶

my_list[1] = 0

Question 3¶

What does len(my_list) return? where my_list = [4, 5, 6]

Answer 3¶

The number of elements in the list, in this case 3.

Question 4¶

Add 7 to the end of my_list (making the lost longer by one element).

Answer 4¶

my_list.append(7)

Question 5¶

Remove the last element.

Answer 5¶

my_list.pop()

Question 6¶

What does [1, 2] + [3,4] produce?

Answer 6¶

[1, 2, 3, 4]

Question 7¶

Write a loop to print all elements in my_list.

Answer 7¶

for x in my_list:
    print(x)

Question 8¶

Extract the elements from index i to index j (inclusive) from my_list into a new list

Answer 8¶

newlist = my_list[i:j+1]

Question 9¶

Extract the first N elements from my_list into a new list

Answer 9¶

newlist = my_list[:N]

Question 10¶

Extract the last element from my_list

Answer 10¶

my_list[-1]

or

my_list[len(mylist)-1]

Question 11¶

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.

Answer 11¶

evens = my_list[::2]
odds = my_list[1::2]

Strings¶

Question 1¶

Create a string s containing the word "Code" in python

Answer 1¶

s = "Code"

Question 2¶

What does len("Code") return?

Answer 2¶

4

Question 3¶

Access the first character of s.

Answer 3¶

s[0]

Question 4¶

What does "Hi" + "There" produce?

Answer 4¶

"HiThere"

Question 5¶

How do I convert a string s to all uppercase?

Answer 5¶

S = s.upper()

Question 6¶

How do I convert a string S to all lowercase? ?

Answer 6¶

s = S.lower()

Question 7¶

Write a loop to print each character in s.

Answer 7¶

for c in s:
    print(c)

Question 8¶

What does "hello"[1] return?

Answer 8¶

"e"

Question 9¶

What does "test" * 2 produce?

Answer 9¶

"testtest"

Question 10¶

What does this code produce?

s = "hello"
s[0] = "H"

Answer 10¶

An error. Strings are not mutable.

String Functions¶

Question 1¶

You are given the string "apple;banana;cherry;date". Write code that creates a list of the fruit names.

Answer 1¶

s = "apple;banana;cherry;date"
fruits = s.split(";")

Question 2¶

You are given the list ["2026", "02", "17"]. Write code that constructs the date string "2026/02/17".

Answer 2¶

parts = ["2026", "02", "17"]
date_string = "/".join(parts)

Question 3¶

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.

Answer 3¶

s = "The experiment failed due to error code 404."
position = s.find("404")

Question 4¶

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.

Answer 4¶

s = "I like Java programming."
new_string = s.replace("Java", "Python")