In this video we will discuss about loops in Python, we have basically 2 types of loops :
While loops: n python, while loop is used to execute a block of statements repeatedly until a given a condition is satisfied. And when the condition becomes false, the line immediately after the loop in program is executed.
While loops : For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, . There is “for in” loop which is similar to for each loop in other languages. Let us learn how to use for in loop for sequential traversals.
Python programming language provides following types of loops to handle looping requirements. Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.
In python, while loop is used to execute a block of statements repeatedly until a given a condition is satisfied. And when the condition becomes false, the line immediately after the loop in program is executed.
Syntax :
while expression:
statement(s)
All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
Example:
# Python program to illustrate
# while loop
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
Output:
Hello Geek
Hello Geek
Hello Geek
The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed.
If else like this:
if condition:
# execute these statements
else:
# execute these statements
and while loop like this are similar
while condition:
# execute these statements
else:
# execute these statements
Output:
Hello Geek
Hello Geek
Hello Geek
In Else Block
# Python program to illustrate
# Single statement while block
count = 0
while (count == 0): print("Hello Geek")
Note: It is suggested not to use this type of loops as it is a never ending infinite loop where the condition is always true and you have to forcefully terminate the compiler.
Syntax:
for iterator_var in sequence:
statements(s)
It can be used to iterate over iterators and a range.
# Python program to illustrate
# Iterating over a list
print("List Iteration")
l = ["geeks", "for", "geeks"]
for i in l:
print(i)
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t = ("geeks", "for", "geeks")
for i in t:
print(i)
# Iterating over a String
print("\nString Iteration")
s = "Geeks"
for i in s :
print(i)
# Iterating over dictionary
print("\nDictionary Iteration")
d = dict()
d['xyz'] = 123
d['abc'] = 345
for i in d :
print("%s %d" %(i, d[i]))
Output:
List Iteration
geeks
for
geeks
Tuple Iteration
geeks
for
geeks
String Iteration
G
e
e
k
s
Dictionary Iteration
xyz 123
abc 345
See the below example:
# Python program to illustrate
# Iterating by index
list = ["geeks", "for", "geeks"]
for index in range(len(list)):
print list[index]
Output:
geeks
for
geeks
Below example explains how to do this:
# Python program to illustrate
# combining else with for
list = ["geeks", "for", "geeks"]
for index in range(len(list)):
print list[index]
else:
print "Inside Else Block"
Output:
geeks
for
geeks
Inside Else Block
Syntax:
for iterator_var in sequence:
for iterator_var in sequence:
statements(s)
statements(s)
The syntax for a nested while loop statement in Python programming language is as follows:
while expression:
while expression:
statement(s)
statement(s)
A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.
# Python program to illustrate
# nested for loops in Python
from __future__ import print_function
for i in range(1, 5):
for j in range(i):
print(i, end=' ')
print()
Output:
1
2 2
3 3 3
4 4 4 4
# Prints all letters except 'e' and 's'
for letter in 'geeksforgeeks':
if letter == 'e' or letter == 's':
continue
print 'Current Letter :', letter
var = 10
Output:
Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k
for letter in 'geeksforgeeks':
# break the loop as soon it sees 'e'
# or 's'
if letter == 'e' or letter == 's':
break
print 'Current Letter :', letter
Output:
Current Letter : e
# An empty loop
for letter in 'geeksforgeeks':
pass
print 'Last Letter :', letter
Output:
Last Letter : s
#python #web-development #machine-learning