SlideShare a Scribd company logo
1 of 16
CONDITIONAL EXECUTION, FUNCTIONS
IN PYTHON PROGRAMMING
PROF POOJA B S
 Basic Level : if statement
 Alternative Level : if-else statement
1. if Statement 2. if-else Statement
Entry
Exit Exit
Conditional Execution, Functions in Python Programming
d
Statement Block
Condition False
True
d
Statement Block1 Statement Block2
True Condition False
 Syntax for if Statement:
if condition:
Statement Block
 Example for if Statement:
1. x=10
if x<40:
print(“Yes 10 is less than 40”) #Yes 10 is less than 40
2. x=10
if x<0:
pass #Do nothing
3. x=10
if x<0:
print(“Yes 10 is less than 40”) #Shows Nothing
 Syntax for if-else Statement:
if condition:
Statement Block1
else:
Statement Block2
 Example for if-else Statement:
x=int(input(“Enter any number”))
if x%2==0:
print(“Number is even”)
else:
print(“Number is odd”)
 Nested Conditionals
 Eg 1
marks=float(input(“Enter Marks”))
if marks>60:
if marks<70:
print(“FC”)
else:
print(“FCD”)
Eg 2
gender=input("Enter Gender:n")
age=int(input("Enter Age:n"))
if gender=='Male':
if age>=21:
print("Boy is eligible for marriage")
else:
print("Boy is not eligible for marriage")
elif gender=='Female':
if age>=18:
print("Girl is eligible for marriage")
else:
print("Girl is not eligible for marriage")
Catching Exceptions using try and except
 Avoid runtime error.
 Possible reason is wrong input. 40/0= divide by zero error: Runtime error
 Eg:
a=int(input("Enter an"))
b=int(input("Enter bn"))
try:
c=a/b
print(c)
except:
print("Division by zero cannot be done")
Functions in Python Programming
 Named sequence of statements
 Performs Computation
Built-in Functions:
 max, min, len
 max function: Gives the largest of values in the list
 min function: Gives the smallest of values in the list
 len function: Total number of characters in the list
 Eg: max(10,20,33) #33
min(-2,-1,0) #-2
len('10,20,30') #8
max('Hellohello') #o
min('Hellohello') #H
len('Hellohello') #10
ASCII Table
Type Conversion Functions
 Convert one type to another.
1. int function
int(‘32’) #32
int(‘hello’) # invalid literal for int() with base 10: 'hello'
int(3.7898) #3
int(-2.056) #-2
2. float function
float(‘32’) #32.0
float(3.7898) #3.7898
3. str function
str(32) #32
str(3.7898) #3.7898
 Pseudorandom Numbers Generation
 Uses module random
 Eg:
import random
random.random() #0.9907777
 Module creates an object. We can access function or variables using the dot operator.
 randint(): takes 2 arguments low and high and returns a random number -> random.randint(2,40)
 choice(): takes a list and returns a random number-> t=[1,2,-3,,12,7]
random.choice(t)
Random Numbers
 Uses module math: import math
 Functions available in math are:
 sqrt(): math.sqrt(34) #5.8309
 pi(): print(math.pi) #3.14159
 log10(): math.log10(2) #0.30102999
 log(): natural logarithm ->math.log(2) #0.69314718
 sin(): argument must be in radians. If it is in degrees convert to radians by multiplying with pi/180:
math.sin(90*math.pi/180) #1.0
 pow(): math.pow(3,4) #81.0
Math Functions
 Define own functions
 Syntax:
def fname(arg_list):
Statement 1
Statement 2
……
Statement N
return value
def: keyword
fname: function name
arg_list: arguments
statements: instructions
return: keyword
Adding New Functions [User Defined Functions]
def myfun():
print('Hello')
print('Inside the function')
print('')
print('Example of Function')
myfun()
print('End Example')
Parameters and Arguments
def test(var): #var is a parameter
print('Inside test()')
print('Argument is:',var)
print('Example of Function with Arguments')
x='Hello'
test(x) #x is an argument
print('')
y=20
test(y) #y is an argument
print('Done With Arguments')
 Easier to read, understand and debug
 Make program simple
 Divide long program to functions
