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]
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