Skip to content

Practice module 4

>Functions and Modules: coding practice

Start with the lesson drills, then combine the ideas in the module labs. Every task has automatic checks, progressive hints, reset and a solution.

Module coding progress9 exercises available

Lesson coding drills

One focused editor exercise for every lesson in this module. (0/5 passed this visit)

You can run every exercise now. Create a free ID to keep passes, code attempts and XP.

Lesson 1: Functions

Not passed yet

Write a function named square that accepts one number and returns its square. Call it with 3 and print the result.

Expected output: 9

# Write your code below

Lesson 2: Function Arguments

Not passed yet

Write a function named repeat with parameters word and times=2. Print word repeated times times. Call repeat("go", 3).

Expected output: gogogo

# Write your code below

Lesson 3: Scope and Recursion

Not passed yet

Write a recursive function named countdown. It should print the number, call itself with one less, and stop after printing 0. Call it with 3.

Expected output: 3
2
1
0

# Write your code below

Lesson 4: Lambda and Built-in Functions

Not passed yet

Use filter with a lambda to keep numbers greater than 3 from [1, 4, 2, 5]. Convert the result to a list and print it.

Expected output: [4, 5]

# Write your code below

Lesson 5: Modules and Packages

Not passed yet

Import the math module. Use math.floor to round 6.9 down, then print the result.

Expected output: 6

# Write your code below

Coding practice: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (0/4 passed this visit)

You can run every exercise now. Create a free ID to keep passes, code attempts and XP.

Lab 1: Write double()

Not passed yet

Write a function double(n) that RETURNS n multiplied by 2, then print double(21).

def double(n):
    # return the doubled value

print(double(21))

Lab 2: Celsius to Fahrenheit

Not passed yet

Write to_f(c) returning c × 9/5 + 32, rounded to 1 decimal. Print to_f(37).

def to_f(c):
    # convert and round

print(to_f(37))

Lab 3: Greeting with a default

Not passed yet

Write greet(name, greeting="Assalamu alaikum") returning "greeting, name!". Print greet("Mim") and then greet("Rafi", "Hello") — two lines.

def greet(name, greeting="Assalamu alaikum"):
    # return the combined string

print(greet("Mim"))
print(greet("Rafi", "Hello"))

Lab 4: Sum any number of bills

Not passed yet

Write total(*bills) that accepts ANY count of amounts and returns their sum. Print total(120, 80.5, 60).

def total(*bills):
    # return the sum

print(total(120, 80.5, 60))

References & Further Reading

Related entries from PyLabs's own reference library.

  • callable() (Check whether an object can be called with parentheses.)
  • locals() (Get the local names currently available in this part of the code.)
  • sorted() (Make a new list containing items in sorted order.)