In: Computer Science
Python
#Exercise 1
#Assign the following first names to a variable name as a list:
peter,paul,mary
#Assign the following last names to a variable name as a list:
ng,lo,lau
#Display each first name in a separate line in turn so that your
screen shows (ignore the #):
#peter
#paul
#mary
#Combine the elements in the list in turn so that you display the
full names of each person in separate lines
#Output should look like the following (ignore the #):
#peter ng
#paul lo
#mary lau
#Exercise 2
#Assign the following grades to a variable name as a list:
80,90,100,70
#Use the sum() function to total up the values in the list and
assign the sum to a variable
#Use the len() function to get the length of the list and assign
the length to a variable
#Compute the average grade using the sum and length of the list and
assign the average to a variable
#Convert the list length, list sum, and average to strings
#Display the results of your calculation in the following format
using the + concatenator
#Your total score for [length of list] grades is [sum of list]
resulting in an average of [average grade]!
#Ask the user for a grade using the prompt: New grade? (include a
space after the ?)
#Convert the new grade into an integer
#Add the new grade to the end of the existing list using the append
method
#Remove the second grade in the list using the pop method
##Use the sum() function to total up the values in the list and
assign the sum to a variable
#Use the len() function to get the length of the list and assign
the length to a variable
#Compute the average grade using the sum and length of the list and
assign the average to a variable
#Display the results of your calculation in the following format
using the + concatenator
#Your total score for [length of list] grades is [sum of list]
resulting in an average of [average grade]!
#Exercise 3
#Ask the user for a student's name using the propmt: Student Name?
(include a space after the ?)
#Assign the following grades to a variable name as a list:
80,90,70,85
#Assign the following requirement codes to a variable name as a
list: HW,MT,IC,HW
#Ask the user for a new requirement code using the prompt:
Requirement Code? (include a space after the ?)
#Ask the user for a new grade using the prompt: Grade? (include a
space after the ?)
#Add the new requirement code to the requirement code list using
the append method
#Convert the new grade to an integer
#Add the new grade to the end of the list of grades using the
append method
#Display the following: Grades for: [student name -- input from
user in first instruction of Exercise 3]
#Combine the information from both lists to display each pair of
requirement code and grade in a separate line
#The output should have the following structure:
#[requirement code] [grade]
#For example, the first two lines should read (ignore the #)
#HW 80
#MT 90
#etc until the last items in the lists -- There should be 5 lines
of code-grade pairings
#Get the sum of the grades list using the sum() function
#Get the length of the grades list using the len() function
#Compute the student's average grade using the sum and length
#Convert the average grade to a string
#Use the + concatenator to display the following statement (ignore
the #):
#[student name]: your average grade is [average grade]!
#If student name is George Smith and average grade is 80, output
should be:
#George Smith: your average grade is 80!
Excercise1:
fn=['peter','paul','mary'] //list values of first name
ln=["ng","lo","lau"]//list values of last name
print(*fn,sep="\n")
print(fn[0]+ln[0],fn[1]+ln[1],fn[2]+ln[2],sep="\n")
Excercise 2:
grades = [80,90,100,70]
def print_grades(grades):
for grade in grades:
print (grade)
print_grades(grades)
def grades_sum(scores):
total = 0
for grade in scores:
total += grade
return total
print (grades_sum(grades))
print("Length of grades:",len(grades))
def grades_average(grades):
sums = grades_sum(grades)
return sums / float(len(grades))
print (grades_average(grades))
n=int(input("Enter a new grade? "))
print(n)
grades.append(n)
print(grades)
grades.pop(1)
print(grades)
def print_grades(grades):
for grade in grades:
print (grade)
print_grades(grades)
def grades_sum(scores):
total = 0
for grade in scores:
total += grade
return total
print (grades_sum(grades))
print("Length of grades:",len(grades))
def grades_average(grades):
sums = grades_sum(grades)
return sums / float(len(grades))
print (grades_average(grades))