0% found this document useful (0 votes)
5 views14 pages

7 Python

The document provides an overview of functions in Python, including their definition, advantages like code reusability, and examples of various types such as standard functions, lambda expressions, and higher-order functions like map, filter, and reduce. It also explains how to take user input and the behavior of the input function, as well as different forms of the print function. Additionally, it includes basic exercises for practicing user input and variable manipulation.

Uploaded by

dawasthi952
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

7 Python

The document provides an overview of functions in Python, including their definition, advantages like code reusability, and examples of various types such as standard functions, lambda expressions, and higher-order functions like map, filter, and reduce. It also explains how to take user input and the behavior of the input function, as well as different forms of the print function. Additionally, it includes basic exercises for practicing user input and variable manipulation.

Uploaded by

dawasthi952
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Functions

Functions:
• Function is a set of instruction which is designed to do a
particular task.
• Biggest advantage:- code reusability i.e. write once call every time.
• Type of functions ?
Example:
Calculate the area of circle by taking radius from user?

def area(r):
ans = 3.14*r*r
return ans
R = eval(input("Enter the radius of circle to calculate the area"))
Catch = area(r)
print(catch)
Def keyword
The `def` keyword is used to define a function in Python. It indicates the start
of a function definition and is followed by the function name and a block of
code that is executed when the function is called.
Example:

Output:
Lambda expressions
Lambda expressions, also known as anonymous functions, they are some
functions which don’t have any name and they are used for instant use these
functions are called Anonymous or Lambda functions.
Example:

Output:
map function
The `map` function applies a given function to each item in an iterable (such
as a list) and returns a new iterable with the results.
Example:

Output:
`filter` function
The `filter` function creates a new iterable that includes only the items
from an iterable that satisfy a given condition.
Example:

Output:
Reduce function
• In Python, the reduce() function is a higher-order function from the functools module
that applies a specified function to a sequence of elements, reducing it to a single value.
It repeatedly applies the function to the elements, accumulating the result along the way.
• It is not available by default.
• The output of the reduce function is always one element only.
functools.reduce(function, sequence, initial=None)
Here's a breakdown of the parameters:
•function: The function to be applied. It should take two arguments and return a single
from functools import reduce
value.
•sequence: The sequence of elements to be reduced.
•initial (optional): An initial value to start the reduction. If not provided, the first
element of the sequence will be used as the initial value.
numbers = [1, 2, 3, 4, 5]

sum_result = reduce(lambda x, y: x + y, numbers)


print(sum_result)
Input from user
This function first takes the input from the user and converts it into a string. The type of the returned object
always will be <class ‘str’>. It does not evaluate the expression it just returns the complete statement as String.
When the input function is called it stops the program and waits for the user’s input. When the user presses
enter, the program resumes and returns what the user typed.
How the input function works in Python :
When input() function executes program flow will be stopped until the user has given input.
The text or message displayed on the output screen to ask a user to enter an input value is optional i.e. the
prompt, which will be printed on the screen is optional.
Whatever you enter as input, the input function converts it into a string. if you enter an integer value still input()
function converts it into a string. You need to explicitly convert it into an integer in your code using typecasting.
Print function
• Form 1:
Print()
• Form 2:
Print(“Hello”,end=“\n”)
• Form 3:
Print(“hello”,sep=“”)
Basic Exercise
• How to add two number after taking input from user ?
• How to swap two variable using third variable after taking
input from user ?
• How to swap 2 variable without using 3rd variable after
taking input from user ?
• How to swap 3 variable without using 4th variable after
taking input from user ?
• Sum of digits after taking input from user ?

You might also like