[go: up one dir, main page]

0% found this document useful (0 votes)
4 views14 pages

Python Session 7 Conditional Statements

Uploaded by

ameen1ahmad06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Python Session 7 Conditional Statements

Uploaded by

ameen1ahmad06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

In [ ]: - Variables

- Data types

- Type cast

- print eval input

- Basic codes

- Today you will get assignment

- pacakges

- Conditional statments

In [ ]: i want to say hai to you

i want to say hai to you if you are my frined

In [ ]: 1 0
On OFF
yes NO
True False

In [1]: 10=10 # = assign the value

Cell In[1], line 1


10=10 # = assign the value
^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

In [3]: 10==10

Out[3]: True

In [5]: 10>0

Out[5]: True

In [7]: 10<0

Out[7]: False

In [ ]: I want to do task based on condition

condition might be True or might be False

if it is True then I will do one task

if it is False then I will do another task

In [ ]: # syntax
# eval(input(
# whenever any line starts with green color
# at last it will end by :
# whenerver any line end with : the next line starts with
# some gap this is called indenatation
if <con>:
<write your code>

In [9]: print('hello')

hello

In [15]: print('hello')
print('hai')

hello
hai

In [ ]: if 10>0 # if True
if 10<0 # if False

In [17]: if 10>0:
print('hello')

hello

In [19]: if 10<0:
print('hello')

In [21]: if True:
print('hello')

hello

In [23]: if False:
print('hello')

In [25]: # mistake-1:
if 10>0
print('hello')

Cell In[25], line 2


if 10>0
^
SyntaxError: expected ':'

In [27]: # mistake-2:
if 10>0:
print('hello')

Cell In[27], line 3


print('hello')
^
IndentationError: expected an indented block after 'if' statement on line 2

In [33]: if 10>0:
print('hello')

hello

In [35]: if 10>0:
print('hello')
print('hai')
Cell In[35], line 3
print('hai')
^
IndentationError: unexpected indent

In [38]: if 10>0:
print('hello')
print('hai')

File <string>:3
print('hai')
^
IndentationError: unindent does not match any outer indentation level

In [40]: # starts
if 10>0: # if 10>0 if True :
print('hello') #hello
###################################
print('hai')

hello
hai

In [42]: if 10<0: # if False


print('hello')
print('hai')

hai

In [44]: print('hello')
if 10<0:
print('hello')
print('hai')

hello
hai

In [46]: print('hello')
if 10>0:
print('hello')
print('hai')

hello
hello
hai

In [52]: print('hello')
if 10<0:
print('hello')
if 10>0:
print('good')
print('hai')

hello
good
hai

In [60]: print('hello')
###########################
if 10<0:
print('hello')
if 10>0:
print('good')
############################
print('hai')
# hello
# if 10<0 : False

hello
hai

In [56]: print('hello')
if 10>0:
print('hello')
if 10>0:
print('good')
print('hai')

hello
hello
good
hai

In [58]: print('hello')
if 10>0:
print('hello')
if 10<0:
print('good')
print('hai')

hello
hello
hai

In [ ]: print('hello')
#########################
if 10<0:
print('hello')
if 10>0:
print('good')
print('hai')

# hello
# if 10<0 if False : No entru

In [62]: print('hello')

if 10>0:
print('hello')
if 10<0:
print('good')
print('hai')

hello
hello
hai

In [64]: if 10>0:
print('hai')
if 10>0:
print('hai')
if 10>0:
print('hai')
hai
hai
hai

In [70]: if 10>0:
print('hai')
if 10>0:
print('hai')
if 10<0:
print('hai')

hai
hai

if-else

In [2]: if True:
print('hello')
else:
print('bye')

hello

In [4]: if False:
print('hello')
else:
print('bye')

bye

In [6]: if True:
print('hello')
else:
print('bye')
##########################
print('good bye')

hello
good bye

