Functions in Python Project
School Name: __________
Subject: Computer Science (083)
Student Name: __________
Class & Section: __________
Roll Number: __________
Academic Session: __________
Submitted To: __________
Introduction
Functions in Python are reusable blocks of code that help reduce repetition and improve readability.
They allow modular programming and make debugging easier. Below are detailed programs
demonstrating functions.
1. Student Marks Analyzer
This program demonstrates the use of functions in a practical scenario.
def calculate_total(marks):
return sum(marks)
def calculate_average(marks):
return sum(marks)/len(marks)
def find_grade(avg):
if avg >= 90:
return 'A'
elif avg >= 75:
return 'B'
elif avg >= 50:
return 'C'
else:
return 'Fail'
marks = [80, 90, 85, 70, 60]
total = calculate_total(marks)
avg = calculate_average(marks)
grade = find_grade(avg)
print("Total:", total)
print("Average:", avg)
print("Grade:", grade)
2. Banking System
This program demonstrates the use of functions in a practical scenario.
def deposit(balance, amount):
return balance + amount
def withdraw(balance, amount):
if amount > balance:
return "Insufficient Balance"
return balance - amount
balance = 1000
balance = deposit(balance, 500)
print("After Deposit:", balance)
balance = withdraw(balance, 300)
print("After Withdrawal:", balance)
3. Calculator Using Functions
This program demonstrates the use of functions in a practical scenario.
def add(a,b): return a+b
def sub(a,b): return a-b
def mul(a,b): return a*b
def div(a,b): return a/b if b!=0 else "Error"
print(add(5,3))
print(sub(5,3))
print(mul(5,3))
print(div(5,3))
4. Prime Numbers in Range
This program demonstrates the use of functions in a practical scenario.
def is_prime(n):
if n<2: return False
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def primes_in_range(start,end):
for i in range(start,end+1):
if is_prime(i):
print(i,end=" ")
primes_in_range(1,50)
5. Password Validator
This program demonstrates the use of functions in a practical scenario.
def check_length(p): return len(p)>=8
def check_digit(p): return any([Link]() for c in p)
def check_upper(p): return any([Link]() for c in p)
password = "Pass1234"
if check_length(password) and check_digit(password) and check_upper(password):
print("Strong Password")
else:
print("Weak Password")
6. List Operations
This program demonstrates the use of functions in a practical scenario.
def find_max(lst): return max(lst)
def find_min(lst): return min(lst)
def sort_list(lst): return sorted(lst)
lst = [5,2,9,1]
print(find_max(lst))
print(find_min(lst))
print(sort_list(lst))
7. Palindrome Checker
This program demonstrates the use of functions in a practical scenario.
def is_palindrome(s):
return s == s[::-1]
words = ["madam","hello","level"]
for w in words:
print(w, is_palindrome(w))
8. Fibonacci Series
This program demonstrates the use of functions in a practical scenario.
def fib(n):
a,b=0,1
for i in range(n):
print(a,end=" ")
a,b=b,a+b
fib(10)
9. Factorial Program
This program demonstrates the use of functions in a practical scenario.
def factorial(n):
result=1
for i in range(1,n+1):
result*=i
return result
print(factorial(6))
10. File Handling
This program demonstrates the use of functions in a practical scenario.
def write_file():
with open("[Link]","w") as f:
[Link]("Hello")
def read_file():
with open("[Link]","r") as f:
print([Link]())
write_file()
read_file()
11. Temperature Converter
This program demonstrates the use of functions in a practical scenario.
def c_to_f(c): return (c*9/5)+32
def f_to_c(f): return (f-32)*5/9
print(c_to_f(0))
print(f_to_c(32))
12. Number Guessing Game
This program demonstrates the use of functions in a practical scenario.
import random
def guess_game():
num=[Link](1,10)
guess=int(input("Enter number: "))
if guess==num:
print("Correct")
else:
print("Wrong, number was",num)
# guess_game()
13. Sum of Digits
This program demonstrates the use of functions in a practical scenario.
def sum_digits(n):
return sum(int(d) for d in str(n))
print(sum_digits(12345))
14. Multiplication Table
This program demonstrates the use of functions in a practical scenario.
def table(n):
for i in range(1,11):
print(n,"x",i,"=",n*i)
table(5)
15. Dictionary Operations
This program demonstrates the use of functions in a practical scenario.
def add_student(d,name,marks):
d[name]=marks
def display(d):
for k,v in [Link]():
print(k,v)
students={}
add_student(students,"A",90)
add_student(students,"B",80)
display(students)