Python Programming Assignment
Python program:
Create an empty list x in Python as follows: x=[]
- Use a while loop that would read 10 values from the user and store it in the list x.
- Use a for loop to display the values of the list on the screen.
- Add a portion of code that would total up all the values of the list and print the sum on the screen.
- Add a portion of code that would find and display the minimum value in the list (Use a for loop).
- Add a portion of code that would find and display the maximum value in the list (Use a for loop).
- Add a portion of code that increases the list elements that are less than 10 by 5 (Use a for loop).
# Create empty list
x = []
# Part a)
n = 0
while n < 10:
element = input(f"Enter element {n+1}: ")
if element.isnumeric():
x.append(int(element))
n += 1
else:
print("Please enter numbers only.")
# Part b)
print("x = [", end = "")
for i in range(len(x)):
print(x[i], end = "")
if i < len(x) - 1:
print(", ", end = "")
print("]")
# Part c)
# Calculate sum of all elements
x_sum = sum(x)
print(f"The sum is: {x_sum}")
# Part d)
min_element = x[0]
# Iterate elements in x, starting at the second element
for i in range(1, len(x)):
if x[i] < min_element:
min_element = x[i]
print(f"The minimum element is: {min_element}")
# Part e)
max_element = x[0]
# Iterate elements in x, starting at the second element
for i in range(1, len(x)):
if x[i] > max_element:
max_element = x[i]
print(f"The maximum element is: {max_element}")
# Part f)
for i in range(1, len(x)):
if x[i] < 10: # element is less than 10
x[i] += 5
# Now print
print("x = [", end="")
for i in range(len(x)):
print(x[i], end="")
if i < len(x) - 1:
print(", ", end="")
print("]")
2. Write a Python program that uses a while loop and an if/else structure to enter a given number of grades then print how many grades there are in the range 90-100 and in the range 80-89 and so on. An example run would be:
Enter number of grades: 5
Enter grade 1: 83
Enter grade 2: 91
Enter grade 3: 86
Enter grade 4: 65
Enter grade 5: 82
The output will be: Range 90-100: 1 Range 80-89: 3 Range 70- 79: 0
Write a Python program that calculates the summation ∑_(n=0)^100▒(3n+1)/(5n^2+2)and prints its value on the screen.
# Ask for number of grades
n = int(input("Enter number of grades: "))
grades_A = 0 # grades between 90-100
grades_B = 0 # grades between 80-89
grades_C = 0 # grades between 70-79
for i in range(n):
grade = int(input(f"Enter grade {i+1}: "))
if grade >= 90 and grade <= 100:
grades_A += 1
elif grade >= 80 and grade <= 89:
grades_B += 1
elif grade >= 70 and grade <= 79:
grades_C += 1
# Now print
print(f"Range 90-100: {grades_A}")
print(f"Range 80-89: {grades_B}")
print(f"Range 70-79: {grades_C}")