1.
Sub:- ICT
2. Presenters Name:-
Khushi jain(IU2141230067)
Aagam buttani(IU2141230019)
3. Class:- CSE-3(Sem 3)
4. Program:- B-TECH
5. Topic Name:- Python functions
Khushi jai
Introduction to Python:-
1. What is Python?
Python is an interpreted, object-oriented, high-level programming
language with dynamic semantics . It was originally released in 1991.
Designed to be easy as well as fun, the name "Python" is a nod to the
British comedy group Monty Python.
2. Uses of Python:-
3. What are the benefits of
Python?
4. Features of Python:-
Python Funtions:-
1.Why do we need pyhton funtions?
Functions in Python. You use functions in
programming to bundle a set of instructions that
you want to use repeatedly or that, because of their
complexity, are better self-contained in a sub-
program and called when needed. That means that
a function is a piece of code written to carry out a
specified task.
2.What are python funtions?
The functions in Python are a classic example of such reusability. So,
to serve a wide range of applications from GUI and mathematical
computing to web development and testing, Python’s interpreter
already comes equipped with numerous functions that are always
available for use.
All you’ll really have to do is download required packages that
according to their documentation and freely avail all its useful
functionalities by just importing them over to your code.
DRY expands to Don’t Repeat Yourself and this concept of having re-
usable blocks of codes is very crucial for achieving abstraction in
Python. Thus, in order to use a function all that you’ll really need is its
name, its purpose, its arguments if it takes any and its result’s type if
it returns any.
3. Types of funtion in python:-
(i) User defined funtion :-
Functions that we define ourselves to do the certain specific task are
referred to as user-defined functions.
Advantages of user-defined functions:-
• User-defined functions help to decompose a large program into small
segments which makes the program easy to understand, maintain
and debug.
• If repeated code occurs in a program. The function can be used to
include those codes and execute when needed by calling that
function.
• Programmers working on a large project can divide the workload by
making different functions.
Syntax:-
def function_name(argument1, argument2, ...):
statement_1
statement_2
...
Example:-
1# Program to illustrate
2# the use of user-defined functions
3
4def add_numbers(x,y):
5 sum = x + y
6 return sum
7
8num1 = 5
9num2 = 6
10
11print("The sum is", add_numbers(num1, num2))
Output:-
Enter a number: 2.4
Enter another number: 6.5
The sum is 8.9
(ii)Built-in Functions :-
The Python interpreter has a number of functions that are
always available for use. These functions are called built-in
functions. For example, print() function prints the given
object to the standard output device (screen) or to the text
stream file.
In Python 3.6, there are 68 built-in functions. But for the sake
of simplicity let us consider the majorly used functions and we
can build on from there.
Syntax:-
The syntax of abs() method is:-
abs(num)
Example:-
1# random integer
2integer = -20
3print('Absolute value of -20 is:', abs(integer))
4
5#random floating number
6floating = -30.33
7print('Absolute value of -30.33 is:', abs(floating))
Output:-
Absolute value of -20 is: 20 Absolute value of -30.33
is: 30.33
Python all() Function:-
Definition:-
The all() method returns True when all
elements in the given iterable are true. If not, it
returns False.
Syntax:-
The syntax of all() method is:
all(iterable)
Example:-
1# all values true
2l = [1, 3, 4, 5]
3print(all(l))
4
5# all values false
6l = [0, False]
7print(all(l))
8
9# one false value
10l = [1, 3, 4, 0]
11print(all(l))
12
13# one true value
14l = [0, False, 5]
15print(all(l))
16
17# empty iterable
18l = []
19print(all(l))
Output:-
True
False
False
False
True
(iii) Python Lambda Functions:-
What Are Lambda functions?
In Python, an anonymous function is a function that
is defined without a name.
While normal functions are defined using
the def keyword, in Python anonymous functions are
defined using the lambda keyword.
Hence, anonymous functions are also called lambda
functions.
How To Use Lambda Functions In Python?
A Lambda function in python has the following syntax:
lambda arguments: expression
Lambda functions can have any number of arguments
but only one expression. The expression is evaluated
and returned. Lambda functions can be used wherever
function objects are required.
Example:-
1# Program to show the use of lambda functions
2
3double = lambda x: x * 2
4
5# Output: 10
6print(double(5))
Output:-
10
In [1]:
(iv)Python Recursive Functions:-
What is recursion in Python?
Recursion is the process of defining something in
terms of itself.
A physical world example would be to place two
parallel mirrors facing each other. Any object in
between them would be reflected recursively.
Example:-
1# An example of a recursive function to
2# find the factorial of a number
3
4def calc_factorial(x):
5 <em>"""This is a recursive function
6 to find the factorial of an integer"""
7
8 </em>if x == 1:
9 return 1
10 else:
11 return (x * calc_factorial(x-1))
12
13num = 4
14print("The factorial of", num, "is", calc_factorial(num))
THANK YOU !