SlideShare a Scribd company logo
FUNCTIONS IN PYTHON
CLASS: XII
COMPUTER SCIENCE(083)
INTRODUCTION
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.
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.
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:
TYPES OF FUNCTIONS
There are three types of functions categories:
Built-in functions:
Modules:
User define Functions:
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() :
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
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’
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
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
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
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'>
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
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:
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
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
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
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
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.

More Related Content

What's hot

Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Recursion
RecursionRecursion
Recursion
Abdur Rehman
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
Smit Parikh
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
narmadhakin
 
Echelon forms
Echelon formsEchelon forms
Echelon forms
kishor pokar
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
RaginiJain21
 
Python : Data Types
Python : Data TypesPython : Data Types
Python list
Python listPython list
Python list
Mohammed Sikander
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
Dr.Sandhiya Ravi
 

What's hot (20)

Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Recursion
RecursionRecursion
Recursion
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Echelon forms
Echelon formsEchelon forms
Echelon forms
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Python list
Python listPython list
Python list
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Inline function
Inline functionInline function
Inline function
 
Python functions
Python functionsPython functions
Python functions
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 

Similar to INTRODUCTION TO FUNCTIONS IN PYTHON

Functions.docx
Functions.docxFunctions.docx
Functions.docx
VandanaGoyal21
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
Prof .Pragati Khade
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
simenehanmut
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
Inzamam Baig
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
Kavitha713564
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
vishnupriyapm4
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
CP-Union
 
python modules1522.pdf
python modules1522.pdfpython modules1522.pdf
python modules1522.pdf
DebanjanMaity13
 
Pres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdfPres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdf
RamziFeghali
 
Unit2 input output
Unit2 input outputUnit2 input output
Unit2 input output
deepak kumbhar
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
ARVIND PANDE
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
vishnupriyapm4
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
Mohd Sajjad
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
paijitk
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
krushnaraj1
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Objects and Graphics
Objects and GraphicsObjects and Graphics
Objects and Graphics
Edwin Flórez Gómez
 
Managing console input
Managing console inputManaging console input
Managing console input
rajshreemuthiah
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
UadAccount
 

Similar to INTRODUCTION TO FUNCTIONS IN PYTHON (20)

Functions.docx
Functions.docxFunctions.docx
Functions.docx
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
python modules1522.pdf
python modules1522.pdfpython modules1522.pdf
python modules1522.pdf
 
Pres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdfPres_python_talakhoury_26_09_2023.pdf
Pres_python_talakhoury_26_09_2023.pdf
 
Unit2 input output
Unit2 input outputUnit2 input output
Unit2 input output
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
Functions_19_20.pdf
Functions_19_20.pdfFunctions_19_20.pdf
Functions_19_20.pdf
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Chapter04.pptx
Chapter04.pptxChapter04.pptx
Chapter04.pptx
 
Objects and Graphics
Objects and GraphicsObjects and Graphics
Objects and Graphics
 
Managing console input
Managing console inputManaging console input
Managing console input
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 

More from vikram mahendra

Communication skill
Communication skillCommunication skill
Communication skill
vikram mahendra
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
vikram mahendra
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
vikram mahendra
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
vikram mahendra
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
vikram mahendra
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
vikram mahendra
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
vikram mahendra
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
vikram mahendra
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
vikram mahendra
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
vikram mahendra
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
vikram mahendra
 
GREEN SKILL[PART-2]
GREEN SKILL[PART-2]GREEN SKILL[PART-2]
GREEN SKILL[PART-2]
vikram mahendra
 
GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]
vikram mahendra
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
vikram mahendra
 
Entrepreneurial skills
Entrepreneurial skillsEntrepreneurial skills
Entrepreneurial skills
vikram mahendra
 

More from vikram mahendra (20)

Communication skill
Communication skillCommunication skill
Communication skill
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
GREEN SKILL[PART-2]
GREEN SKILL[PART-2]GREEN SKILL[PART-2]
GREEN SKILL[PART-2]
 
GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
Entrepreneurial skills
Entrepreneurial skillsEntrepreneurial skills
Entrepreneurial skills
 

Recently uploaded

GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 

Recently uploaded (20)

GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 

INTRODUCTION TO FUNCTIONS IN PYTHON

  • 1. FUNCTIONS IN PYTHON CLASS: XII COMPUTER SCIENCE(083) INTRODUCTION
  • 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.