Loops In Python

Spread the love

There are 2 type of loops in python.

  • for loop
  • while loop

Let’s have detailed information about loops in python.

For loops in Python

for loops in python used for iterating over sequence or series. This sequence can be list, tuple, set or dictionary.

Here is the basic syntax of for loops in python 3

Syntax of for loop in python 3

#python for loop syntax

for value in sequence:
    code

Here is the syntax of for loops in python 3.

  • First for is a keyword to use for loop
  • value is the variable that takes the value of the item inside the sequence on each iteration
  • Next line starts with indentation (refers code of for loop)
  • At last it’s a code which will execute in for loop

Example Of For Loop In Python

#Example of for loops in python example

for i in range(5):
    print(i)

#Output
0
1
2
3
4

Here,

  • For is a keyword
  • “i” is a value
  • range() is a function that return number of sequence (Here it returns 0 to 4 because index starts with 0)
  • Next line starts with indentation that shows code is part of for loop
  • At last print statement print values according to range function

Loop In Python List

We can also access each element or item using for loop

Here is the example how you use loop in python list

#loop in python lists

list = [1,2,3,4,5,6,7,8,9,10]

for i in list:
    print(i)

#Output
1
2
3
4
5
6
7
8
9
10

Like this you can also iterate over a list, tuple, set or dictionary.

Quiz On For Loop

Access each element of tuple using for loop

While Loops In Python

The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is satisfied.

Here is the basic syntax of while loop in python 3 and how to use while loop in python

#Example of while loop in python 3

while expression:
    code 
  • First While is a keyword to use while loop
  • Here expression checked first
  • The remaining code of the loop is executes only if expression evaluates to True

Example Of While Loop In Python

Here is the example of while loop in python 3

#Example of while loop

number = 0
while number < 5 :
    print(number)
    number = number + 1

#Output
0
1
2
3
4

This is the example of while loop in python 3

  • We declared a variable called ‘number’ and initially give it 0 value
  • We used while keyword at beginning then we use comparison operator (<)
  • New line started with indentation and printed number
  • At the end we increase the value of number so the loop run again until condition become false

Quiz On While Loop

#While loop quiz

number = 5
while number > 0:
    print(number)
    number = number - 1

Guess the output of above quiz


Spread the love
12 comments
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

If-else In Python

Spread the love

Spread the loveIf-else in python used to check conditions. These statements are also used to make decisions. before we move further have a quick look at comparison operators, which we…
Spread the love
View Post

Operators In Python

Spread the love

Spread the loveOperators in python is a symbol that perform certain operation on one or more variable or a value. There are 7 types of operators in python Arithmetic operators…
Spread the love
View Post