0% found this document useful (0 votes)
1 views8 pages

Python Exercises Final

The document contains Python exercises for a student named Barua Labonno, focusing on four programming tasks. The tasks include checking for leap years, assigning letter grades based on numerical scores, calculating the factorial of a number, and summing the squares of a list of numbers. Each task is accompanied by example inputs and outputs, along with the corresponding Python code solutions.

Uploaded by

barualabonno165
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)
1 views8 pages

Python Exercises Final

The document contains Python exercises for a student named Barua Labonno, focusing on four programming tasks. The tasks include checking for leap years, assigning letter grades based on numerical scores, calculating the factorial of a number, and summing the squares of a list of numbers. Each task is accompanied by example inputs and outputs, along with the corresponding Python code solutions.

Uploaded by

barualabonno165
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
You are on page 1/ 8

NAME: BARUA Labonno

ID: 24113453d

Python Exercises

Question 1: (25 marks)

Write a program that checks if a given year is a leap year.

Rules for determining a leap year:

Divisible by 4: A year is a leap year if it is divisible by 4.

Divisible by 100: However, if the year is divisible by 100, it is


not a leap year, unless:

Divisible by 400: If the year is also divisible by 400, then it is a


leap year.

According to these rules, 2020 is a leap year (divisible by 4), 1900


is not a leap year (divisible by 100 but not by 400), 2000 is a leap
year (divisible by 400).

#Two examples of input and output are illustrated below:

Enter a year: 1900

1900 is not a leap year.

Enter a year: 2000

2000 is a leap year.

Answer:

year = int(input("Enter a year: "))

if year % 400 == 0:
print(f"{year} is a leap year.")

elif year % 100 == 0:

print(f"{year} is not a leap year.")

elif year % 4 == 0:

print(f"{year} is a leap year.")

else:

print(f"{year} is not a leap year.")

Enter a year: 1988

1988 is a leap year.

Enter a year: 2001

2001 is not a leap year.


Question 2: (25 marks)

Write a program that assigns a letter grade based on a numerical


score (0-100) entered by the user. Use the following scale:

 A: 90-100

 B: 80-89

 C: 70-79

 D: 60-69

 F: Below 60

 #An example of input and output is illustrated below:

 Enter your score: 75

 Your grade is: C

Answer:

score = int(input("Enter your score: "))

if score >= 90:

grade = "A"

elif score >= 80:

grade = "B"
elif score >= 70:

grade = "C"

elif score >= 60:

grade = "D"

else:

grade = "F"

print(f"Your grade is: {grade}")

Enter your score: 65

Your grade is: D

Question 3: (25 marks)

Write a Python function named “factorial” that takes an integer


as a parameter and returns the factorial of this integer if it is
positive. If this integer is nonpositive, the function should print
“Error! The input must be positive.”
# Example:

# factorial(4) should return 24 (4!=1*2*3*4)

# factorial(-3) should print “Error! The input must be positive.”

ANSWER:

def factorial(n):

if n < 0:

print("Error! The input must be positive.")

else:

result = 1

for i in range(1, n +1):

result *= i

return result

factorial(8)

40320
factorial(-6)

Error! The input must be positive.

Question 4: (25 marks)

Write a Python function named “sum_of_squares” that takes a list


as input and returns the sum of the squares of all numbers in the
list.

#Example:

# sum_of_squares([1, 3, 5]) should return 35 (1^2 + 3^2 + 5^2)

# sum_of_squares([4, 6]) should return 52 (4^2 + 6^2)

Answer:
def sum_of_squares(lst):

total = 0

for num in lst:

total += num ** 2 # Square each number and add to total

return total

sum_of_squares([8,4])

80

sum_of_squares([0, 2, 4])

20

You might also like