Different Functions In Python
What is function ?
Function is word that use to make
the program ,without function to make a progarm is very hard ,even progarm is
not possible .Function is divided into three parts in python.Some functions are
very important because wuithout some functions program is not possible like
input() function,int() ,eval(),float() function these are very important in
python .These functions are mandotory in python.
Standard Definition:
Function is a colloection of
statements which is made to perform a specific task.
Q.What are the types of function?
The function in python is divded
into three parts.
1.In built function
2.User defined function
3.Modules (function inside
module)
Here we will
discuss about first in built function that are easy to use and get the output
very fast in short work:
First
1.int()
function -It is used to implicit the given data into integer type.
Testcases:
Input:
24.0
Output:
24
Testcases2:
Input:
-6
Output:
6
2.eval()
funtction-It is used to implicit the given data into integers what ever you
have have input +ve ,or -ve or fraction number.
3.str() -It
is used to convert given input into
string type.
4.join()
function -It is used to join the first string into second string. Let us
understand using some examples:
Testcases:
Input:
abcd
Ram
Output:
R abcd a abcd m abcd
5.isupper()
-This function is used to check that your given string is in upper or not.
If the
condition is true then output will be True.
6.islower()
-This function is used to chec given string is in lower case or not .If
the condition is true.then Output will be True.
7.max()
-This function is used to find maximum from the list.
8.min()
-This function is used to find minum value from the list.
9.replace()-This
function is used to reolace any word from the string by any other word.
10.remove()-This
function is used to delete the element from the lsit using index value or
direct by giving that value.
11.clear()
-This is used to make a list empty list.
All these
functions are in built that we can use easily in our program by easy efforts .
So these are
built in function .
Now we will move forward to learn user defined
function .It means we will define the function and wev can use our function in
program .
How
userdefined Function work?
The userdefined function is defined by the user ,but don’t have to use in built
function as a userdefined function.
So let us
understand how userdefined work.As we know first we have to make a userdefined
function .And then we have to write satements in the indent of that function
whatever statements you want to write.OR you can write any any logic of
Fibonacci series ,or palindrome and many more programs inside your defined
function.
And if you
want to execute you’re the program whatever you have written in your indent.
So Now you
have to come outside the indent of the program and then call that function .
Then your
program will execute in the console screen .
Note:
You can call
your function also in console screen by its name.
If you will
not call the function that you have defined your function then your program
will not execute .
How to make userdefined Functions?
So let us we
understand that how to make userdefined function.
Let take
example:
First use
def keyword and space and then write
your function whatever you want to make like add() function .
After this
your function is made now you have to write any thing whatever you want to
write in the indent .
Then I have
use print() and write any thing.
Now if you
want to print your statement in the
console screen that you have wrote in the print() function.
So for that
come out of the indent and then call your function that you have made i.e add() then your statement will show in the screen.
Testcases1:
Output:
Hello
Userdefined function
def add():
print(“Hello Userdfined function”)
add()
Now we will
write another program using your own function.
Here we will
define another function i.e sum()
def sum():
a=1
b=2
S=a+b
print(S)
#Now your program is ready but if you want to execute
then you have to call your function that by his name i.e sum() function.
sum()
Output:
3
I think you
have understood the main algorithm to make a program using a your own function,
Now if you want
to call your function in another file ,So first you have to save that file
using .py extention.
Let us
understand using example:
SUM.py |
def sum(): a=1 b=2 S=a+b print(S) |
Now you have
saved your file in SUM.py file .Then now you have to open your new file to execute
your program in an another file,
.py |
import SUM print(SUM.sum()) Output: 3 |
Similarily
you can do many programs and you can call your program in another file.
So Let us we
will do program on palindrome number to understand the userdefined function in clearly,
Testcases1:
Input:
121
Output:
Number is palindrome
Testcases2:
Input:
122
Output:
Not
Pallindrome
Pallindrome.py |
#Let us we are defining the function i.e
pallindrome() function and then in the indent of the defining function we
will write concept or algorithm of the main program def pallindrome():
N=int(input("Enter any positive number:")) temp=N rev=0
while(N>0):
rem=N%10
rev=rev*10+rem
N=N//10 if
temp==rev:
print("Given number is pallindrom.") else:
print("Given number is not pallindrome.") pallindrome() |
Now If you want to execute this program in
an another file then you can call your
function another file by calling its name .so again we will try to understand
that how to call the function in another:
Untitled.py |
import Pallindrome print(Pallindrome.pallindrome()) |
So in this way you have to call your program in
another file.
I think you
have understood that how to call your function in an another file.
Now we will
discuss about some modules of the python
MODULES IN
PYTHON
In python
there modules in python i.e math module,random
module.
1.Math
module :
In math
module is the module that are stored in python library.In which math module is
very important play role to use functions that are stored in math module.
So Let us
understand by reading the functions that are in math module
· factorial()
· floor()
· sqrt()
· sin()
· cos()
· tan()
· cot()
· cosec()
· sec()
· ceil()
How to use
this function sin python Let us understand using examples:
To find square
root any number
Testcases1:
Input:
4
Output:
2
Untitled.py |
import math print(math.sqrt(4)) |
To do or to
use all math module function you have to first import your file in math module
that you have to use function.It is similar like that we have done already in
the above topic i.e how to call your defined function in another file.
Program to find
power of the number .
Testcases1:
23
Output:
8
|
Output: |
8
Program to
find positive number if it is negative
Testcases1:
-4
Output:
4
Untitled.py |
import math print(math.fabs(-4)) |
So Now you can use all functions in the
same file
Untitled.py |
import math print(math.sqrt(4)) print(math.fabs(-4)) print(math.pow(2,3)) print(math.floor(42.4)) print(math.sin(1)) print(math.ceil(43.8)) |
Similarily
you can use all math library functions.
2.Random
Random is a module as well as it is also a
function.
random()-It
is used to print random number it means any number without any argument
|
randint()-It
is used to print number between the number .Let us understand using example:
|
import random print(random.randint(1,10)) |
.
In randint()
function give output any between start to end it can give 1 or 2 or 3 or……….10.
randrange()-This
function print the number between the range but it nor give number i.e in the end
value .
Testcases1:
range(1,10)
Output:
Must be 1
to9 it will not give 10
Like
1
Untitled.py |
import random print(random.randrange(1,10)) |
Now I think
you have understood functions in python i.e in built ,userdefined and modules
function.
No comments: