Programming in Python Lab 6
LAB 6: FUNCTION IN PYTHON
Instructions:
• You are to code the following programs in the lab and show the output to your lecturer.
• Write comment to make your programs readable.
• Use descriptive variables in your program (Name of the variables should show their purposes)
• Note: Design the solution using pseudocode before coding
Part A: Function
Function with variable
1. Write a Python program that defines a function to accept a variable number of arguments.
The function should print each argument received. Use the concept of variable-length
arguments (*args) to handle an arbitrary number of inputs.
Return multiple values from a function
2. Write a program to create function calc() that will accept two variables and calculate the sum
and difference of the two variables. Hint: Use addition and subtraction.
Complete the given code below:
def calc(x, y):
# Write the missing Code
result = calc(40, 10)
print(result)
Function with a default argument
3. Write a program to create a function named employee() using the following conditions:
a. Program should accept the employee’s name and salary and display both.
b. If the salary is not provided when calling the function, assign a default value of 9000 to it.
Inner function to calculate the addition
4. Write a Python program to create the following:
a. Create an outer function that will accept two parameters, y and z.
b. Create an inner function inside an outer function that will calculate the addition of y and z.
c. Lastly, the outer function will add 5 into addition and return it
Assign a different name to function and call the function using the new name.
5. Based on the example given, assign a new name to the function and call it using the new name.
def student(name, age):
print(name, age)
student("Kelvin", 26)
Built-in Function
6. Generate a Python list of all the even numbers between 2 to 50.
7. Find the largest number from the given list [4, 28, 97, 56, 16].
Programming in Python Lab 6
Part B: Test yourself!
1. Maximum of Two Values:
Write a function named maxtwo that accepts two integer values as arguments and returns the
value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the
function, the function should return 12. Use the function in a program that prompts the user to
enter two integer values. The program should display the value that is the greater of the two.
2. You have decided to program a simple calculator. You calculator can perform the following
operations:
a. It can multiply two numbers.
b. It can divide one number by another.
c. It can get the remainder of two numbers.
d. It can subtract one number from another.
Write a function that asks the user for two numbers, and the operation they wish to perform on
these two numbers and return the result. Note: Choices should be entered as strings.
3. Write a function that accepts a list of strings and sorts them alphabetically (You may use the sorted
function in the function).
4. Optional: Write a Python program to read the name of student, the TP Number and enter his/her
subject marks which should be stored in a list. Compute the total, the average percentage of a
student's marks and the grade. Finally display the Name of the student, TP Number, Total,
Percentage, and Grade of that semester using the grading criteria below.
Score Grade
80-100 A+
75-79 A
70-74 B+
65-69 B
60-64 C+
55-59 C
50-54 C-
40-49 D
0-39 F
a) Implement a mark function to accept the list of subject marks as a parameter and return
the total marks.
b) Implement an average function to compute and return the average (percentage) of the
total marks.
c) Implement a grade function that takes the average (percentage) as a parameter and
returns the corresponding grade based on the grading criteria above.
d) Implement a display function to print the student's name, TP number, total marks,
percentage, and grade.