Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

UNIVERSITY INSTITUTE OF COMPUTING

MASTERS OF COMPUTER APPLICATIONS


PYTHON PROGRAMMING
21CAH-645

DISCOVER . LEARN . EMPOWER


1
Python Conditions and If statements

● Python supports the usual logical a, b=input("enter two numbers:").split()


conditions from mathematics: if b > a:
○ Equals: a == b, Not Equals: a != b, print("b is greater than a")
○ Less than: a < b elif a == b:
○ Less than or equal to: a <= b print("a and b are equal")
○ Greater than: a > b else:
○ Greater than or equal to: a >= b print("a is greater than b")

2
Python If ... Elif … Else

● The elif keyword is pythons way of saying "if the previous


conditions were not true, then try this condition“
● The else keyword catches anything which isn't caught by the
preceding conditions
Example
if a > b :
print(“a is greater than b”
elif a == b:
print(“a is equal to b”)
else:
print(“b is greater then a”)

3
Python Loops

● i=1 ● i=1
while i < 6: while i < 6:
print(i) print(i)
i=i+1 if i == 3:
break
● i=1 i += 1
while i < 6:
print(i)
if i == 3:
continue
i += 1

4
Demo Loops and if

#Generate prime numbers within a range The range() function returns a


for y in range(3,100): sequence of numbers, starting
num=y from 0 by default, and increments
by 1 (by default), and ends at a
is_prime=True specified number.
for x in range(2,num): for i in range(6):
if num%x==0: print(i)
for i in range(2, 6):
is_prime=False
print(i)
break
if is_prime==True:
print(num)
5
THANK YOU

For queries
Email: kawaljitkaur.uic@cumail.in

13/06/2024 6

You might also like