0% found this document useful (0 votes)
61 views2 pages

Python Functions Exercises for Beginners

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
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)
61 views2 pages

Python Functions Exercises for Beginners

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

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)

Common questions

Powered by AI

Variable-length argument functions in Python offer flexibility by automatically handling multiple inputs, reducing the need to manually parse or handle varying numbers of arguments. This enhances maintainability by ensuring that code can dynamically adapt to different input sizes, minimizes errors related to argument handling, and centralizes logic for iterating over arguments, allowing for changes without extensive refactoring .

Python handles functions with a variable number of arguments using the *args syntax, allowing them to accept and process any number of positional arguments. This is significant compared to regular functions, which require a fixed number of arguments, as it provides flexibility, enabling functions to handle dynamic inputs and enhancing their reusability .

Recursive functions can simplify iterative processes by reducing boilerplate code and leveraging the function call stack for iteration, inherently managing state without explicit loops. In the context of calculating the sum of numbers from 0 to 10, recursion substitutes for an explicit loop, making the code cleaner and more intuitive by directly expressing the mathematical summation as a series of reductions to simpler instances of the same problem .

A Python function can return multiple values by separating them with commas in the return statement. This is useful in scenarios such as mathematical operations where multiple results are computed at once, like returning both the sum and difference of two numbers. For instance, a calculation function can accept two numbers and return their addition and subtraction in a single call .

Yes, you can rename a function reference in Python by assigning it to another variable, creating an alias. This feature might be used to improve code readability, align with naming conventions, or adapt existing code to new requirements without altering the original function. For instance, renaming display_student to showStudent allows a change in function interface gracefully without affecting the function's implementation or past usage .

Inner functions facilitate encapsulation by restricting the scope of the inner function's logic to its containing outer function, enhancing modularity and reducing namespace pollution. Their potential benefit is demonstrated in scenarios like an outer function performing a complex operation where the inner function handles a logical unit like addition, contributing to keeping the outer function clearer and focusing on high-level logic, such as adding an extra operation post-calculation .

Default arguments in Python functions allow for function calls with fewer arguments, providing flexibility and simplifying the function's interface. They initial values in the absence of explicit ones. For example, in the function show_employee, the salary parameter defaults to 9000 if not provided, allowing callers to omit it when the salary is not relevant or should use a standard value, streamlining the code and reducing errors .

Function scope in Python dictates the visibility of variables and functions, wherein inner functions restrict scope to their enclosing outer function. This promotes encapsulation and information hiding by preventing external code from accessing or modifying the inner function's logic and variables directly, maintaining a clean namespace and protecting the inner workings of function implementations. Encapsulation is enhanced as internal details remain hidden while exposing only necessary interfaces .

Adding fixed values within a function provides a way to adjust results in a fixed manner, facilitating a consistent operation modification. Using an inner addition function, the outer function can consistently implement additional logic after calculating primary operations. In the provided example, adding 5 to the result of an addition within the outer function demonstrates fixed value enhancement to integrate specific business logic or computational requirement upon the computed operation .

A recursive function is a function that calls itself repeatedly until a base condition is met. In Python, it can be used to calculate the sum of numbers from 0 to 10 by defining a function that adds the current number to the result of itself called with the next lower number. This continues until the number reaches zero, at which point the recursion ends and the accumulated sum is returned .

You might also like