Skip to main content

✍️ Many Mini Functions

The exercises on this page are small and many. Try as many as you can to get used to writing functions.

Pay close attention to whether your function should do a print(), or if it should do a return.

  AI: These examples were generated by AI and then reviewed and corrected by a human.
 

Use a prompt like this with your favourite AI companion to get more exercises if you need:

I'm a student learning to program in Python. Make exercises for me to practice writing simple functions.  

Criteria:
- use only simple types: int, bool, float, str
- do not require the use of conditional statements or any control flow beyond the return keyword
- do not require the use of string manipulation
- each function body should be 1-3 lines
- make exercises for 0, 1, 2 params
- require some functions to display (i.e. print) something, but most functions (say 75%) should be required to return values instead
- can require basic arithmetic, but no external libraries such as the math library

greet

Write a function greet that takes no parameters and prints "Hello, World!".

The code below calls your function:

greet()

add

Write a function add that takes two parameters a and b and returns their sum. Include code that calls add with two numbers of your choice and prints the result.

square

Write a function square that is given one number n (i.e. a parameter) and returns n squared. Write code that calls your function with a number of your choice and prints the result.

celsius_to_fahrenheit

Write a function celsius_to_fahrenheit that takes one number c (degrees Celsius) and returns the equivalent in Fahrenheit (c * 9 / 5 + 32).

Include code that calls celsius_to_fahrenheit with a temperature of your choice and prints the result.

shout

Write a function shout that takes one string s and returns the string in uppercase followed by an exclamation mark.

You’ll want to use the Python string function “upper” like this:

my_name = "bobby"
my_name_caps = my_name.upper()  # this returns "BOBBY"

The code below calls your function:

result = shout("hello")
print(result)

average

Write a function average that takes two numbers a and b and returns their average.

The code below calls your function:

result = average(4, 8)
print(result)

full_name

Write a function full_name that takes two strings first and last and returns them combined as "first last".

The code below calls your function:

result = full_name("Ada", "Lovelace")
print(result)

rectangle_area

Write a function rectangle_area that takes two numbers w and h and returns the area of a rectangle with those dimensions.

The code below calls your function:

result = rectangle_area(3, 5)
print(result)

double

Write a function double that takes one number n and returns twice its value.

The code below calls your function:

result = double(21)
print(result)

bmi

Write a function bmi that takes weight (kg) and height (m) and returns the body mass index (weight / (height * height)).

The code below calls your function:

result = bmi(70, 1.75)
print(result)

get_pi_approx

Write a function get_pi_approx that takes no parameters and returns the value 3.14159. Add a call to your function.