Control statements and loops are basic building blocks for every programming language. In most of the programming languages, control & loop blocks are identified using curly({}) brackets around the control & loop statements. Since Python does not support curly brackets, it is important to have proper indentation to identify the block. Python code will fail without proper indentation.
Example 1:
a = 2; b = 5
if a < b:
print(True)
Output: True
Example 2:
if a < b: print("a is less than b")
Output: a is less than b
Example 3:
if a > b: print("a is greater than b") #No Output
Output:
Example 4:
if 3 in [1,2,3,4]:
print('3 is present')
Output: 3 is present
Example 1:
a = 7; b = 5
if a < b:
print(True)
else:
print(False)
Output: False
Example 2:
if a < b:
print("b is greater than a")
else:
print("a is greater than b")
Output: a is greater than b
Example 3:
a = 7; b = 10
if a < b:
print("b is greater than a")
else:
print("a is greater than b")
Output: b is greater than a
Example 4:
if 5 in [1,2,3,4]:
print('5 is present')
else:
print('5 is not present')
Output: 5 is not present
Example 1:
a = 7; b = 5
if a < b:
print("b is greater than a")
elif( a > b ):
print("a is greater than b")
Output: a is greater than b
Example 2:
a = 7; b = 10
if a < b:
print("b is greater than a")
elif( a > b ):
print("a is greater than b")
Output: b is greater than a
Example 3:
a = 7; b = 5; c=7
if a < c:
print("c is greater than a")
elif( a < b ):
print("b is greater than a")
elif( a==c):
print("a is equal to c")
Output: a is equal to c
Example 4:
if 5 in [1,2,3,4]:
print('5 is present')
elif 4 in [1,2,3,4]:
print('4 is present')
Output: 4 is present
Example 1:
a = 7; c=7
if a < c:
print("c is greater than a")
elif( a > c ):
print("a is greater than c")
else:
print("a is equal to c")
Output: a is equal to c
Example 1:
for x in [0, 1, 2, 3]:
print(x)
Output:
0
1
2
3
Example 2:
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
if x%2==0:
print(x)
Output:
2
4
6
8
10
Example 3:
for x in range(1,11):
if x%2==0:
print(x)
Output:
2
4
6
8
10
Example 1:
i = 1
while i <= 3:
print(i)
i += 1
Output:
1
2
3
Example 2:
i = 1
while i <= 9:
if i%3 == 0:
print(i)
i += 1
Output:
3
6
9
Example 1:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
Output:
1
2
3 <-- Loop stopped/breaked when i = 3
Example 1:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Output:
1
2
4 <-- i = 3 is missing because of continue
5
6