Responsive Ad Slot

Latest

latest

Python Loops 7 | Learn With Pirates

Thursday, 29 April 2021

/ by Captain Jay's

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. 

Type of loops in Python

Mainly the python has two types of loops
  • for
  • while
We will cover one by one in this tutorial.

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 string
Range 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

The range function used for the generated sequence of number. we can define the start point and endpoint.

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

The break is used to come out of the loop when encountered it instruction the program to. exit the loop now.

Continue statement

for i in range(9):
if i == 7:
continue
print(i)

Output
0
1
2
3
4
5
6
8

Continue is used to stop the current iteration of the loop and continue with the next one it instructs the program to "Skip this iteration".

Pass statement 

i = 4
if i>0:
pass

while i>6:
pass

print("Pirates are Great")

Output
Pirates are Great


Pass is a null statement in python it instructs to "Do nothing"

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

Don't Miss
© all rights reserved
made with by templateszoo