2024-12-19
Max. 100 points
Name:
| Task | Max. | Achieved |
|---|---|---|
| 1 | 20 | |
| 2 | 30 | |
| 3 | 25 | |
| 4 | 25 | |
| Sum | 100 |
| Statement | True | False |
|---|---|---|
| Python sets are mutable. | ||
sys.exit(.) reports to the operating system
whether execution was successful or not. | ||
| The "ifmain" pattern improves program performance when executed. | ||
The ipython shell allows interactive execution
of Python code. | ||
In Python, int is mutable. | ||
for loops are used for repeated execution
as long as an expression is true. | ||
| Python modules are essentially files with Python code. | ||
PYTHONPATH is used to tune performance of
Python programs. |
t = ('a', 'b', 'c', )
print(t.append('d'))
def sum(a, b, c):
return a + b
sum(4, 2, 3)
def area(length, width):
return length + width
print(area(5, 2))
l = [4, 3, 2]
print(l.sort())
s = 'Hello World!'
s = s.lower()
print(s)
print({1, 1, 1})
is_leap(year: int) -> bool.
Given a year as a number, report if it is a leap year. A leap year occurs:
is_leap(1981) returns False,
is_leap(1984) returns True,
is_leap(1900) returns False while
is_leap(2000) returns True.
Document your function with a docstring.
(25 points)
def is_leap(year):
"""Return True if the given year is a leap year, otherwise false."""
if year % 400 == 0:
return True
if year % 100 == 0:
return False
if year % 4 == 0:
return True
return False
sumproduct(first: iterable, second: iterable)
in Python that takes two sequences of numbers with equal length and returns
the sum of the product of the numbers.sumproduct((1, 3, 4), (9, 2, 3)) ⇒ 1*9 + 3*2 + 4*3 = 27;
sumproduct([3, 5], [2, 4]) ⇒ 26].
Add a proper docstring and a doctest to receive full points.def sumproduct(first, second):
"""
Return the sum of the products of `zip(first, second)`.
>>> sumproduct((1, 3, 4), (9, 2, 3))
27
"""
sum = 0
for i, j in zip(first, second):
sum += i * j
return sum