🐍 Python Code
# EXAMPLE 51
# Create and Call Function
def message():
print("Welcome to Python Functions")
message()
🤖 Python Output
Welcome to Python Functions 🎉
A fun beginner programming course for young programmers
Created by Geta Ba / GETCOM
A function is a reusable block of code that performs a task.
# EXAMPLE 51
# Create and Call Function
def message():
print("Welcome to Python Functions")
message()
Welcome to Python Functions 🎉
Functions can receive information using parameters.
# EXAMPLE 52
# Greeting Function
def greeting(name):
print("Hello", name)
greeting("Gech")
Hello Gech 👋
A function can receive numbers and return a result.
# EXAMPLE 53
# Add Two Numbers
def add(a,b):
return a+b
result = add(10,20)
print("Sum =", result)
Sum = 30
Functions can perform mathematical operations.
# EXAMPLE 54
# Subtraction Function
def subtract(a,b):
return a-b
answer = subtract(50,20)
print("Difference =", answer)
Difference = 30
Functions make repeated calculations easier.
# EXAMPLE 55
# Multiplication Function
def multiply(a,b):
return a*b
result = multiply(5,6)
print("Multiplication =", result)
Multiplication = 30 ⭐
A function can divide two numbers and return the answer.
# EXAMPLE 56
# Division Function
def divide(a,b):
return a / b
result = divide(20,5)
print("Division =", result)
Division = 4.0
A function can check whether a number is even or odd.
# EXAMPLE 57
# Even or Odd Function
def check_number(number):
if number % 2 == 0:
return "Even Number"
else:
return "Odd Number"
result = check_number(15)
print(result)
Odd Number
A function can automatically calculate student grades.
# EXAMPLE 58
# Student Grade Function
def grade(mark):
if mark >= 90:
return "A"
elif mark >= 70:
return "B"
elif mark >= 50:
return "C"
else:
return "F"
result = grade(85)
print("Grade =", result)
Mark = 85
Grade = B ⭐
A function can calculate the average of numbers.
# EXAMPLE 59
# Average Function
def average(a,b,c):
total = a+b+c
avg = total/3
return avg
result = average(80,90,70)
print("Average =", result)
Marks = 80, 90, 70
Average = 80.0
A function can identify vowels and consonants.
# EXAMPLE 60
# Vowel Checker Function
def check_letter(letter):
letter = letter.lower()
if letter in "aeiou":
return "Vowel"
else:
return "Consonant"
result = check_letter("A")
print("Letter A is", result)
Letter A is
Vowel 🔤
A function can find the largest number from given values.
# EXAMPLE 61
# Maximum Number Function
def maximum(a,b,c):
if a > b and a > c:
return a
elif b > c:
return b
else:
return c
result = maximum(20,50,30)
print("Maximum =", result)
Numbers = 20, 50, 30
Maximum = 50 📈
A function can find the smallest number from values.
# EXAMPLE 62
# Minimum Number Function
def minimum(a,b,c):
if a < b and a < c:
return a
elif b < c:
return b
else:
return c
result = minimum(20,50,30)
print("Minimum =", result)
Numbers = 20, 50, 30
Minimum = 20 📉
A function can repeat actions using loops.
# EXAMPLE 63
# Loop Function
def print_numbers():
for i in range(1,6):
print(i)
print_numbers()
1
2
3
4
5 🔁
A function can calculate factorial values using loops.
# EXAMPLE 64
# Factorial Function
def factorial(number):
result = 1
for i in range(1, number+1):
result = result * i
return result
answer = factorial(5)
print("Factorial =", answer)
5! = 5×4×3×2×1
Factorial = 120 ⭐
A function can check whether a number is prime.
# EXAMPLE 65
# Prime Number Function
def prime(number):
if number <= 1:
return "Not Prime"
for i in range(2,number):
if number % i == 0:
return "Not Prime"
return "Prime Number"
result = prime(7)
print(result)
Number = 7
Prime Number ✅
A function can perform different mathematical operations.
# EXAMPLE 66
# Calculator Function
def calculator(a,b,operation):
if operation == "+":
return a+b
elif operation == "-":
return a-b
elif operation == "*":
return a*b
elif operation == "/":
return a/b
else:
return "Invalid Operation"
result = calculator(20,5,"*")
print("Answer =", result)
20 * 5
Answer = 100 🧮
A function can calculate total salary including bonus.
# EXAMPLE 67
# Salary Calculator
def salary_calculator(basic,bonus):
total = basic + bonus
return total
salary = salary_calculator(5000,1000)
print("Total Salary =", salary)
Basic Salary = 5000
Bonus = 1000
Total Salary = 6000 💰
A function can calculate product total cost.
# EXAMPLE 68
# Shopping Cost
def shopping(price,quantity):
total = price * quantity
return total
cost = shopping(50,4)
print("Total Cost =", cost)
Price = 50
Quantity = 4
Total Cost = 200 🛒
Functions can manage student information.
# EXAMPLE 69
# Student Registration
students = []
def register_student(name):
students.append(name)
return "Student Added Successfully"
print(register_student("Gech"))
print(register_student("Abebe"))
print(students)
Student Added Successfully
Student Added Successfully
['Gech', 'Abebe'] 👨🎓
A small banking system using multiple functions.
# EXAMPLE 70
# Mini Banking System
balance = 0
def deposit(amount):
global balance
balance += amount
def withdraw(amount):
global balance
if amount <= balance:
balance -= amount
else:
print("Insufficient Balance")
def show_balance():
print("Balance =", balance)
deposit(1000)
withdraw(300)
show_balance()
Deposit = 1000
Withdraw = 300
Balance = 700 💰