In [8]: if False:
print('hello')
else:
print('bye')
##########################
print('good bye')

bye
good bye

In [10]: print('hey')
if True:
print('hello')
else:
print('bye')
print('good bye')

hey
hello
good bye
In [12]: print('hey')
if True:
print('hello')
print('why are you come in middile')
else:
print('bye')
print('good bye')

Cell In[12], line 5


else:
^
SyntaxError: invalid syntax

In [14]: print('hey')
if True:print('hello')
else:print('bye')
print('good bye')

hey
hello
good bye

In [ ]: # Q1) WAP ask the user enter a number


# print positive if number greater than>=0
# otherwise print negative

# Idea:
# step-1: num= eval(input())
# step-2: if <cond>:
# step-3: print()
# step-4: else:
# step-5: print()

# Q2) repeat above qn by taking a random number

# Q3) WAP ask the user two numbers n1 and n2


# print which is the greatest number
# idea:
# step-1: n1=eval(input())
# step-2: n2=eval(input())
# step-3: if <condition>:
# step-4: print()
# step-5: else:
# step-6: print()

# Q4) Lets play a game


# we will enter one number n1
# code will give some random number 1 to 10 n2
# if both are equal then print won
# other wise lost

# idea
# n1=random.randint(1,10)
# n2=eval()
# if <con>:
# print('won')
# else:
# print('lost')
# Q5) WAP ask
# ask the user enter distance in km (50km)
# if distance greater than 25km
# ask the user enter the charge per km
# print total charge
# otherwise
# print enjoy free ride

# idea
# step-1: distance = eval(input)
# step-2: if <condition>
# step-3: charge=eval()
# step-4: total charge= distance tarvelled * charge
# step-5: else:
# step-6: print free ride

#Q6) WAP ask the user enter a gender


# if gender equal to male
# ask the distance
# enter the charge
# print the total charge
# else
# print free ride

#Q7) WAP ask the user enter a number


# find that number is even numberb or odd number

#Q8) wap ask the user enter


# course name
# ask the user enter institute name
# if course name equal to data science and institute name equal to naresh it
# print life is secure
# otherwise
# print katam zindagi

In [ ]: # Q1) WAP ask the user enter a number


# print positive if number greater than>=0
# otherwise print negative

# Idea:
# step-1: num= eval(input())
# step-2: if <cond>:
# step-3: print()
# step-4: else:
# step-5: print()

In [18]: num=eval(input('enter a number:'))


if num>=0:
print('positive number')
else:
print('negative number')

positive number

In [24]: import random


num=random.randint(-100,100)
if num>=0:
print(f'{num}:positive number')
else:
print(f'{num}:negative number')

-23:negative number

In [ ]: # always focus on how you are passing the number


# 1. hardcoded num=100
# 2. asking user from input=eval(input())
# 3. random number

In [26]: # Q3) WAP ask the user two numbers n1 and n2


# print which is the greatest number
# idea:
# step-1: n1=eval(input())
# step-2: n2=eval(input())
# step-3: if <condition>:
# step-4: print()
# step-5: else:
# step-6: print()

n1=eval(input('enter a n1:'))
n2=eval(input('enter a n2:'))
if n1>n2:
print(f'{n1} is greater')
else:
print(f'{n2} is greater')

20 is greater

In [ ]: # Q4) Lets play a game


# we will enter one number n1
# code will give some random number 1 to 10 n2
# if both are equal then print won
# other wise lost

# idea
# n1=random.randint(1,10)
# n2=eval()
# if <con>:
# print('won')
# else:
# print('lost')

In [36]: n1=random.randint(1,10)
print(n1)
n2=eval(input('enter a number:'))
if n1==n2:
print('won')
else:
print('loss')

8
won

In [ ]: # Q5) WAP ask


