Statinfer

104.1.4 Conditional operators in Python

Mainly used conditional operators in python.

Link to the previous post: https://statinfer.com/104-1-3-python-objects/

 In this post we will go through some commonly used conditional operations.
  • if-then-else statement
  • for loop
  • break in for loop

If-Then-Else statement

If Condition

In [100]:
age=60
if age<50:
    print("Group1")
print("Done with If")
Done with If

If-else statement

In [101]:
age=60
if age<50:
    print("Group1")
else:
    print("Group2")
print("Done with If else")
Group2
Done with If else

Multiple else conditions in if

In [102]:
marks=75

if(marks<30):
    print("fail")
elif(marks<60):
    print("Second Class")
elif(marks<80):
     print("First Class")
elif(marks<100):
     print("Distinction")
else:
    print("Error in Marks")
First Class
In [103]:
marks=20

if(marks<30):
    print("fail")
elif(marks<60):
    print("Second Class")
elif(marks<80):
     print("First Class")
elif(marks<100):
     print("Distinction")
else:
    print("Error in Marks")
fail

Nested if

In [104]:
x=45

if(x<50):
    print("Number is less than 50")
    if(x<40):
         print ("Number is less than 40")
         if(x<30):
             print("Number is less than 30")
         else:
             print("Number is greater than 30")
    else:
        print("Number is greater than 40")
else:
    print("Number is greater than 50")
Number is less than 50
Number is greater than 40
In [105]:
x=35

if(x<50):
    print("Number is less than 50")
    if(x<40):
         print ("Number is less than 40")
         if(x<30):
             print("Number is less than 30")
         else:
             print("Number is greater than 30")
    else:
        print("Number is greater than 40")
else:
    print("Number is greater than 50")
Number is less than 50
Number is less than 40
Number is greater than 30

For loop

Example-1

In [107]:
my_num=1

for i in range(1,20):
    my_num=my_num+1
    print("my num value is", my_num)
    i=i+1
my num value is 2
my num value is 3
my num value is 4
my num value is 5
my num value is 6
my num value is 7
my num value is 8
my num value is 9
my num value is 10
my num value is 11
my num value is 12
my num value is 13
my num value is 14
my num value is 15
my num value is 16
my num value is 17
my num value is 18
my num value is 19
my num value is 20

Example-2

In [108]:
sumx = 0 
x=1

for x in range(1,20): 
     sumx = sumx + x
     print(sumx)
1
3
6
10
15
21
28
36
45
55
66
78
91
105
120
136
153
171
190

Break Statement in for loop

  • To stop execution of a loop
  • Stopping the loop in midway using a condition
In [110]:
sumx = 0 
x=1

for x in range(1,200): 
     sumx = sumx + x
     if(sumx>500):
         break
     print(sumx)
1
3
6
10
15
21
28
36
45
55
66
78
91
105
120
136
153
171
190
210
231
253
276
300
325
351
378
406
435
465
496

The next post is about Python Packages.
Link to the next post :https://statinfer.com/104-1-5-python-packages/

0 responses on "104.1.4 Conditional operators in Python"

Leave a Message

Blog Posts

Hurry up!!!

"use coupon code for FLAT 30% discount"  datascientistoffer        ___________________________________      Subscribe to our youtube channel. Get access to video tutorials.                

Contact Us

Statinfer Software Solutions#647 2nd floor 1st Main, Indira Nagar 1st Stage, 100 feet road,Indranagar Bangalore,Karnataka, Pin code:-560038 Landmarks: Opp. Namma Metro Pillar 48.

Connect with us

linkin fn twitter g

How to become a Data Scientist.?

top