0% found this document useful (0 votes)
27 views6 pages

Lab Manual - AETN2302 - L9 - V2

This document provides instructions for a lab on functions, tuples, and dictionaries in Python. The lab consists of 4 parts that have students write and modify code to work with functions, tuples, dictionaries, and prime number checking.

Uploaded by

Zille Huma
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)
27 views6 pages

Lab Manual - AETN2302 - L9 - V2

This document provides instructions for a lab on functions, tuples, and dictionaries in Python. The lab consists of 4 parts that have students write and modify code to work with functions, tuples, dictionaries, and prime number checking.

Uploaded by

Zille Huma
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/ 6

End of

Lab 9 – “Functions, Tuples, Dictionaries”


Student Name _Ghanem Alkhaja_ ID _60073120_ Date:

Lab Instructions and Rules

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

Input: Day =1, Month =3, 2023,


Output: 60

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.

Complete the code in the box:

tuple1 = ("a", "b", "c")


tuple2 = "d", "e", "f"
tuple3 = "h", "i", "g"
tuple4 = tuple1 + (10,20,30)
tuple5 = tuple2*2
tuple6 = tuple3
print(len(tuple4))
print(tuple4)
print(tuple5)
print(tuple6)

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:

{'key2': 'value2', 'key3': 'value3', 'Adam': 1234}


{'key2': 'value2', 'key3': 'value3', 'Adam': 'aaaa', 'John': 'abcd'}
{'key2': 'value2', 'key3': 'value3', 'Adam': 'aaaa'}

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.

Complete the code in the editor.

def is_prime(num):
if num <= 1:
return False
for i in range(2, num):
if num%i == 0:
return False
return True

print(is_prime(25)) # Output: False


print(is_prime(53)) # Output: 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

for i in range(1, 20):


if is_prime(i + 1):
print(i + 1, end=" ")

Expected output

2 3 5 7 11 13 17 19
Suggested Scoring Rubric

Possible Earned
Activity Section Points Points

Part 1: Program setup 25


Part 2: Code 50
Part 3: output verification 25
Total Packet Tracer Score 100

End of document REFERENCES: Cisco Network academy

You might also like