Program 1: Student Marks Report using Functions
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
# Function to calculate total and percentage
def calculate_result(marks):
total = sum(marks)
percentage = total / len(marks)
return total, percentage
# Function to decide grade
def find_grade(percentage):
if percentage >= 90:
return "A+"
elif percentage >= 75:
return "A"
elif percentage >= 60:
return "B"
elif percentage >= 40:
return "C"
else:
return "Fail"
student = "Aarav"
marks = [86, 92, 78, 88, 81]
total, percentage = calculate_result(marks)
grade = find_grade(percentage)
print("Student Name:", student)
print("Marks:", marks)
print("Total Marks:", total)
print("Percentage:", percentage)
print("Grade:", grade)
Output:
Student Name: Aarav
Marks: [86, 92, 78, 88, 81]
Total Marks: 425
Percentage: 85.0
Grade: A
Program 2: Simple Calculator using Functions
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Division by zero is not allowed"
return a / b
def modulus(a, b):
return a % b
x = 20
y = 6
print("First number:", x)
print("Second number:", y)
print("Addition:", add(x, y))
print("Subtraction:", subtract(x, y))
print("Multiplication:", multiply(x, y))
print("Division:", divide(x, y))
print("Modulus:", modulus(x, y))
Output:
First number: 20
Second number: 6
Addition: 26
Subtraction: 14
Multiplication: 120
Division: 3.3333333333333335
Modulus: 2
Program 3: Count Vowels in a Sentence using Function
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def count_vowels(sentence):
vowels = "aeiouAEIOU"
count = 0
for ch in sentence:
if ch in vowels:
count += 1
return count
text = "Python makes file handling easy"
answer = count_vowels(text)
print("Sentence:", text)
print("Number of vowels:", answer)
Output:
Sentence: Python makes file handling easy
Number of vowels: 9
Program 4: Factorial using Recursive Function
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
number = 5
result = factorial(number)
print("Number:", number)
print("Factorial:", result)
Output:
Number: 5
Factorial: 120
Program 5: Write Student Data to a File
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def save_student(filename, name, roll, marks):
with open(filename, "w") as file:
[Link]("Student Name: " + name + "\n")
[Link]("Roll Number: " + str(roll) + "\n")
[Link]("Marks: " + str(marks) + "\n")
file_name = "[Link]"
save_student(file_name, "Riya", 12, 91)
print("Data saved successfully in", file_name)
with open(file_name, "r") as file:
print("\nFile Content:")
print([Link]())
Output:
Data saved successfully in [Link]
File Content:
Student Name: Riya
Roll Number: 12
Marks: 91
Program 6: Read a File and Count Words
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def count_words(filename):
with open(filename, "r") as file:
text = [Link]()
words = [Link]()
return len(words)
with open("[Link]", "w") as file:
[Link]("Python is simple and powerful. ")
[Link]("File handling is very useful.")
words = count_words("[Link]")
print("Total words in file:", words)
Output:
Total words in file: 9
Program 7: Append Daily Tasks to a File
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def add_task(filename, task):
with open(filename, "a") as file:
[Link](task + "\n")
file_name = "[Link]"
add_task(file_name, "Complete Python homework")
add_task(file_name, "Revise functions")
add_task(file_name, "Practice file handling")
print("Tasks added successfully")
print("\nAll Tasks:")
with open(file_name, "r") as file:
print([Link]())
Output:
Tasks added successfully
All Tasks:
Complete Python homework
Revise functions
Practice file handling
Program 8: Copy Content from One File to Another
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def copy_file(source, destination):
with open(source, "r") as file1:
data = [Link]()
with open(destination, "w") as file2:
[Link](data)
with open("[Link]", "w") as file:
[Link]("This is the original file.\n")
[Link]("It contains important text.")
copy_file("[Link]", "[Link]")
print("File copied successfully")
print("\nCopied File Content:")
with open("[Link]", "r") as file:
print([Link]())
Output:
File copied successfully
Copied File Content:
This is the original file.
It contains important text.
Program 9: Find Longest Word in a File
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def longest_word(filename):
with open(filename, "r") as file:
words = [Link]().split()
longest = words[0]
for word in words:
if len(word) > len(longest):
longest = word
return longest
with open("[Link]", "w") as file:
[Link]("function program computer file handling")
word = longest_word("[Link]")
print("Longest word in file:", word)
Output:
Longest word in file: handling
Program 10: Mini Result System using File Handling
Aim: To demonstrate Python functions and/or file handling with clear output.
Program:
def calculate_percentage(marks):
return sum(marks) / len(marks)
def write_result(filename, name, marks):
percentage = calculate_percentage(marks)
with open(filename, "w") as file:
[Link]("Name: " + name + "\n")
[Link]("Marks: " + str(marks) + "\n")
[Link]("Percentage: " + str(percentage) + "\n")
if percentage >= 33:
[Link]("Result: Pass\n")
else:
[Link]("Result: Fail\n")
write_result("[Link]", "Kabir", [72, 81, 69, 75, 84])
print("Result file created successfully")
print("\nResult File Content:")
with open("[Link]", "r") as file:
print([Link]())
Output:
Result file created successfully
Result File Content:
Name: Kabir
Marks: [72, 81, 69, 75, 84]
Percentage: 76.2
Result: Pass