0% found this document useful (0 votes)
3 views3 pages

Python 3

The document contains a Python programming assignment by a freshman student, Mvondo Anthony, detailing various tasks including an area and volume calculator for different shapes, file operations for reading numbers and writing cities, temperature conversion from Celsius to Fahrenheit, and population density calculations. Each task is implemented with code snippets demonstrating the use of mathematical functions, file handling, and conditional statements. The assignment is supervised by Dr. Augustine S. Nsang at the PK Fokam Institute of Excellence.

Uploaded by

Anthony Junang
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)
3 views3 pages

Python 3

The document contains a Python programming assignment by a freshman student, Mvondo Anthony, detailing various tasks including an area and volume calculator for different shapes, file operations for reading numbers and writing cities, temperature conversion from Celsius to Fahrenheit, and population density calculations. Each task is implemented with code snippets demonstrating the use of mathematical functions, file handling, and conditional statements. The assignment is supervised by Dr. Augustine S. Nsang at the PK Fokam Institute of Excellence.

Uploaded by

Anthony Junang
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

# -------------------------------------------------------------

# Name: Mvondo Anthony


# Level: Freshman 1
# Course: Python Programming
# Instructor: Dr. Augustine S. Nsang
# School: PK Fokam Institute of Excellence
# -------------------------------------------------------------

Question 1 : Area and Volume Calculator

>>> import math


>>> print("Choose a shape:")
>>> print("1. Cone")
>>> print("2. Cylinder")
>>> print("3. Sphere")
>>> choice = input("Enter your choice (1/2/3): ")

>>> if choice == "1":


... r = float(input("Enter the radius of the cone: "))
... h = float(input("Enter the height of the cone: "))
... l = [Link](r**2 + h**2)
... area = [Link] * r * (r + l)
... volume = (1/3) * [Link] * r**2 * h
... print("Surface Area of Cone =", round(area, 2))
... print("Volume of Cone =", round(volume,

...
>>> elif choice == "2":
... r = float(input("Enter the radius of the cylinder: "))
... h = float(input("Enter the height of the cylinder: "))
... area = 2 * [Link] * r * (r + h)
... volume = [Link] * r**2 * h
... print("Surface Area of Cylinder =", round(area, 2))
... print("Volume of Cylinder =", round(volume, 2))
...
>>> elif choice == "3":
... r = float(input("Enter the radius of the sphere: "))
... area = 4 * [Link] * r**2
... volume = (4/3) * [Link] * r**3
... print("Surface Area of Sphere =", round(area, 2))
... print("Volume of Sphere =", round(volume, 2))
...
>>> else:
... print("Invalid choice!")

Question 2 : File and List Operation

1: Read numbers from file and calculate sum + mean

>>> with open("[Link]", "r") as file:


... numbers = [float([Link]()) for line in file]
...
>>> total = sum(numbers)
>>> mean = total / len(numbers)
>>> print("Sum =", total)
>>> print("Mean =", mean)

2: Write cities alphabetically after 'Garoua' to a file

>>> cities = ["Bafoussam", "Bamenda", "Bertoua", "Douala", "Ebolowa",


... "Garoua", "Kribi", "Limbe", "Maroua", "Ngaoundere", "Yaounde"]
...
>>> after_garoua = [city for city in cities if city > "Garoua"]
>>> with open("[Link]", "w") as f:
... for city in after_garoua:
... [Link](city + "\n")
...
>>> print("Cities after 'Garoua' written to [Link]")

Question 3: Celsius Fahrenheit Conversion

>>> temps_in_celsius = [0, 10, 20, 30, 40, 100]


>>> temps_in_fahrenheit = []
>>> for c in temps_in_celsius:
... f = (9/5) * c + 32
... temps_in_fahrenheit.append(f)
...
>>> print("Celsius:", temps_in_celsius)
>>> print("Fahrenheit:", temps_in_fahrenheit)

Question 4: Population and Density


>>> population = float(input("Enter population: "))
>>> land_area = float(input("Enter land area: "))

>>> # a)
>>> if population < 10_000_000:
... print(population)
...
>>> # b)
>>> if 10_000_000 <= population <= 35_000_000:
... print(population)
...
>>> # c)
>>> density = population / land_area
>>> if density > 100:
... print("Densely populated")
...
>>> # d)
>>> if density > 100:
... print("Densely populated")
... else:
... print("Sparsely populated")

You might also like