Functions allow programmers to organize code into reusable blocks to perform related actions. There are three types of functions: built-in functions, modules, and user-defined functions. Built-in functions like int(), float(), str(), and abs() are predefined to perform common tasks. Modules like the math module provide additional mathematical functions like ceil(), floor(), pow(), sqrt(), and trigonometric functions. User-defined functions are created by programmers to customize functionality.
2. A function is a block of organized, reusable code that is used to perform
a single, related action.
What are Functions?
Functions are a convenient way to divide your code into useful blocks,
allowing us to order our code, make it more readable, reuse it and save
some time. Also functions are a key way to define interfaces so
programmers can share their code.
A Function in Python is used to utilize the code in more than one place
in a program. It is also called method or procedures.
3. Why we need functions?
We can easily work with problems whose solutions can be written in the
form of small python programs.
Like Example: To Accept the name and display it.
nm=input(“enter the name”)
print(“your name=“,nm)
As the problems become more complex, then the program size and
complexity and it become very difficult for you to keep track of the data
and know each and every line.
4. So python provides the feature to divide a large program into different smaller
modules or functions. These have the responsibility to work upon data for
processing. Example:
statements
statements
statements
statements
statements
statements
statements
statements
statements
.
.
.
statements
This
program is
one long,
complex
sequences
of
statements
If we are using
the functions
the task divided
into smaller
tasks, each task
perform his
work and all
these task are
combined when
need according
to requirements
Function1:
statements
statements
statements
statements
Function2:
statements
statements
statements
statements
Function3:
statements
statements
statements
statements
Task 1:
Task 2:
Task 3:
5. TYPES OF FUNCTIONS
There are three types of functions categories:
Built-in functions:
Modules:
User define Functions:
6. Built-In Functions
The Python built-in functions are predefined functions that are already
available in python. It makes programming more easier, faster and more
powerful. These are always available in the standard library.
Type conversion functions:
It provide functions that convert values from one type to another.
str() :
float() :
eval() :
int() :
7. int() : Convert any value into an integer.
Example:
If we want to accept two numbers A, B from user and using input() and
you know that input return string, so we need to convert it to number
using int()
A=int(input(“Enter value for A”))
B=int(input(“Enter value for B”))
s=A+B
print(“sum=“,s)
--------OUTPUT---------
Enter value of A 20
Enter value of B 30
sum=50
A=input(“Enter value for A”)
B=input(“Enter value for B”)
s=A+B
print(“sum=“,s)
--------OUTPUT---------
Enter value of A 20
Enter value of B 30
sum=2030
It concatenate means
joining of characters
8. str() : Convert any value into an string.
Example:
If we want to convert number 25 into a string, so we need to use str()
A=25
print(“A=“,A)
--------OUTPUT---------
A=25
A=25
print(“A=“,str(A))
--------OUTPUT---------
A=’25’
9. float() : Convert any value into an string.
Example:
If we want to convert number 25 into a floating value, so we need to use
float()
A=float(25)
print(“A=“,A)
--------OUTPUT---------
A=25.0
If we convert a string value into
float
A=float(‘45.895’)
print(“A=“,A)
--------OUTPUT---------
A=45.895
10. eval() : It is used to evaluate the value of string. It takes value as string
and evaluates it into integer or float
A=eval(‘45+10’)
print(“A=“,A)
--------OUTPUT---------
A=55
If we accept value in integer or float it
should automatically evaluate by the
function for that we use eval().
A=eval(input(“enter the value”))
print(“A=“,A)
If we enter value in number it convert
it automatically into number
--------OUTPUT---------
Enter the value 45
A=45
If we enter value in number it convert
it automatically into number
--------OUTPUT---------
Enter the value 45.78
A=45.78
11. abs() : It return absolute value of a single number. It takes an integer or
floating value and always return positive value.
A=abs(-45)
print(“A=“,A)
--------OUTPUT---------
A=45
A=abs(-45.85)
print(“A=“,A)
--------OUTPUT---------
A=45.85
pow(x,y) : function returns the value of x to the power of y (xy)
A=pow(2,3)
print(“A=“,A)
--------OUTPUT---------
A=8
12. type() : If you wish to find the type of a variable,
A=10
B=9.23
C=‘That’
N=[1,2,3]
M=(20,30)
D={‘rollno’:101,’name’:’rohit’}
print(type(A))
print(type(B))
print(type(C))
print(type(N))
print(type(M))
print(type(D))
-------OUTPUT----------
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
13. round() : It is used to get the result up to a specified number of digits.
print(round(10))
print(round(10.8))
print(round(6.3))
-----Output-----
10
11
6
The round() method takes two argument
• The number to be rounded and
• Up to how many decimal places
If the number after the decimal place given
• >=5 than + 1 will be added to the final value
• <5 than the final value will return as it is
print(round(10.535,0))
print(round(10.535,1))
pprint(round(10.535,2))
-----Output-----
11.0
10.5
10.54
14. Modules
A file containing functions and variables defined in separate files. A
module is simply a file that contain python code in the form of separate
functions with special task. The module file extension is same .py . To use
it, you must import the math module:
15. We discuss one module file:
Inside this Math module there are many functions of math's
ceil()
floor()
pow(x,y) sqrt(value)
cos(value)sin(value) tan(value)
Which includes trigonometric functions, representation functions,
logarithmic functions, etc.
Math.py
Math.pi
16. ceil()
floor() It rounds a number downwards to its nearest integer
It rounds a number upwards to its nearest integer.
import math
x = math.ceil(1.4)
print(x)
-----OUTPUT-----
2
import math
x = math.floor(1.4)
print(x)
-----OUTPUT-----
1
import math
x = math.ceil(-1.4)
print(x)
-----OUTPUT-----
-1
import math
x = math.floor(-1.4)
print(x)
-----OUTPUT-----
-2
17. pow(x,y)
This method returns the power of the x corresponding to the
value of y. In other words, pow(2,4) is equivalent to 2**4.
import math
number = math.pow(2,4)
print("The power of number:",number)
-------OUTPUT------
The power of number: 16
number = 2**4
print("The power of number:",number)
-------OUTPUT------
The power of number: 16
18. sqrt(value) It returns the square root of a given number.
import math
print(math.sqrt(100))
----OUTPUT----
10.0
import math
print(math.sqrt(3))
----OUTPUT----
1.7320508075688772
Math.pi
It is a well-known mathematical constant and defined as the ratio of
circumstance to the diameter of a circle. Its value is 3.141592653589793.
import math
print(math.pi)
------OUTPUT------
3.141592653589793
19. cos(value)sin(value) tan(value)
Trigonometric functions
This function
returns the sine of
value passed as
argument. The
value passed in this
function should be
in radians.
This function returns
the cosine of value
passed as argument.
The value passed in
this function should
be in radians.
This function returns the
tangent of value passed as
argument. The value
passed in this function
should be in radians.