Python if and for statements

In this article, we will learn about if, while, for, break, continue and pass statements

  • Python conditions can be used with if, while and for statements.
  • elif – the previous conditions were not true, then try this condition
  • else – catches anything which isn’t caught by the preceding conditions
>>> if 10 > 20:
...     print ("10 is greater then 20")
... else:
...     print("10 is less then 20")
...
10 is less then 20

>>> a = 500
>>> b = 200
>>> if b>a:
...     print("b is greater then a")
... elif a==b:
...     print("a is equal to b")
... else:
...     print("a is greater than b")
...

# Nested if statments
a is greater than b
>>> x = 80
>>> if x > 20:
...     print("x is greater then 20")
...     if x > 40:
...             print("and above 40")
...     else:
...             print("but not above 40")
...
x is greater then 20
and above 40
  • While loop– we can execute a set of statements as long as a condition is true.
>>> x = 1
>>> while x < 10:
...     print(x)
...     x += 2
...
1
3
5
7
9

>>> x = 0
>>> while x < 5:
...     print("Tick:" + str(x))
...     x = x + 1
... else:
...     print("End of printing while")
...
Tick:0
Tick:1
Tick:2
Tick:3
Tick:4
End of printing while
  • For statement iterates over the items of any sequence , in the order that they appear in the sequence
>>> weeks = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]
>>> for w in weeks:
...     print(w)
...
Sun
Mon
Tue
Wed
Thu
Fri
Sat

>>> for i in range(6):
...     print(i)
...
0
1
2
3
4
5

>>> names = ["Girish","Gopal","Sanjay","Ganesh","Raghu"]
>>> for i in range(len(names)):
...     print(i, names[i])
...
0 Girish
1 Gopal
2 Sanjay
3 Ganesh
4 Raghu

>>> for x in "Girish":
...     print(x)
...
G
i
r
i
s
h

# Nested Loops
>>> names = ["Girish","Gopal","Sanjay","Ganesh","Raghu"]
>>> charac = ["Good","Super","Sweet"]
>>> for x in charac:
...     for y in names:
...             print(x,y)
...
Good Girish
Good Gopal
Good Sanjay
Good Ganesh
Good Raghu
Super Girish
Super Gopal
Super Sanjay
Super Ganesh
Super Raghu
Sweet Girish
Sweet Gopal
Sweet Sanjay
Sweet Ganesh
Sweet Raghu

>>> for num in range(2,10):
...     if num%2 == 0:
...             print("Even Number", num)
...
Even Number 2
Even Number 4
Even Number 6
Even Number 8

>>> for num in range(2,10):
...     if num%2 !=0:
...             print("Odd Number", num)
...
Odd Number 3
Odd Number 5
Odd Number 7
Odd Number 9
  • Break statement – we can stop the loop before it has looped through all the items
  • Continue statement – we can stop the current iteration of the loop, and continue with the next
  • Pass statement – It does nothing. It can be used when a statement is required syntactically but the program requires no action.
>>> x = 1
>>> while x < 10:
...     print(x)
...     if x==2:
...             break
...     x += 2
...
1
3
5
7
9

>>> x = 0
>>> while x < 6:
...     x +=1
...     if x == 4:
...             continue
...     print(x)
...
1
2
3
5
6

>>> names
['Girish', 'Gopal', 'Sanjay', 'Ganesh', 'Raghu']
>>> for x in names:
...     print(x)
...     if x=="Sanjay":
...             break
...
Girish
Gopal
Sanjay
>>> for x in names:
...     if x=="Sanjay":
...             continue
...     print(x)
...
Girish
Gopal
Ganesh
Raghu

>>> while True:
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt
>>>
>>> class myClass:
...     pass
...
>>> myclsobj = myClass()
>>> myclsobj
<__main__.myClass object at 0x038E3770>
>>> def log(*args):
...     pass
...
>>> log("test")
>>> log()

>>> num = [1,2,3,4,5,6]
>>> for n in num:
...     if n==3:
...             continue
...     print(n)
... else:
...     print("End of printing for loop")
...
1
2
4
5
6
End of printing for loop

References

  • https://docs.python.org/3/tutorial/controlflow.html

Learn more about python articles in our upcoming blogs.

Happy Learning!