Why Functions

More Related Content

What's hot

Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2vikram mahendra
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling Intro C# Book
 
C Prog. - ASCII Values, Break, Continue
C Prog. -  ASCII Values, Break, ContinueC Prog. -  ASCII Values, Break, Continue
C Prog. - ASCII Values, Break, Continuevinay arora
 
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 PYTHONvikram mahendra
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Neeru Mittal
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Alex Penso Romero
 
The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189Mahmoud Samir Fayed
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#Giorgio Zoppi
 
0.my book draft chap 1
0.my book draft chap 10.my book draft chap 1
0.my book draft chap 1manhduc1811
 

What's hot (20)

Nested loops
Nested loopsNested loops
Nested loops
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
C++ lecture 02
C++   lecture 02C++   lecture 02
C++ lecture 02
 
Quiz 10 cp_sol
Quiz 10 cp_solQuiz 10 cp_sol
Quiz 10 cp_sol
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
C Prog. - ASCII Values, Break, Continue
C Prog. -  ASCII Values, Break, ContinueC Prog. -  ASCII Values, Break, Continue
C Prog. - ASCII Values, Break, Continue
 
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
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
Session12 pointers
Session12 pointersSession12 pointers
Session12 pointers
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))
 
The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.6 book - Part 21 of 189
 
Operators
OperatorsOperators
Operators
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
0.my book draft chap 1
0.my book draft chap 10.my book draft chap 1
0.my book draft chap 1
 

Similar to Python Conditionals and Functions

Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2Abdul Haseeb
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189Mahmoud Samir Fayed
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
Burrowing through go! the book
Burrowing through go! the bookBurrowing through go! the book
Burrowing through go! the bookVishal Ghadge
 
functions2-200924082810.pdf
functions2-200924082810.pdffunctions2-200924082810.pdf
functions2-200924082810.pdfpaijitk
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperDeepak Singh
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE Saraswathi Murugan
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans incnayakq
 
C Programming Language
C Programming LanguageC Programming Language
C Programming LanguageRTS Tech
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schoolsDan Bowen
 

Similar to Python Conditionals and Functions (20)

Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.6 book - Part 22 of 189
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
Burrowing through go! the book
Burrowing through go! the bookBurrowing through go! the book
Burrowing through go! the book
 
functions2-200924082810.pdf
functions2-200924082810.pdffunctions2-200924082810.pdf
functions2-200924082810.pdf
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
C tutorial
C tutorialC tutorial
C tutorial
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Pythonppt28 11-18
Pythonppt28 11-18Pythonppt28 11-18
Pythonppt28 11-18
 
FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
 
informatics practices practical file
informatics practices practical fileinformatics practices practical file
informatics practices practical file
 
C Programming Language
C Programming LanguageC Programming Language
C Programming Language
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 

More from Pooja B S

String Methods and Files
String Methods and FilesString Methods and Files
String Methods and FilesPooja B S
 
Lists in Python
Lists in PythonLists in Python
Lists in PythonPooja B S
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
Python Basics
Python BasicsPython Basics
Python BasicsPooja B S
 

More from Pooja B S (6)

Iteration
IterationIteration
Iteration
 
String Methods and Files
String Methods and FilesString Methods and Files
String Methods and Files
 
Lists in Python
Lists in PythonLists in Python
Lists in Python
 
Dictionary
DictionaryDictionary
Dictionary
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Python Basics
Python BasicsPython Basics
Python Basics
 

Recently uploaded

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 