# ask the user enter distance in km (50km)
# if distance greater than 25km
# ask the user enter the charge per km
# print total charge
# otherwise
# print enjoy free ride
# idea
# step-1: distance = eval(input)
# step-2: if <condition>
# step-3: charge=eval()
# step-4: total charge= distance tarvelled * charge
# step-5: else:
# step-6: print free ride

In [46]: travel_distance=eval(input('enter how much distance you want travel in km:'))


free_distance=eval(input('how much distance i can get free travel in km:'))
if travel_distance>free_distance:
chargeble_distance=travel_distance-free_distance
print('the chargeble distance is:',chargeble_distance)
charge=eval(input('enter the charge:'))
total_charge=chargeble_distance*charge
print(f'the total charge is:{chargeble_distance}*{charge}=',total_charge)
else:
print('enjoy free ride')

the chargeble distance is: 175


the total charge is:175*3= 525

In [ ]: travel_distance=eval(input('enter how much distance you want travel in km:'))


if travel_distance>25:
charge=eval(input('enter the charge:'))
total_charge=(travel_distance-25)*charge
print('the total charge is:',total_charge)
else:
print('enjoy free ride')

In [56]: #Q6) WAP ask the user enter a gender


# if gender equal to male
# ask the distance
# enter the charge
# print the total charge
# else
# print free ride

gender=input('enter the gender')


if gender=='male': # 'MALE'=='male'
distance=eval(input('enter the distance in km:'))
charge=eval(input('enter the charge per km:'))
total_charge=distance*charge
print('total charge is:',total_charge)
else:
print('free ride')

free ride

In [54]: 'MALE'.lower()

Out[54]: 'male'

In [ ]: #Q7) WAP ask the user enter a number


# find that number is even numberb or odd number

# even number means reminder =0 when we divide with 2


In [62]: int(str(1234)[::-1])

Out[62]: 4321

In [64]: 5/2 # normal divsion

Out[64]: 2.5

In [66]: 5//2 # q

Out[66]: 2

In [68]: 5%2 # re

Out[68]: 1

In [72]: 10%2,11%2

Out[72]: (0, 1)

In [76]: num=eval(input('enter a number:'))


if num%2==0:
print(f'{num} is even')
else:
print(f'{num} is odd')

21 is odd

In [78]: num=random.randint(1,100)
if num%2==0:
print(f'{num} is even')
else:
print(f'{num} is odd')

14 is even

In [84]: #Q8) wap ask the user enter


# course name
# ask the user enter institute name
# if course name equal to data science and institute name equal to naresh it
# print life is secure
# otherwise
# print katam zindagi

cname=input('enter the course:')


iname=input('enter the inst.name:')
if cname=='data science' and iname=='naresh it':
print('your life is secure')
else:
print('katam zindagi')

katam zindagi

In [88]: cname=input('enter the course:')


iname=input('enter the inst.name:')
if cname=='data science' or iname=='naresh it':
print('your life is secure')
else:
print('katam zindagi')
your life is secure

one outcome based on one condition: if condition

two outcomes based on one condition: if -else

three outcomes based on two conditions:if -elif -else

four outcomes: if-elif-elif-else

first one represent with if, last one represent with else

In [ ]: if <con>:
<write your code>
elif <con>:
<write your code>
else:
<write your code>

In [6]: # wap ask the user enter a number just print if it is postive
num=eval(input('enter a number:'))
if num>0:
print(f'{num} is postive')

In [12]: # wap ask the user enter a number print if it is postive otherwise negative
num=eval(input('enter a number:'))
if num>0:
print(f'{num} is postive')
else:
print(f'{num} is negative')

0 is negative

In [18]: #wap ask the user enter a number print if it is postive


# print zero if it is zero
# otherwise negative
num=eval(input('enter a number:'))
if num>0:
print(f'{num} is postive')
elif num==0:
print(f'{num} is zero')
else:
print(f'{num} is negative')

0 is zero

