Professional Documents
Culture Documents
Table of Contents
1. What is Loop?
2. Real World Examples of Loop
3. For Loop Python - Syntax and Examples
4. for loop with else clause
5. While Loop
What is Loop?
Loop is an important programming concept and
exist in almost every programming language
(Python, C, R, Visual Basic etc.). It is used to
repeat a particular operation(s) several times
until a specific condition is met. It is mainly used
to automate repetitive tasks.
https://www.listendata.com/2019/04/loops-in-python.html 1/11
6/2/2019 Loops in Python explained with examples
for i in range(1,10,2):
print(i)
Output
1
3
5
7
9
https://www.listendata.com/2019/04/loops-in-python.html 2/11
6/2/2019 Loops in Python explained with examples
mylist = [30,21,33,42,53,64,71,86,97,10]
for i in mylist:
print(i)
Output
30
21
33
42
53
64
71
86
97
10
for i in mylist[::3]:
print(i)
Output
30
42
https://www.listendata.com/2019/04/loops-in-python.html 3/11
6/2/2019 Loops in Python explained with examples
71
10
new
https://www.listendata.com/2019/04/loops-in-python.html 4/11
6/2/2019 Loops in Python explained with examples
import numpy as np
k=np.array(l1)
new=k[np.where(k<=10)]
k = "deepanshu"
import string
for n in string.ascii_lowercase:
if n in k:
print(n + ' exists in ' + k)
else:
print(n + ' does not exist in ' + k)
string.ascii_lowercase returns
'abcdefghijklmnopqrstuvwxyz'.
import pandas as pd
np.random.seed(234)
df = pd.DataFrame({"x1" :
np.random.randint(low=1, high=100, size=10),
https://www.listendata.com/2019/04/loops-in-python.html 5/11
6/2/2019 Loops in Python explained with examples
"Month1" :
np.random.normal(size=10),
"Month2" :
np.random.normal(size=10),
"Month3" :
np.random.normal(size=10),
"price" : range(10)
})
df
for i in range(1,4):
print(df["Month"+str(i)]*1.2)
Month1.
import pandas as pd
newDF = pd.DataFrame()
for i in range(1,4):
data = pd.DataFrame(df["Month"+str(i)]*1.2)
newDF=pd.concat([newDF,data], axis=1)
https://www.listendata.com/2019/04/loops-in-python.html 6/11
6/2/2019 Loops in Python explained with examples
import pandas as pd
import numpy as np
for i in range(1,4):
df['newcol'+str(i)] = np.where(df['x1'] >=
50,
df['Month'+str(i)] * df['price'],
df['Month'+str(i)])
mydata = pd.DataFrame({"X1" :
["A","A","B","B","C"]})
https://www.listendata.com/2019/04/loops-in-python.html 7/11
6/2/2019 Loops in Python explained with examples
s= "Your Input"
"i am {}".format(s)
1. Continue statement
2. Break statement
Continue Statement
When continue statement is executed, it skips
the further code in the loop and continue
iteration.
In the code below, we are avoiding letters a and
d to be printed.
https://www.listendata.com/2019/04/loops-in-python.html 8/11
6/2/2019 Loops in Python explained with examples
for n in "abcdef":
if n =="a" or n =="d":
continue
print("letter :", n)
letter : b
letter : c
letter : e
letter : f
Break Statement
When break statement runs, it breaks or stops
the loop.
In this program, when n is either c or d, loop
stops executing.
for n in "abcdef":
if n =="c" or n =="d":
break
print("letter :", n)
letter : a
letter : b
https://www.listendata.com/2019/04/loops-in-python.html 9/11
6/2/2019 Loops in Python explained with examples
2 is a prime number
3 is a prime number
4 = 2 * 2
5 is a prime number
6 = 2 * 3
7 is a prime number
8 = 2 * 4
9 = 3 * 3
While Loop
https://www.listendata.com/2019/04/loops-in-python.html 10/11
6/2/2019 Loops in Python explained with examples
i = 1
while i < 10:
print(i)
i += 2 #means i = i + 2
print("new i :", i)
Output:
1
new i : 3
3
new i : 5
5
new i : 7
7
new i : 9
9
new i : 11
counter = 1
while (counter <= 5):
if counter < 2:
print("Less than 2")
elif counter > 4:
print("Greater than 4")
else:
print(">= 2 and <=4")
counter += 1
https://www.listendata.com/2019/04/loops-in-python.html 11/11