The Big List
This is an in-progress list of Python programming practice problems. The problems are roughly categorized by topic, and questions are listed so that a particular question only requires concepts covered before it. For example, questions in the Functions section will not require if-statements.
We will not post solutions to these questions, and there is no autograder. We heavily encourage you to compare solutions to classmates or come to office hours if you are unsure about your solutions. If you get stuck, check out the Getting Unstuck guide, or post to Piazza.
Asserts
int
assertions
- Given a variable
number
containing anint
, write a single line of code that asserts thatnumber
is equal to 3 - Given a variable
count
containing anint
, write a single line of code that asserts thatcount
is greater than or equal to 0. - Given two variables
first
andsecond
containingint
values, write a single line of code that asserts thatfirst
is strictly smaller thansecond
. - Given two variables
num_apples
andnum_pears
containingint
values, write a single line of code that asserts thatnum_apples
andnum_pears
are not equal. - Given a variable
day
containing an int, write a single line of code that asserts thatday
is between 1 and 7 inclusive.
String
assertions
- Given a variable
name
containing a String, write a single line of code that asserts thatname
contains the value 'Ben' - Given a variable
name
containing a String, write a single line of code that asserts thatname
does not contain the value 'John' - Given a variable
name
containing a String, write a single line of code that asserts thatname
is at most 10 characters long. - Given a variable
name
containing a String, write a single line of code that asserts thatname
is neither 'Ben' nor 'John'
type
assertions
- Given a variable
count
, write a single line of code that assertscount
contains anint
- Given a variable
ratio
, write a single line of code that assertsratio
contains afloat
- Given a variable
name
, write a single line of code that asserts thatname
is a String. - Given two variables
num_apples
andnum_pears
, write a single line of code that asserts that bothnum_apples
andnum_pears
containint
s
Functions
Define parameters in the order they are listed in the prompt.
Function headers
- Write the function header for a function named
my_function
, which takes in no parameters - Write the function header for a function named
convert
, which takes in a single parameter namedmeters
, and returnsmeters
times 1000 - Write a function header for a function named
multiply
, which takes in two positional parameters namedmultiplier
andmultiplicand
- Write a function header for a function named
get_capacity
, which takes in a single parameter namedwater_level
with default value0
- Write a function header for a function named
take_power
which takes in two parameters:base
, with no default value, andexponent
, with a default value of 2
Return statements
- Complete the following function by writing a single line of code that returns the value
3
def return_three():
- Complete the following function by writing a single line of code that returns the value of
result
def return_variable(num):
result = num + 3
- Complete the following function by writing a single line of code that returns the value of the parameter
num
def return_parameter(num):
- Complete the following function by writing a single line of code that returns the value of the parameter
num
multiplied by5
def quintuple(num):
- Complete the following function by writing a single line of code that returns the value of the first parameter subtracted by the second parameter
def subtract(a, b):
Full functions
- Write a function named
print_hello
, which takes in no parameters and prints the String 'Hello!' - Write a function named
add_three
, which takes in a single parameternum
, and returns the value ofnum
plus the value3
. - Write a function named
compare
, which takes in two parametersa
andb
, which returnsTrue
ifa
is greater thanb
, andFalse
otherwise. This function should be two lines including the header, and should not require the use of if-statements or a ternary! - Write a function named
fifth_power
, which takes in a single parameternum
, asserts thatnum
is anint
, and then returnsnum
raised to the 5th power. This function should be three lines including the header.