Skip to content

Latest commit

 

History

History
194 lines (167 loc) · 5.25 KB

Prachi_Python_Loops.md

File metadata and controls

194 lines (167 loc) · 5.25 KB

Loops:

What are Loops?

Loops have set of statements which are executed until the condition becomes false.
Loops are used for iteration over a sequence of statements.
We can use loops to iterate over lists, tuples, sets, dictionaries.
We can iterate over a string too as it has set of characters.

Need of Loops?

Let us consider a very basic example to understand the need of loops.

Suppose we want to print numbers from 1 to 100,
either we can print them by writing a print statement for each of them,
or we can use iteration of print statement.
So, we will be using only one print statement and iterate over that to print our values.
Thus , loops makes our code efficient and our life easier .

Types of loops in python

Their are two types of loops in python:
1.for loop
2.while loop

Let's take a look at both types of loops:

1.for loop:

Flowchart of for loop:
Till the time the expression has a value and the conditions are satisfied for loop is executed , when the expression has reached it's last value , the control comes out of for loop.

We will see how for loop is used for lists, tuples, dictionaries , sets and numbers.

  • For loop for lists

Let us understand via an example:

Example 1:
fruits=["dragonfruit","custard apple","mango"]  #list
for i in fruits:  
  print(i)  

Output:
dragonfruit
custard apple
mango

Here , we are iterating over the list of name fruits. First time 'i' takes the value of fruits[0] , next time i=fruits[1] , and finally i becomes fruits[2]. And , inside the loop we are printing 'i'. So, the values of the list is printed one by one.

Example 2:
shapes=["triangle","square","rectangle"]
for s in shapes:
  print(s)

Output:
 triangle
 square
 rectangle

This is another similar example. Again , we are taking the values of the list and printing it one by one by iterating over the lists.

  • For loop for tuples

Let us understand via an example:

Example:

colours=("blue","yellow","red")
for c in colours:
  print(c)

Output:
 blue
 yellow
 red

Here , colours is a tuple. We are iterating ober a tuple. Each time the value of c changes with each iteraton and the new value is printed. Firstly , c is "blue". After the first iteration the value of c becomes "red". After the second iteration the value of c becomes "yellow". And in each iteration the value of c is printed.
This , is how we iterate over a tuple.

  • For loop for sets

Let us understand via an example:

Example:

sets={1,5,11,8}
for c in sets:
  print(c)

Output:
 1
 5
 11
 8

Here sets is the set we are iterating over.Each time c takes the value of sets and prints it.This is how we use set in for loop.
  • For loop for dictionaries

Let us understand via an example:

Example:

dicts={"name":"Prachi","year":"1998","cgpa":"9.23"}
for keys in dicts:
  print(keys)

Output:
 name
 year
 cgpa1

In each iteration keys takes the values of key in dict and prints the same.
  • For loop for strings

Let us understand via an example:

Example:

s="Prachi"
for c in s:
  print(c)

Output:
 P
 r
 a
 c
 h
 i

Again , c takes the value of each character in each iteration and prints the same.

Thus, we see for loops can be used in lists, tuples, set , dictionaries, strings,etc.

2.While loop
Flowchart of while loop:

The while loop executes till the condition is True , once the condition becomes false , it exits the while loop.

Syntax:
while (condition):
  statement(s)

Here condition is the boolean condition , and the loop will execute till the boolean condition is True. Execution of the loop means, the statement(s) inside the loop will be executed.

Example:
i=0
while i<5:
 print(i)
 i=i+1

Output:
 0
 1
 2
 3
 4

The while loop executes till the condition is True. Here the condition is i<5 . Till i was less than 5, while loop was executed and we got the above output.
This is how we use while loops.

This contribution has been made by Prachi Agarwal. Contact me directly on my e-mail:agarwal.ashu.21@gmail.com . Or ping me at, https://www.instagram.com/prachi_diwan21/ .