0% found this document useful (0 votes)
2 views

lab report 9

The lab report details the objectives and outcomes of Lab 9, focusing on functions, tuples, and dictionaries in programming. It covers the creation and manipulation of functions for date calculations, tuple generation, dictionary operations, and prime number checking. The overall experience emphasizes the importance of functions in organizing code and solving problems effectively.

Uploaded by

4jgsnyswmr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lab report 9

The lab report details the objectives and outcomes of Lab 9, focusing on functions, tuples, and dictionaries in programming. It covers the creation and manipulation of functions for date calculations, tuple generation, dictionary operations, and prime number checking. The overall experience emphasizes the importance of functions in organizing code and solving problems effectively.

Uploaded by

4jgsnyswmr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

End of document

AETN2302 Applied
Programming 1
Lab Report
Lab Number: Lab 9
Title of the Lab: “Functions, Tuples, Dictionaries”

Name of the Author: Khadega Alaskari


Name of Instructor: Amith
Date the Lab was started: 13/6/2024
Due Date for this Report: 15/6/2024

Mark Composition
Report 25
Code 50
Program Verification 25
Total 100
Lab Introduction:

Add a paragraph, explaining what is the overall goal for this lab (consider all part of the lab as
a whole)

This lab aims to teach two main things: how to organize code using functions and how to use these
functions in a program. The idea is to break down the code into smaller, reusable parts, making it easier
to understand and maintain. We learn not only how to create these functional units but also how to
make them work together by calling them in their programs. Overall, the goal is to lay the groundwork
for good programming habits and understanding the basics of structuring code.

Part I

Explain briefly about the goals of this part

In this lab section, the goal is to extend a previous function that calculates the days in a month. Three
functions are introduced: `is_year_leap` checks if a given year is a leap year, `days_in_month` calculates the
days in a month considering leap years, and `day_of_year` computes the day of the year based on inputs for
year, month, and day. The latter function validates inputs and returns None for invalid cases. The lab
emphasizes practical applications of functions in date calculations, using test data to illustrate the
functionality, including examples with specific day, month, and year values and their corresponding
expected outputs.

Plan the code:

Write in words and/or a flow chart what you want each section of your code to do
1. Input the year, month, and day
2. If the month is more than 12 or the day is more than 31, stop
3. Set Total as the day
4. Reduce the month by 1
5. While the month is greater than or equal to 1
6. Add days_in_month(year, month) to Total
7. Reduce the month by 1
8. Print the final Total
9. End of the program.

Include the code you developed

def is_year_leap(year):
if year<1582:
return None
elif 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 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(2000, 12, 31))

Include some input/output test data

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

Reflect on your work:

What errors did you come across? no


How did you overcome these errors?
What new technique (function, operator, or approach) did you use?
Any other comments?

Part II

Explain briefly about the goals of this part


In Part II of the lab, the objective is to modify the given code to generate specific tuples
(`tuple4`, `tuple5`, and `tuple6`) based on existing tuples (`tuple1`, `tuple2`, and `tuple3`)
without directly defining the new tuples. The modifications involve concatenating `tuple1`
with a numeric tuple (`b`) for `tuple4`, repeating `tuple2` twice for `tuple5`, and assigning the
values of `tuple3` to `tuple6`.

Plan the code:

Write in words and/or a flow chart what you want each section of your code to do
1. Create tuple1 with values "a", "b", "c"
2. Create tuple2 with values "d", "e", "f"
3. Create tuple3 with values "h", "i", "g"
4. Create tuple4 by adding values from tuple1 and (10, 20, 30)
5. Create tuple5 by adding values from tuple2 and tuple2
6. Create tuple6 by copying values from tuple3
7. Print the length of tuple4
8. Print the values of tuple4
9. Print the values of tuple5
10. Print the values of tuple6
11. End of the program.

Include the code you developed

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


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

print(len(tuple4))
print(tuple4)
print(tuple5)
print(tuple6)
Include some input/output test data

Expected output:

6
('a', 'b', 'c', 10, 20, 30)
('d', 'e', 'f', 'd', 'e', 'f')
('h', 'i', 'g')

Reflect on your work:

What errors did you come across? no


