Python Tutorial : Loop
Python is one of the popular language programming language , Python are very popular since 1980 for its simplicity , easy of learning and code under stability for both beginner and professional level . Python code are smart , need less code compared to other programming language and syntax are easy to understand. Lot of Inbuild library are integrated with Python for different work , other than that many third party libraries specially for Artificial Intelligence model training third party libraries are very popular .
In this article we will learn about "loop" , what is loop ? loop is nothing but approach for examination of every object , for example a string "This is a python code" , if i need every character of this string , I need to use "loop", "loop" will return every character unless there is any stopping condition . Python support two kind of loop , "for loop" and "While" loop , both are very popular in this days.
for loop — iterates over a sequence (like list, tuple, dictionary, string)
while loop — runs as long as a condition is True
Example of for loop
Example 1
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
output
apple
banana
mango
Example 2
for letter in "Python":
print(letter)
output
P
y
t
h
o
n
Example of while loop
Example 1
count = 1
while count <= 5:
print("Count:", count)
count += 1
output
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Some More Topic about Loop:
- break
- continue
- pass
- else with loops
- Nested loops
1.break : Break statement break loop for a particular conditions .Loop stops iterating when it reach to break statement.
Example 1
for i in range(10):
if i == 5:
break
print(i)
output
0 1 2 3 4
2.continue :continue statement skips the current iteration , that means if any condition satisfy ,loop will bypass the condition.
Example 1
for i in range(10): if i % 2 == 0: continue print(i)
for i in range(10):
if i % 2 == 0:
continue
print(i)
output
1 3 5 7 9
3.pass : pass do nothing , simple tell the program to do the next work
1 3 5 7 9
3.pass : pass do nothing , simple tell the program to do the next work
Example 1
for i in range(5): passprint("Loop finished!")
for i in range(5):
pass
print("Loop finished!")
output
Loop finished!
4.else with loops : We can write if else condition withing the loop , if condition satisfied do one thing else do another thing.
Loop finished!
4.else with loops : We can write if else condition withing the loop , if condition satisfied do one thing else do another thing.
Example 1
word = "Python"for ch in word: if ch.lower() in ['a', 'e', 'i', 'o', 'u']: print(ch, "is a vowel") else: print(ch, "is a consonant")
word = "Python"
for ch in word:
if ch.lower() in ['a', 'e', 'i', 'o', 'u']:
print(ch, "is a vowel")
else:
print(ch, "is a consonant")
output
P is a consonant
y is a consonant
t is a consonant
h is a consonant
o is a vowel
n is a consonant
5.Nested loops : A loop can be under another loop , may be for loop under for loop or for loop under while loop etc.
Example 1
for i in range(1, 6):
for j in range(1, 6):
print(i * j, end="\t")
print()
output
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Example 2
i = 1
while i <= 3:
j = 1
while j <= 3:
print(f"i={i}, j={j}")
j += 1
i += 1
output
i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=2, j=2
i=2, j=3
i=3, j=1
i=3, j=2
i=3, j=3
Some Unique Fact of Python Loop
Backward Looping :Pyhon loop can be reverse direction also using reversed()
for i in reversed(range(1, 6)): print(i)
Add an else Clause to a Loop :Very Unique feature which is not available in other programming language
No comments:
Post a Comment