SlideShare a Scribd company logo
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 (20)

PPTX
Intro to Python Programming Language
Dipankar Achinta
 
PDF
Python If Else | If Else Statement In Python | Edureka
Edureka!
 
PPTX
Python PPT
Edureka!
 
PPTX
File handling in Python
Megha V
 
PPTX
Singly & Circular Linked list
Khulna University of Engineering & Tecnology
 
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
PDF
Processor Organization and Architecture
Vinit Raut
 
PDF
Module 1-Python prog.-BPLCK105B by Dr.SV.pdf
SURESHA V
 
PPTX
Beginning Python Programming
St. Petersburg College
 
PPTX
Loops in Python
AbhayDhupar
 
PPTX
Operators in python
deepalishinkar1
 
PPTX
Unit 1 computer architecture (1)
DevaKumari Vijay
 
PPTX
Python
SHIVAM VERMA
 
PDF
Data handling CBSE PYTHON CLASS 11
chinthala Vijaya Kumar
 
PPT
Microcontroller-8051.ppt
Dr.YNM
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Python basic syntax
Mohamed Essam
 
PPT
Data transfer and manipulation
Sanjeev Patel
 
PPTX
Python basics
Hoang Nguyen
 
Intro to Python Programming Language
Dipankar Achinta
 
Python If Else | If Else Statement In Python | Edureka
Edureka!
 
Python PPT
Edureka!
 
File handling in Python
Megha V
 
Singly & Circular Linked list
Khulna University of Engineering & Tecnology
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Maulik Borsaniya
 
Processor Organization and Architecture
Vinit Raut
 
Module 1-Python prog.-BPLCK105B by Dr.SV.pdf
SURESHA V
 
Beginning Python Programming
St. Petersburg College
 
Loops in Python
AbhayDhupar
 
Operators in python
deepalishinkar1
 
Unit 1 computer architecture (1)
DevaKumari Vijay
 
Python
SHIVAM VERMA
 
Data handling CBSE PYTHON CLASS 11
chinthala Vijaya Kumar
 
Microcontroller-8051.ppt
Dr.YNM
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Python basic syntax
Mohamed Essam
 
Data transfer and manipulation
Sanjeev Patel
 
Python basics
Hoang Nguyen
 

Similar to Python Conditionals and Functions (20)

PPTX
Python programming workshop session 2
Abdul Haseeb
 
DOCX
Mcq cpup
tahir_ali786
 
PPTX
Ch no 4 Python Functions,Modules & packages.pptx
gboy4529248
 
PDF
An Introduction to Part of C++ STL
乐群 陈
 
PDF
Introduction to python programming
Rakotoarison Louis Frederick
 
PDF
cel shading as PDF and Python description
MarcosLuis32
 
PDF
The Ring programming language version 1.6 book - Part 22 of 189
Mahmoud Samir Fayed
 
PPS
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
PPTX
CPP Homework help
C++ Homework Help
 
PDF
Burrowing through go! the book
Vishal Ghadge
 
PDF
functions2-200924082810.pdf
paijitk
 
PDF
Computer science-2010-cbse-question-paper
Deepak Singh
 
PPT
C tutorial
Anurag Sukhija
 
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
PPTX
Pythonppt28 11-18
Saraswathi Murugan
 
PPTX
FUNDAMENTALS OF PYTHON LANGUAGE
Saraswathi Murugan
 
PPTX
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
PPTX
CPP Homework Help
C++ Homework Help
 
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
DOCX
Qust & ans inc
nayakq
 
Python programming workshop session 2
Abdul Haseeb
 
Mcq cpup
tahir_ali786
 
Ch no 4 Python Functions,Modules & packages.pptx
gboy4529248
 
An Introduction to Part of C++ STL
乐群 陈
 
Introduction to python programming
Rakotoarison Louis Frederick
 
cel shading as PDF and Python description
MarcosLuis32
 
The Ring programming language version 1.6 book - Part 22 of 189
Mahmoud Samir Fayed
 
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
CPP Homework help
C++ Homework Help
 
Burrowing through go! the book
Vishal Ghadge
 
functions2-200924082810.pdf
paijitk
 
Computer science-2010-cbse-question-paper
Deepak Singh
 
C tutorial
Anurag Sukhija
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
Pythonppt28 11-18
Saraswathi Murugan
 
FUNDAMENTALS OF PYTHON LANGUAGE
Saraswathi Murugan
 
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
CPP Homework Help
C++ Homework Help
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
vikram mahendra
 
Qust & ans inc
nayakq
 
Ad

More from Pooja B S (6)

PPTX
Iteration
Pooja B S
 
PPTX
String Methods and Files
Pooja B S
 
PPTX
Lists in Python
Pooja B S
 
PPTX
Dictionary
Pooja B S
 
PPTX
String Manipulation in Python
Pooja B S
 
PPTX
Python Basics
Pooja B S
 
Iteration
Pooja B S
 
String Methods and Files
Pooja B S
 
Lists in Python
Pooja B S
 
Dictionary
Pooja B S
 
String Manipulation in Python
Pooja B S
 
Python Basics
Pooja B S
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Horarios de distribución de agua en julio
pegazohn1978
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 

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