How did you overcome these errors?
What new technique (function, operator, or approach) did you use? Creating a new tuple from
existing tuples.
Any other comments?

Part III

Explain briefly about the goals of this part


In Part III of the lab, the task is to make changes to a given dictionary to achieve a specific
desired output. The steps involve adding and deleting elements, changing values for specific
keys, and displaying the dictionary after each change. The code initializes a dictionary,
performs these operations (e.g., adding "Adam" with the value "1234," deleting the first
element, changing the value for "Adam," adding "John" with the value "abcd," and deleting
the last element), and prints the dictionary's state at different points.

Plan the code:

Write in words and/or a flow chart what you want each section of your code to do

1. Make a dictionary with some stuff inside.


2. Put "Adam" in there with the number 1234.
3. Take out the thing labeled "key1".
4. Show what's in the dictionary now.
5. Change the number for "Adam" to 'aaaa'.
6. Add "John" with 'abcd'.
7. Show the updated dictionary.
8. Remove the last thing added.
9. Show the final dictionary.
10. End of the program.

Include the code you developed

dictionary = {"key1": "value1","key2": "value2","key3": "value3"}


dictionary['Adam'] = '1234'
del dictionary['key1']
print(dictionary)
dictionary['Adam'] = 'aaaa'
dictionary['John'] = 'abcd'
print(dictionary)
dictionary.popitem()
print(dictionary)
Include some input/output test data

Expected output:

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


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

Reflect on your work:

What errors did you come across? no


How did you overcome these errors?
What new technique (function, operator, or approach) did you use? dictionary.popitem()
Any other comments?

Part IV

Explain briefly about the goals of this part

In Part IV of the lab, the goal is to create a function named `is_prime` that determines whether
a given number is a prime number. The function returns `True` if the number is prime and
`False` otherwise. The code iterates through potential divisors starting from 2 to the given
number and checks for divisibility. The second part of the code uses this function to print all
prime numbers smaller than 20. The lab emphasizes understanding and implementing a prime-
checking function, showcasing its application in identifying prime numbers within a specified
range. The expected output includes a list of prime numbers up to 20, demonstrating the
successful implementation of the `is_prime` function.
Plan the code:

Write in words and/or a flow chart what you want each section of your code to do

1. Create a function is_prime(num) to check if a number is prime.


- If the number is more than 1:
- Start with divisor as 2.
- Go through each number from 2 to num - 1:
- If num is divisible by the current number, it's not prime; return False.
- If no divisor is found, it's prime; return True.
- If the number is 1 or less, it's not prime; return False.

2. Print whether 25 is a prime using is_prime(25).

3. For each number i from 1 to 19:


- If is_prime(i + 1) is True, print i + 1.

4. Print whether 25 is a prime again using is_prime(25).


5. End of the program.

Include the code you developed


First code:

def is_prime(num):
if (num> 1):
divisor = 2
for i in range (divisor, num):
if(num % i) == 0:
return False
else:
return False
return True
print(is_prime(25))

Second code:
def is_prime(num):

if (num> 1):
divisor = 2

for i in range (divisor, num):

if(num % i) == 0:

return False

else:

return False

return True

for i in range(1, 20):

if is_prime(i + 1):

print(i + 1, end=" ")

print()

print(is_prime(25))

Include some input/output test data


First Code:

Expected output

False

Second code:

Expected output
2 3 5 7 11 13 17 19

Reflect on your work:

What errors did you come across? no


How did you overcome these errors?
What new technique (function, operator, or approach) did you use?
Any other comments?

Lab Summary and conclusion:

Add a paragraph, summarizing the overall experience and observations in this lab (consider all
parts of the lab as a whole)

In this lab, we focused on two main objectives: programming with functions and calling
functions. Throughout the different parts, we applied these concepts practically. Part I
involved calculating the day of the year using a function. Part II dealt with manipulating tuples
without directly defining new ones. In Part III, we worked with dictionaries, performing tasks
like adding, deleting, and modifying elements. Lastly, in Part IV, we created a function to
check for prime numbers and used it to find primes in each range. The lab provided a hands-on
experience, demonstrating how functions can be powerful tools for solving various problems
and organizing code effectively. Overall, it offered a well-rounded exploration of
programming with functions and calling them in Python.

You might also like