Write a module lf_marks.py with average() and grade() functions and a __main__ guard, then import it and use it. (The program writes the module to a temporary folder so it runs as one file.)
import sys, tempfile, pathlib
folder = pathlib.Path(tempfile.mkdtemp())
(folder / "lf_marks.py").write_text('''
def average(marks):
return sum(marks) / len(marks)
def grade(avg):
return "A" if avg >= 85 else "B" if avg >= 70 else "C"
if __name__ == "__main__":
print("self-test:", grade(average([90, 80])))
''')
sys.path.insert(0, str(folder))
import lf_marks
for name, marks in {"Asha": [91, 88, 95], "Ravi": [72, 65, 70]}.items():
avg = lf_marks.average(marks)
print(f"{name}: {avg:.1f} -> {lf_marks.grade(avg)}")
Asha: 91.3 -> A Ravi: 69.0 -> C
The self-test line does not run because the module is imported, not executed. Calls use the module prefix lf_marks.