Hands-On Python Exercise: Understanding Basic Functions

Arzath AreeffArzath Areeff
2 min read

๐Ÿ”น Question 1: Greet User

Write a function called greet_user that takes a name as input and prints a greeting message like "Hello, Areeff!".


๐Ÿ”น Question 2: Add Two Numbers

Write a function called add_numbers that takes two numbers and returns their sum.


๐Ÿ”น Question 3: Check Even or Odd

Write a function called is_even that takes a number and returns True if it's even, otherwise False.


๐Ÿ”น Question 4: Find the Square

Write a function called square that takes a number and returns its square.


๐Ÿ”น Question 5: Convert Celsius to Fahrenheit

Write a function called celsius_to_fahrenheit that converts a temperature from Celsius to Fahrenheit using the formula:

F = C * 9/5 + 32

๐Ÿง  Answers


โœ… Answer 1: Greet User

def greet_user(name):
    print(f"Hello, {name}!")

# Example call
greet_user("Areeff")

โœ… Answer 2: Add Two Numbers

def add_numbers(a, b):
    return a + b

# Example call
result = add_numbers(3, 5)
print(result)  # Output: 8

โœ… Answer 3: Check Even or Odd

def is_even(number):
    return number % 2 == 0

# Example call
print(is_even(4))  # Output: True
print(is_even(7))  # Output: False

โœ… Answer 4: Find the Square

def square(num):
    return num * num

# Example call
print(square(6))  # Output: 36

โœ… Answer 5: Celsius to Fahrenheit

def celsius_to_fahrenheit(celsius):
    return celsius * 9/5 + 32

# Example call
print(celsius_to_fahrenheit(0))   # Output: 32.0
print(celsius_to_fahrenheit(100)) # Output: 212.0
1
Subscribe to my newsletter

Read articles from Arzath Areeff directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arzath Areeff
Arzath Areeff

I co-founded digizen.lk to promote online safety and critical thinking. Currently, Iโ€™m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.