Python Exam 4: Functions, Lambda, and Modules (10 mixed exercises)

Python Exam 4: Functions and Modules

Test your ability to write reusable code with functions and modules.

Exercise 1 Creating a Function

Create a simple function:

  1. Define a function named `my_function`.
  2. The function prints "Hello from a function".
  3. Call the function to run it.
Solution
def my_function():
  print("Hello from a function")

my_function()
Exercise 2 Arguments

Pass data to a function:

  1. Define a function `my_function` that takes an argument `fname`.
  2. The function prints the name followed by "Refsnes".
  3. Call it with the name "Emil".
Solution
def my_function(fname):
  print(fname + " Refsnes")

my_function("Emil")
Exercise 3 Arbitrary Arguments (*args)

Handle a variable number of inputs:

  1. Define a function that accepts `*kids`.
  2. Print "The youngest child is " followed by the third item (index 2).
  3. Call it with "Emil", "Tobias", "Linus".
Solution
def my_function(*kids):
  print("The youngest child is " + kids[2])

my_function("Emil", "Tobias", "Linus")
Exercise 4 Keyword Arguments (**kwargs)

Handle a dictionary of arguments:

  1. Define a function that accepts `**kid`.
  2. Print the last name `kid["lname"]`.
  3. Call it with `fname="Tobias", lname="Refsnes"`.
Solution
def my_function(**kid):
  print("His last name is " + kid["lname"])

my_function(fname = "Tobias", lname = "Refsnes")
Exercise 5 Return Values

Return a result from a function:

  1. Define `my_function(x)` that returns `5 * x`.
  2. Print the result for input 3.
Solution
def my_function(x):
  return 5 * x

print(my_function(3))
Exercise 6 Lambda Functions

Create an anonymous function:

  1. Create a `lambda` function that takes `a` and adds 10.
  2. Store it in a variable `x`.
  3. Print the result of `x(5)`.
Solution
x = lambda a : a + 10
print(x(5))
Exercise 7 Modules

Use an external module:

  1. Import a module `mymodule` (assume it exists).
  2. Use it with the alias `mx`.
  3. Print `mx.person1["age"]`.
Solution
import mymodule as mx

# print(mx.person1["age"])
Exercise 8 Dates

Work with dates:

  1. Import the `datetime` module.
  2. Print the current time using `datetime.datetime.now()`.
  3. Print only the current year.
Solution
import datetime

x = datetime.datetime.now()
print(x)
print(x.year)
Exercise 9 Math

Use math functions:

  1. Use `min` and `max` to find the smallest and largest numbers in `(5, 10, 25)`.
  2. Use `abs(-7.25)` to get the absolute value.
  3. Use `pow(4, 3)` to calculate a power.
Solution
x = min(5, 10, 25)
y = max(5, 10, 25)
z = abs(-7.25)
p = pow(4, 3)

print(x, y, z, p)
Exercise 10 JSON in Python

Convert data:

  1. Import `json`.
  2. You have a dictionary `x = {"name": "John", "age": 30}`.
  3. Convert it to JSON text with `json.dumps(x)`.
  4. Print the result.
Solution
import json

x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}

y = json.dumps(x)

print(y)
Smart Editor

Write code and see the result instantly

Try it free