lab report 9
lab report 9
AETN2302 Applied
Programming 1
Lab Report
Lab Number: Lab 9
Title of the Lab: “Functions, Tuples, Dictionaries”
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
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.
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.
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
Test data:
Input: Day =1, Month =1, 2023,
Output: 1
Input: Day =1, Month =3, 2020,
Output: 61
Part II
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.
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')
Part III
Write in words and/or a flow chart what you want each section of your code to do
Expected output:
Part IV
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
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
if(num % i) == 0:
return False
else:
return False
return True
if is_prime(i + 1):
print()
print(is_prime(25))
Expected output
False
Second code:
Expected output
2 3 5 7 11 13 17 19
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.