• No products in the cart.

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/

Statinfer

Statinfer derived from Statistical inference. We provide training in various Data Analytics and Data Science courses and assist candidates in securing placements.

Contact Us

info@statinfer.com

+91- 9676098897

+91- 9494762485

 

Our Social Links

top
© 2020. All Rights Reserved.