Python Loops 7 | Learn With Pirates
Chapter 7: Python Loops
In programming when you have the same task perform multiple time like you want to multiplication table for any number can you write several line code for multiplication.
Here this stage python has some loop concept that helps us to complete the repeated task at once.
Here this stage python has some loop concept that helps us to complete the repeated task at once.
Type of loops in Python
Mainly the python has two types of loops- for
- while
While loop
While loop is executed while the condition is true. after the condition is false they stop and exit from the program.i = 0
while i<8:
print("Hello " + str(i))
i = i + 1
print("Done")
Output
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Done
For Loop
A for loop is used to iterate through a sequence like a list, tuple or stringRange function in python
i= 0
for i in range(0, 10):
print(i)
i=i+1
Output
0
1
2
3
4
5
6
7
8
9
Break Statment
for i in range(100):
print(i)
if i == 7:
break
else:
print("This is inside else of for")
Output
0
1
2
3
4
5
6
7
Continue statement
for i in range(9):
if i == 7:
continue
print(i)
Output
0
1
2
3
4
5
6
8
Pass statement
i = 4
if i>0:
pass
while i>6:
pass
print("Pirates are Great")
Output
Pirates are Great
Termination
If you Like this tutorial please share with your friend and family. I make an amazing tutorial in this blog. If you have any queries and question ask me in the comment section below.
No comments
Post a Comment