• No products in the cart.

# 104.1.4 Conditional operators in Python

##### Mainly used conditional operators in python.

Link to the previous post: https://test.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 :
```age=60
if age<50:
print("Group1")
print("Done with If")
```
```Done with If
```

### If-else statement

In :
```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 :
```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 :
```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 :
```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 :
```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 :
```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 :
```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 :
```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://test.statinfer.com/104-1-5-python-packages/

20th June 2017

### 0 responses on "104.1.4 Conditional operators in Python"

#### Statinfer

Statinfer derived from Statistical inference is a company that focuses on the data science training and R&D.We offer training on Machine Learning, Deep Learning and Artificial Intelligence using tools like R, Python and TensorFlow 