name = input("Enter your name: ")
print("Hello, " + name + "!")
Lists
- Create a list called fruits
- Print the list using a print statement
fruits = ["apple", "banana", "cherry"]
print("List of fruits:", fruits)
Dictionaries
- Create a dictionary called student with information about a student
- Print the info about the student by accessing the info from the database
student = {"name": "John", "age": 20, "grade": "A"}
print("Student name:", student['name'], "\nStudent age:", student['age'],"\nStudent grade:", student['grade'])
Iteration (For Loops)
- Using a for loop and the range function, print “iteration” + the number of the iteration
- The range function will stop after the 5th iteration because it doesn’t include the last number in the range
for i in range(1, 6):
print("Iteration", i)
Mathematical Analysis
- Define a function that takes in a list (numbers) and gets the average of the list by dividing the sum of the list by the length
- Return the average
def calculate_average(numbers):
return sum(numbers) / len(numbers)
numbers = [12, 24, 36, 48, 60]
average = calculate_average(numbers)
print("Average:", average)
Mathematical Analysis Part 2 (If/Else Statement)
- Get an input number and modulo the number by 2
- If the result is 0, the number is even, so return (num + “is even”)
- If the result isn’t 0, the number is odd, so return (num + “is odd”)
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even.")
else:
print(num, "is odd.")
Recursion
- Define a function that gets a number and returns the factorial of the number
- By using recursion, you can multiply the input number with every number (down to 0) that has a difference of 1
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
number = int(input("Enter a number to calculate its factorial: "))
result = factorial(number)
print("Factorial of", number, "is", result)
Combination of all the concepts from above
- First function
- Add a student to keep track of (get name and age)
- Ask the user if they want to add more students or not
- If the user wants more students, use recursion to call the function again (adds another student)
- If the user doesn’t want more students, exit the function
- Second function
- Utilize dictionaries to keep track of the grades of all students
- Use iteration to go through each student in the student list
- Get the grade of a student
- If the user wants more grade points, loop back to the code and add another grade point
- At the end, divide the total grade by the amount of iterations performed to get the total grade
- Repeat for each student in the students list
- Use iteration to go through each student in the student list
- Utilize dictionaries to keep track of the grades of all students
- Third function
- Print the details of each student
- Get the name, age, and score of each student
- Get name/age from the students list and the ages list
- Get the grade of the students by indexing the grades dictionary with the student name
- Get the name, age, and score of each student
- Print the details of each student
- Fourth function
- Calculate the average grade of all the students
- Get the sum of all the student grades
- Divide by # of students
- Calculate the average grade of all the students
- Fifth function
- Assess the performance of the “class” by assessing the grade average
# Get all students and their age via recursion
def get_user_input(students=None, ages=None):
if students is None:
students = []
ages = []
# Get student info
student = input("Enter student name: ").lower()
age = int(input("Enter student age: "))
students.append(student.capitalize())
ages.append(age)
# Recursion to add more students if desired
keep_going = input('Would you like to add another student? [Y/N]').lower()
if keep_going == 'y':
return get_user_input(students, ages)
else:
return students, ages
# Add grades to each student by using a dictionary
def create_student_grades(students):
keep_going = 'y'
grades = {}
# Iterate through each student in the student list with for loop
for student in students:
grades[student] = 1
iteration = 0
while keep_going == 'y':
iteration += 1
grade = int(input(f"Enter {student}'s grade: "))
grades[student] += grade
keep_going = input('Would you like to add another grade? [Y/N]').lower()
grades[student] = grades[student] / iteration
keep_going = 'y'
return grades
# Print the student details using iteration
def print_student_details(students, grades, ages):
age = [age for age in ages]
index = -1
for student in students:
index += 1
print(f"{student}: Age - {age[index]}, Grade - {grades[student]}")
# Get the sum of the grades of all students
def calculate_average_grade(grades):
total_grade = sum(grades.values())
num_students = len(grades)
average_grade = total_grade / num_students
return average_grade
# Assess how well the class average is
def assess_performance(average_grade):
if average_grade >= 90:
return "Excellent"
elif average_grade >= 80:
return "Good"
elif average_grade >= 70:
return "Satisfactory"
else:
return "Needs Improvement"
# Run all functions in one big main function
def student_grader():
students, ages = get_user_input()
grades = create_student_grades(students)
print("\nStudent Details:")
print_student_details(students, grades, ages)
average_grade = calculate_average_grade(grades)
print(f"\nAverage Grade: {average_grade}")
performance = assess_performance(average_grade)
print(f"\nOverall Class Performance: {performance}")
if __name__ == "__main__":
student_grader()
Enter student name: fda
Enter student age: 45
Would you like to add another student? [Y/N] y
Enter student name: asd
Enter student age: 44
Would you like to add another student? [Y/N] n
Enter Fda's grade: 95
Would you like to add another grade? [Y/N] n
Enter Asd's grade: 100
Would you like to add another grade? [Y/N] n
Student Details:
Fda: Age - 45, Grade - 96.0
Asd: Age - 44, Grade - 101.0
Average Grade: 98.5
Overall Class Performance: Excellent