0% found this document useful (0 votes)
4 views5 pages

Simple Python Programs with Explanations

The document provides three simple Python programs with line-by-line explanations, inputs, and outputs. Program 10 generates a summary of data from a CSV file, Program 11 tracks student grades and identifies the top student, and Program 12 displays the contents of a specified directory. Each program includes sample inputs and outputs for clarity.

Uploaded by

Nelvit FernNdes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Simple Python Programs with Explanations

The document provides three simple Python programs with line-by-line explanations, inputs, and outputs. Program 10 generates a summary of data from a CSV file, Program 11 tracks student grades and identifies the top student, and Program 12 displays the contents of a specified directory. Each program includes sample inputs and outputs for clarity.

Uploaded by

Nelvit FernNdes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Simple Programs (10, 11, 12) with Line-by-Line Explanation, Input &

Output

Program 10: Simple Data Summary Generator

Program Code:
import csv

file = open("[Link]")

rows = [Link](file)

header = next(rows)

column = input("Enter column name: ")

index = [Link](column)

values = []

for r in rows:

[Link](float(r[index]))

print("Max =", max(values))

print("Min =", min(values))

print("Average =", sum(values) / len(values))

Line-by-Line Explanation:
• import csv – Imports the CSV module.

• file = open('[Link]') – Opens the CSV file.

• rows = [Link](file) – Reads all rows.

• header = next(rows) – Reads the first row (column names).


• column = input(...) – User enters column name.

• index = [Link](column) – Finds the column number.

• values = [] – Empty list for storing values.

• for r in rows: – Loops through each row.

• [Link](float(r[index])) – Converts and stores number.

• print('Max =', max(values)) – Prints maximum value.

• print('Min =', min(values)) – Prints minimum value.

• print('Average =', sum(values)/len(values)) – Prints average value.

Sample Input:
Enter column name: Temp

Sample CSV File ([Link]):


City,Temp
Delhi,33
Mumbai,30
Chennai,35
Kolkata,32

Sample Output:
Max = 35
Min = 30
Average = 32.5

Program 11: Simple Student Grade Tracker

Program Code:
students = []

marks = []

n = int(input("How many students? "))

for i in range(n):

name = input("Enter name: ")

mark = float(input("Enter marks: "))


[Link](name)

[Link](mark)

avg = sum(marks) / n

top_index = [Link](max(marks))

print("Average Marks =", avg)

print("Topper =", students[top_index], "with", marks[top_index], "marks")

Line-by-Line Explanation:
• students = [] – Stores student names.

• marks = [] – Stores marks.

• n = int(input(...)) – User enters number of students.

• for i in range(n): – Loop for each student.

• name = input(...) – Reads student name.

• mark = float(input(...)) – Reads marks.

• [Link](name) – Adds name.

• [Link](mark) – Adds marks.

• avg = sum(marks) / n – Calculates average.

• top_index = [Link](max(marks)) – Finds index of highest marks.

• print(...) – Displays results.

Sample Input:
How many students? 3
Enter name: Anu
Enter marks: 88
Enter name: Ravi
Enter marks: 92
Enter name: Maya
Enter marks: 76
Sample Output:
Average Marks = 85.33
Topper = Ravi with 92 marks

Program 12: Simple Directory Viewer

Program Code:
import os

path = input("Enter folder path: ")

for root, dirs, files in [Link](path):

print("Folder:", root)

for d in dirs:

print(" Subfolder:", d)

for f in files:

print(" File:", f)

Line-by-Line Explanation:
• import os – Allows folder operations.

• path = input(...) – User enters folder path.

• for root, dirs, files in [Link](path): – Walks through all folders/files.

• print('Folder:', root) – Shows current folder.

• for d in dirs: – Loops subfolders.

• print(' Subfolder:', d) – Prints subfolder.

• for f in files: – Loops files.

• print(' File:', f) – Prints file name.

Sample Input:
Enter folder path: test_folder

Sample Output:
Folder: test_folder
Subfolder: sub1
Subfolder: sub2
File: [Link]
File: [Link]

Folder: test_folder/sub1
File: [Link]

Folder: test_folder/sub2
File: [Link]

You might also like