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

Lab 9

The document outlines the instructions and objectives for Lab 9 focused on functions, tuples, and dictionaries in programming. It includes specific tasks for writing functions to calculate the day of the year, modifying tuples and dictionaries, and checking for prime numbers. Additionally, it provides expected outputs and a scoring rubric for the lab activities.

Uploaded by

4jgsnyswmr
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)
20 views6 pages

Lab 9

The document outlines the instructions and objectives for Lab 9 focused on functions, tuples, and dictionaries in programming. It includes specific tasks for writing functions to calculate the day of the year, modifying tuples and dictionaries, and checking for prime numbers. Additionally, it provides expected outputs and a scoring rubric for the lab activities.

Uploaded by

4jgsnyswmr
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 : Khadega Alaskari ID: 60098609

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):


if year < 1582 or month < 1 or month > 12:
return None
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
res = days[month - 1]
if month == 2 and is_year_leap(year):
res = 29
return res

def day_of_year(year, month, day):


if day < 1 and month <= 12 :
return None
total_days= day
for m in range(1,month):
total_days += days_in_month(year,m)
return total_days

print(day_of_year(2023, 1, 1))
print(day_of_year(2020, 3, 1))
print(day_of_year(2023, 3, 1))
print(day_of_year(2000, 12, 31))
print(day_of_year(2023, 12, 31))

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"
…………………………
…………………………
…………………………
print(len(tuple4))
print(tuple4)
print(tuple5)
print(tuple6)
tuple1 = ("a", "b", "c")
tuple2 = "d", "e", "f"
tuple3 = "h", "i", "g"
tuple4=tuple1+(10,20,30)
tuple5=2*tuple2
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",
}
…………
…………
…………

Your code

Expected output:

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


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

PART IV (if time)

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.

Your code

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:

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