In [ ]: num=eval(input('enter a number:'))
if num>0:
print(f'{num} is postive')
elif num<0:
print(f'{num} is negative')
else:
print(f'{num} is zero')

In [30]: #wap ask the user enter a number print if it is postive


# also check zero otherwise negative
num=eval(input('enter a number:')) # -20
if num>=0: # -20>=0 F
if num==0: # 20==0 F
print(f'{num} is zero')
else:
print(f'{num} is postive')
else:
print(f'{num} is negative')

-20 is negative

In [ ]: sir im a lil confused about


when to use if under if statment
and elif under if if statemnt..
could u pls elaborate on that??

In [ ]: # wap ask the user enter percentage of marks


# print A grade if per greater than equal to 75
# print B grade if per between 60 to 75
# print C grade if per between 45 to 60
# print D grade if per between 30 to 45
# otherwise print fail

In [ ]: percentage=eval(input('Enter the percentage'))


if percentage>=75:
print("A grade")
elif percentage<75 and percentage>60:
print("B grade")
elif percentage<60 and percentage>45:
print("C grade")
elif percentage<45 and percentage>30:
print("D grade")
else:
print("Fail")

num = eval(input('enter a percentage'))


if num>75:
print(f'{num} A')
elif num >=60 and num<=75:
print(f'{num} B')
elif num >=45 and num<60:
print(f'{num} C')
elif num >=30 and num<45:
print(f'{num} D')
else:
print(f'{num} fail')

In [34]: percentage=eval(input('Enter the percentage'))


if percentage>=75: # 65>=75 F
print("A grade")
elif percentage>=60: # 65>=60 T
print("B grade")
elif percentage>=45: # 65>=45 T
print("C grade")
elif percentage>=30: # 65>=30 T
print("D grade")
else:
print("Fail")
# elif : if above condition false then only come and check my condition
# else: if above condition false then directly consider me as solution

B grade

In [ ]: percentage=eval(input('Enter the percentage'))


if percentage>=100:
print("enter a valid number")
elif percentage>=75:
print("A grade")
elif percentage>=60:
print("B grade")
elif percentage>=45:
print("C grade")
elif percentage>=30:
print("D grade")
else:
print("Fail")

In [38]: percentage=eval(input('Enter the percentage'))


if percentage>=75: # 90>=75 T
print("A grade")
if percentage>=60: # 90>=60 T
print("B grade")

# both if indicates individual set up

A grade
B grade

In [40]: percentage=eval(input('Enter the percentage'))


if percentage>=75:
print("A grade")
elif percentage>=60:
print("B grade")

A grade

In [ ]: percentage=eval(input('Enter the percentage'))


if percentage>=75:
print("A grade")
if percentage>=60:
print("B grade")

In [42]: percentage=eval(input('Enter the percentage')) # 90


if percentage>=75: # 90>=75 T
print("A grade") # A
if percentage>=60: # 90>60 T
print("B grade") # B

A grade
B grade

In [44]: # If percentage greater than 75


# then ask the user enter gender
# if gender = boy print no reservation
# other wise print reservation

percentage=eval(input('Enter the percentage'))


if percentage>=75:
print("A grade")
gender=input('enter the gender:')
if gender=='Male':
print('No reservation')
else:
print('33% reservation')

A grade
33% reservation

In [ ]: # wap ask the user enter age


# if age gretaer than or equal to 100 then print unlucky person
# if age between 60 to 100 print ss
# if age between 30 to 60 print MA
# if age between 20 to 30 print young
# if age between 13 to 20 print teenage
# otherwise print kid

In [54]: age = int(input("Enter your age: "))

if age >= 100:


print("Unlucky person")
elif age >= 60:
print("Senior citizen (ss)")
elif age >= 30:
print("Middle-aged (MA)")
elif age >= 20:
print("Young")
elif age >= 13:
print("Teenage")
else:
print("Kid")

Middle-aged (MA)

In [ ]:

You might also like