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
numbercontaining anint, write a single line of code that asserts thatnumberis equal to 3 - Given a variable
countcontaining anint, write a single line of code that asserts thatcountis greater than or equal to 0. - Given two variables
firstandsecondcontainingintvalues, write a single line of code that asserts thatfirstis strictly smaller thansecond. - Given two variables
num_applesandnum_pearscontainingintvalues, write a single line of code that asserts thatnum_applesandnum_pearsare not equal. - Given a variable
daycontaining an int, write a single line of code that asserts thatdayis between 1 and 7 inclusive.
String assertions
- Given a variable
namecontaining a String, write a single line of code that asserts thatnamecontains the value 'Ben' - Given a variable
namecontaining a String, write a single line of code that asserts thatnamedoes not contain the value 'John' - Given a variable
namecontaining a String, write a single line of code that asserts thatnameis at most 10 characters long. - Given a variable
namecontaining a String, write a single line of code that asserts thatnameis neither 'Ben' nor 'John'
type assertions
- Given a variable
count, write a single line of code that assertscountcontains anint - Given a variable
ratio, write a single line of code that assertsratiocontains afloat - Given a variable
name, write a single line of code that asserts thatnameis a String. - Given two variables
num_applesandnum_pears, write a single line of code that asserts that bothnum_applesandnum_pearscontainints
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 returnsmeterstimes 1000 - Write a function header for a function named
multiply, which takes in two positional parameters namedmultiplierandmultiplicand - Write a function header for a function named
get_capacity, which takes in a single parameter namedwater_levelwith default value0 - Write a function header for a function named
take_powerwhich 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
nummultiplied 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 ofnumplus the value3. - Write a function named
compare, which takes in two parametersaandb, which returnsTrueifais greater thanb, andFalseotherwise. 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 thatnumis anint, and then returnsnumraised to the 5th power. This function should be three lines including the header.