Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
132 views

Python Functions Exercise

The document contains 7 exercises to practice Python functions: 1. Create a function that takes name and age and prints them. 2. Create a function that accepts a variable number of arguments and prints them. 3. Create a function that returns multiple values - addition and subtraction of inputs. 4. Create a function with a default argument so it can be called with or without that argument. 5. Create nested functions - an inner function inside an outer function to calculate addition. 6. Create a recursive function to calculate the sum from 0 to a given number. 7. Assign a new name to an existing function and call it using the new

Uploaded by

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

Python Functions Exercise

The document contains 7 exercises to practice Python functions: 1. Create a function that takes name and age and prints them. 2. Create a function that accepts a variable number of arguments and prints them. 3. Create a function that returns multiple values - addition and subtraction of inputs. 4. Create a function with a default argument so it can be called with or without that argument. 5. Create nested functions - an inner function inside an outer function to calculate addition. 6. Create a recursive function to calculate the sum from 0 to a given number. 7. Assign a new name to an existing function and call it using the new

Uploaded by

Mateo Hysa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Functions Exercise Write a program to create function 

calculation() such that it can accept two


variables and calculate addition and subtraction. Also, it must return both addition
and subtraction in a single return call.
Exercise 1: Create a function in Python
Exercise 4: Create a function with default argument
Write a program to create a function that takes two arguments, name and age, and
print their value. Write a program to create a function show_employee() using the following
conditions.
Exercise 2: Create a function with variable length of arguments
 It should accept the employee’s name and salary and display both.
Write a program to create function func1() to accept a variable length of arguments
and print their value.  If the salary is missing in the function call then assign default value 9000
to salary
Note: Create a function in such a way that we can pass any number of arguments
to this function and the function should process them and display each argument’s Exercise 5: Create an inner function to calculate the addition in the following way
value.
Create an outer function that will accept two parameters, a and b
Function call:
Create an inner function inside an outer function that will calculate the addition of
# call function with 3 arguments a and b
func1(20, 40, 60) At last, an outer function will add 5 into addition and return it
# call function with 2 arguments Exercise 6: Create a recursive function
func1(80, 100) Write a program to create a recursive function to calculate the sum of
numbers from 0 to 10.
Expected Output:
A recursive function is a function that calls itself, again and again.
Printing values
Expected Output:
20
55
40
Exercise 7: Assign a different name to function and call it through the new name
60
Below is the function display_student(name, age). Assign a new
Printing values
name show_tudent(name, age) to it and call it using the new name.
80

100

Exercise 3: Return multiple values from a function


Solutions show_employee("Ben", 12000) display_student("Emma", 26)

Exercise 1 show_employee("Jessa") showStudent = display_student

def demo(name, age): Exercise 5 showStudent("Emma", 26)

print(name, age) def outer_fun(a, b):

demo("Ben", 25) square = a ** 2

Exercise 2 def addition(a, b):

def func1(*args): return a + b

for i in args: add = addition(a, b)

print(i) # add 5 to the result

return add + 5

func1(20, 40, 60) result = outer_fun(5, 10)

func1(80, 100) print(result)

Exercise 3 Exercise 6

def calculation(a, b): def addition(num):

addition = a + b if num:

subtraction = a - b return num + addition(num - 1)

return addition, subtraction else:

res = calculation(40, 10) return 0

print(res) res = addition(10)

Exercise 4 print(res)

def show_employee(name, salary=9000): Exercise 7

print("Name:", name, "salary:", salary) def display_student(name, age):

print(name, age)

You might also like