Hands-On Python Exercise: Understanding Basic Functions


๐น 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
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.