User-Defined Functions (With Parameters) – Programs 1–10
Program 1: Calculate the Area of a Rectangle
Program:
def calculate_area(length, width):
"""Calculate and return the area of a rectangle given length and width."""
area = length * width
return area
# Test the function
print("Area of Rectangle:", calculate_area(5, 10))
Expected Output:
Area of Rectangle: 50
Program 2: Find the Square of a Number
Program:
def square(number):
"""Return the square of a number."""
return number ** 2
# Test the function
print("Square of 7:", square(7))
Expected Output:
Square of 7: 49
Program 3: Convert Celsius to Fahrenheit
Program:
def celsius_to_fahrenheit(celsius):
"""Convert Celsius to Fahrenheit."""
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
# Test the function
print("Temperature in Fahrenheit:", celsius_to_fahrenheit(25))
Expected Output:
Temperature in Fahrenheit: 77.0
Program 4: Check if a Number is Even or Odd
Program:
def is_even(number):
"""Check if a number is even or odd."""
return "Even" if number % 2 == 0 else "Odd"
# Test the function
print("Number 15 is:", is_even(15))
Expected Output:
Number 15 is: Odd
Program 5: Calculate Simple Interest
Program:
def simple_interest(principal, rate, time):
"""Calculate simple interest."""
interest = (principal * rate * time) / 100
return interest
# Test the function
print("Simple Interest:", simple_interest(1000, 5, 2))
Expected Output:
Simple Interest: 100.0
Program 6: Calculate Factorial of a Number
Program:
def factorial(n):
"""Calculate the factorial of a number."""
result = 1
for i in range(1, n + 1):
result *= i
return result
# Test the function
print("Factorial of 5:", factorial(5))
Expected Output:
Factorial of 5: 120
Program 7: Find Maximum of Three Numbers
Program:
def max_of_three(a, b, c):
"""Return the maximum of three numbers."""
return max(a, b, c)
# Test the function
print("Maximum of (3, 7, 5):", max_of_three(3, 7, 5))
Expected Output:
Maximum of (3, 7, 5): 7
Program 8: Count Vowels in a String
Program:
def count_vowels(s):
"""Count the number of vowels in a string."""
vowels = "aeiouAEIOU"
count = sum(1 for char in s if char in vowels)
return count
# Test the function
print("Number of Vowels:", count_vowels("Hello World"))
Expected Output:
Number of Vowels: 3
Program 9: Calculate the Power of a Number
Program:
def power(base, exponent):
"""Calculate the power of a number."""
return base ** exponent
# Test the function
print("2 raised to the power 3:", power(2, 3))
Expected Output:
2 raised to the power 3: 8
Program 10: Convert Kilometers to Miles
Program:
def km_to_miles(km):
"""Convert kilometers to miles."""
miles = km * 0.621371
return miles
# Test the function
print("5 km in miles:", km_to_miles(5))
Expected Output:
5 km in miles: 3.106855
User-Defined Functions (With Parameters) – Programs 11–20
Program 11: Calculate Compound Interest
Program:
def compound_interest(principal, rate, time):
"""Calculate compound interest."""
amount = principal * (1 + rate / 100) ** time
interest = amount - principal
return interest
# Test the function
print("Compound Interest:", compound_interest(1000, 5, 2))
Expected Output:
Compound Interest: 102.5
Program 12: Check if a Number is Prime
Program:
def is_prime(n):
"""Check if a number is prime."""
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
# Test the function
print("Is 7 prime?", is_prime(7))
Expected Output:
Is 7 prime? True
Program 13: Calculate the Length of a String
Program:
def string_length(s):
"""Calculate the length of a string."""
return len(s)
# Test the function
print("Length of 'Python':", string_length("Python"))
Expected Output:
Length of 'Python': 6
Program 14: Find the Minimum Value in a List
Program:
def find_minimum(numbers):
"""Find the minimum value in a list of numbers."""
return min(numbers)
# Test the function
print("Minimum in [5, 3, 9, 1]:", find_minimum([5, 3, 9, 1]))
Expected Output:
Minimum in [5, 3, 9, 1]: 1
Program 15: Calculate the Area of a Circle
Program:
def area_of_circle(radius):
"""Calculate the area of a circle given the radius."""
pi = 3.14159
return pi * radius ** 2
# Test the function
print("Area of Circle with radius 3:", area_of_circle(3))
Expected Output:
Area of Circle with radius 3: 28.27431
Program 16: Reverse a List
Program:
def reverse_list(lst):
"""Reverse the elements of a list."""
return lst[::-1]
# Test the function
print("Reversed list [1, 2, 3, 4]:", reverse_list([1, 2, 3, 4]))
Expected Output:
Reversed list [1, 2, 3, 4]: [4, 3, 2, 1]
Program 17: Find the GCD of Two Numbers
Program:
def gcd(a, b):
"""Find the greatest common divisor (GCD) of two numbers."""
while b:
a, b = b, a % b
return a
# Test the function
print("GCD of 48 and 18:", gcd(48, 18))
Expected Output:
GCD of 48 and 18: 6
Program 18: Count Words in a Sentence
Program:
def count_words(sentence):
"""Count the number of words in a sentence."""
words = sentence.split()
return len(words)
# Test the function
print("Number of words in 'Hello world program':", count_words("Hello world program"))
Expected Output:
Number of words in 'Hello world program': 3
Program 19: Check if a Year is a Leap Year
Program:
def is_leap_year(year):
"""Check if a year is a leap year."""
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
# Test the function
print("Is 2024 a leap year?", is_leap_year(2024))
Expected Output:
Is 2024 a leap year? True
Program 20: Concatenate Two Strings
Program:
def concatenate_strings(s1, s2):
"""Concatenate two strings."""
return s1 + s2
# Test the function
print("Concatenated String:", concatenate_strings("Hello, ", "World!"))
Expected Output:
Concatenated String: Hello, World!