Python Conditionals and Functions

  • 1. CONDITIONAL EXECUTION, FUNCTIONS IN PYTHON PROGRAMMING PROF POOJA B S
  • 2.  Basic Level : if statement  Alternative Level : if-else statement 1. if Statement 2. if-else Statement Entry Exit Exit Conditional Execution, Functions in Python Programming d Statement Block Condition False True d Statement Block1 Statement Block2 True Condition False
  • 3.  Syntax for if Statement: if condition: Statement Block  Example for if Statement: 1. x=10 if x<40: print(“Yes 10 is less than 40”) #Yes 10 is less than 40 2. x=10 if x<0: pass #Do nothing 3. x=10 if x<0: print(“Yes 10 is less than 40”) #Shows Nothing
  • 4.  Syntax for if-else Statement: if condition: Statement Block1 else: Statement Block2  Example for if-else Statement: x=int(input(“Enter any number”)) if x%2==0: print(“Number is even”) else: print(“Number is odd”)
  • 5.  Nested Conditionals  Eg 1 marks=float(input(“Enter Marks”)) if marks>60: if marks<70: print(“FC”) else: print(“FCD”)
  • 6. Eg 2 gender=input("Enter Gender:n") age=int(input("Enter Age:n")) if gender=='Male': if age>=21: print("Boy is eligible for marriage") else: print("Boy is not eligible for marriage") elif gender=='Female': if age>=18: print("Girl is eligible for marriage") else: print("Girl is not eligible for marriage")
  • 7. Catching Exceptions using try and except  Avoid runtime error.  Possible reason is wrong input. 40/0= divide by zero error: Runtime error  Eg: a=int(input("Enter an")) b=int(input("Enter bn")) try: c=a/b print(c) except: print("Division by zero cannot be done")
  • 8. Functions in Python Programming  Named sequence of statements  Performs Computation Built-in Functions:  max, min, len  max function: Gives the largest of values in the list  min function: Gives the smallest of values in the list  len function: Total number of characters in the list  Eg: max(10,20,33) #33 min(-2,-1,0) #-2 len('10,20,30') #8 max('Hellohello') #o min('Hellohello') #H len('Hellohello') #10
  • 10. Type Conversion Functions  Convert one type to another. 1. int function int(‘32’) #32 int(‘hello’) # invalid literal for int() with base 10: 'hello' int(3.7898) #3 int(-2.056) #-2 2. float function float(‘32’) #32.0 float(3.7898) #3.7898 3. str function str(32) #32 str(3.7898) #3.7898
  • 11.  Pseudorandom Numbers Generation  Uses module random  Eg: import random random.random() #0.9907777  Module creates an object. We can access function or variables using the dot operator.  randint(): takes 2 arguments low and high and returns a random number -> random.randint(2,40)  choice(): takes a list and returns a random number-> t=[1,2,-3,,12,7] random.choice(t) Random Numbers
  • 12.  Uses module math: import math  Functions available in math are:  sqrt(): math.sqrt(34) #5.8309  pi(): print(math.pi) #3.14159  log10(): math.log10(2) #0.30102999  log(): natural logarithm ->math.log(2) #0.69314718  sin(): argument must be in radians. If it is in degrees convert to radians by multiplying with pi/180: math.sin(90*math.pi/180) #1.0  pow(): math.pow(3,4) #81.0 Math Functions
  • 13.  Define own functions  Syntax: def fname(arg_list): Statement 1 Statement 2 …… Statement N return value def: keyword fname: function name arg_list: arguments statements: instructions return: keyword Adding New Functions [User Defined Functions]
  • 14. def myfun(): print('Hello') print('Inside the function') print('') print('Example of Function') myfun() print('End Example')
  • 15. Parameters and Arguments def test(var): #var is a parameter print('Inside test()') print('Argument is:',var) print('Example of Function with Arguments') x='Hello' test(x) #x is an argument print('') y=20 test(y) #y is an argument print('Done With Arguments')
  • 16.  Easier to read, understand and debug  Make program simple  Divide long program to functions Why Functions