for loop

  • loops are used for iteration
  • loop will allow you to visit each value of a sequnenvce one at a time

Anatomy of loop

  • for : keyword
  • i : new_variable
  • in : keyword
  • iterable
In [2]:
for sdfgvswf12341235 in [234,5,234,5234,5,2345,7]:
    print(sdfgvswf12341235)
234
5
234
5234
5
2345
7
In [3]:
for i in 1234:
    print(i)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-aa84d9a4c8e7> in <module>()
----> 1 for i in 1234:
      2     print(i)

TypeError: 'int' object is not iterable
In [4]:
for i in "hello":
    print(i)
h
e
l
l
o
In [7]:
a = [10, 4, 7]
for i in a:
    print(i)
10
4
7
In [8]:
for i in range(len(a)):
    print(i)
0
1
2
In [9]:
# range(start=0, end-> end-1)
In [11]:
list(range(10,20))
Out[11]:
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
In [13]:
for i in list(range(10, 15)):
    print(i)
10
11
12
13
14
In [14]:
for i in range(10, 15):
    print(i)
10
11
12
13
14
In [16]:
list(range(10))
Out[16]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [17]:
for i in range(3):
    print("hello")
hello
hello
hello
In [18]:
for i in [2134,22345,23534]:
    print("hello")
hello
hello
hello
In [19]:
for i in range(3):
    print(i)
0
1
2
In [21]:
a = "apple"
for i in range(len(a)):
    print(i, a[i])
0 a
1 p
2 p
3 l
4 e
In [22]:
for i in "apple":
    print(i)
a
p
p
l
e

WAP to check if a given word has consecutive vowels

In [28]:
word = input("Enter a word ")
flag = 0
for i in range(len(word)-1):
    if word[i] == word[i+1] and word[i] in "aeiouAEIOU":
        flag += 1
if flag>=1:
    print("Yes. It has consecutive vowels")
else:
    print("No")
Enter a word google
Yes. It has consecutive vowels

break and continue

In [32]:
x = 10
pass
y = 12
In [35]:
break
  File "<ipython-input-35-6aaf1f276005>", line 1
    break
         ^
SyntaxError: 'break' outside loop
In [36]:
continue
  File "<ipython-input-36-6ca52a340915>", line 1
    continue
            ^
SyntaxError: 'continue' not properly in loop
In [41]:
for i in range(1,11):
    if i%3==0:
        break
    print(i)
1
2
In [42]:
for i in range(1,11):
    if i%3==0:
        continue # skip the current iteration and continue with next iteration
    print(i)
1
2
4
5
7
8
10
In [52]:
a = input("Enter a word ")
b = ""
for i in a:
    if i in "aeiouAEIOU":
        continue
        print("Line never executed")
    else:
        b += i
print(b)
Enter a word India
nd
In [46]:
for i in range(1000, 10000):
    if i==1005:
        break
    print(i)
1000
1001
1002
1003
1004
In [47]:
for i in range(10):
    break
In [49]:
a = input("Enter a word ")
b = ""
for i in a:
    if i not in "aeiouAEIOU":
        b += i       
print(b)
Enter a word India
nd

for else loop

In [59]:
# else if executed if break is not executed
n = int(input("Enter a number"))
for i in range(2, n):
    if n%i==0:
        print("Not prime")
        break
else:
    print("Prime")
Enter a number11
Prime
In [62]:
for i in range(10):
    if i==3:
        break
    print(i)
else:
    print("else block")
0
1
2
In [63]:
for i in range(10):
    if i==30:
        break
    print(i)
else:
    print("else block")
0
1
2
3
4
5
6
7
8
9
else block
In [ ]: