Lab Manual - AETN2302 - L9 - V2
Lab Manual - AETN2302 - L9 - V2
1. If you arrive later than 10 minutes after the lab started, you will not be allowed to
do the lab.
3. There is no time allocated to redo any missed labs.
4. Absence from labs will result in a grade of zero unless a written medical excuse is
provided.
OBJECTIVES
1. Programming based on functions;
2. Calling functions
EQUIPMENT
1. Pcs
2. Sandbox
PART I
Scenario: From the previous lab, remember that we wrote a function that receives month and
year as its input and outputs the number of days in that month. In this lab, we use that function
and write a function that takes three arguments (a year, a month, and a day of the month) and
returns the corresponding day of the year or returns None if any of the arguments is invalid.
Your code
def is_year_leap(year):
if year % 4 !=0:
return False
elif year%100!=0:
return True
elif year %400!=0:
return False
else:
return True
def days_in_month(year, month):
months = [31,28,31,30,31,30,31,31,30,31,30,31]
if is_year_leap(year) and month ==2:
return 29
return months[month-1]
def day_of_year(year,month,day):
if month > 12:
return None
elif day > 31:
return None
total = day
month = month-1
while month >=1:
total+= days_in_month(year,month)
month-=1
return total
print(day_of_year(2023, 1, 1))
print(day_of_year(2020, 3, 1))
print(day_of_year(2023, 3, 1))
Test data:
Input: Day =1, Month =1, 2023,
Output: 1
Input: Day =1, Month =3, 2020,
Output: 61
PART II
Scenario: Modify the given code to give the desired output below. Do not directly define
tuples 4, 5, 6. Generate them based on the existing tuples.
Expected output
6
('a', 'b', 'c', 10, 20, 30)
('d', 'e', 'f', 'd', 'e', 'f')
('h', 'i', 'g')
PART III
Scenario: Modify the given dictionary to give the desired output.
1- Add a new element, Adem:1234.
2- Delete the first element.
3- Show the dictionary.
4- Chang the element: Adem:1234, to Adem:aaaa
5- Add a new element: John: abcd.
6- Show the dictionary.
7- Delete the last element.
8- Show the dictionary.
dictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
…………
…………
…………
dictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
dictionary["adem"]=1234
del dictionary["key1"]
print(dictionary)
dictionary["adem"]="aaa"
dictionary["john"]="abcd"
print(dictionary)
dictionary.popitem()
print(dictionary)
Expected output:
PART IV
Scenario: A natural number is prime if it is greater than 1 and has no divisors other than 1 and
itself. Complicated? Not at all. For example, 8 isn't a prime number, as you can divide it by 2 and
4 (we can't use divisors equal to 1 and 8, as the definition prohibits this). On the other hand, 7 is
a prime number, as we can't find any legal divisors for it. Your task is to write a function
checking whether a number is prime or not.
The function is called is_prime; takes one argument (the value to check), returns True if the
argument is a prime number, and False otherwise.
Hint: try to divide the argument by all subsequent values (starting from 2) and check the
remainder - if it's zero, your number cannot be a prime; think carefully about when you should
stop the process.
def is_prime(num):
if num <= 1:
return False
for i in range(2, num):
if num%i == 0:
return False
return True
Once you make sure your function is working, we want to print all prime numbers smaller than
20 using the above function. Run code below:
def is_prime(num):
if num <= 1:
return False
for i in range(2, num):
if num%i == 0:
return False
return True
Expected output
2 3 5 7 11 13 17 19
Suggested Scoring Rubric
Possible Earned
Activity Section Points Points