EMAIL SUPPORT

dclessons@dclessons.com

LOCATION

US

Python-Loops

Python-Loops

While-Loop

Depends upon condition True or False, while loop is used to execute a group of statements several times.

Syntax of While loop is given below

While condition:

        Statements

In above syntax, while checks the condition and if the result is true, the statements is executed written below ( : ) and it will execute several times, till the condition is true. As soon as condition is false, python Interpreter exits out from loop statements.

Example1: Write a Python Program, to display number from 1 to 20 using While loop.

x=1                                                                                                                                                                          while x<=20:                                                                                                                                                                 print("print value of x:", x)                                                                                                                                         x=x+1                                                                                                                                                                  print("end")                                                                                                                                                              Output:=====================                                                                                                                        print value of x: 1                                                                                                                                                    print value of x: 2                                                                                                                                                      print value of x: 3                                                                                                                                                    print value of x: 4                                                                                                                                                    print value of x: 5                                                                                                                                                    print value of x: 6                                                                                                                                                      print value of x: 7                                                                                                                                                    print value of x: 8                                                                                                                                                  print value of x: 9                                                                                                                                                    print value of x: 10                                                                                                                                                  print value of x: 11                                                                                                                                                  print value of x: 12                                                                                                                                                  print value of x: 13                                                                                                                                                    print value of x: 14                                                                                                                                                    print value of x: 15                                                                                                                                                    print value of x: 16                                                                                                                                                    print value of x: 17                                                                                                                                                    print value of x: 18                                                                                                                                                    print value of x: 19                                                                                                                                                  print value of x: 20                                                                                                                                                    end

Example 2: Write a Python Program to display even number between 0 to100.

x=0                                                                                                                                                                          while x<=100:                                                                                                                                                                if(x%2==0):                                                                                                                                                                    print("print value of x:", x)                                                                                                                                  x=x+1                                                                                                                                                                print("end")

For Loop

For loop works with sequence like string, List, tuple, range. Based on result on condition of For loop, if condition is true, the for loop statements is executed in sequence. Below is the syntax of For Loop.

For var in sequence:

       Statements

Example 3: Write a program to display character in string using for loop.

x='dclessons'                                                                                                                                                            ch=len(x)                                                                                                                                                                  for y in range(ch):                                                                                                                                                          print("print character:", x[y])                                                                                                                              Output:=======================                                                                                                                    print character: d                                                                                                                                                      print character: c                                                                                                                                                      print character: l                                                                                                                                                        print character: e                                                                                                                                                    print character: s                                                                                                                                                    print character: s                                                                                                                                                      print character: o                                                                                                                                                      print character: n                                                                                                                                                      print character: s

Example 4: Write a program to print the numbers from 15 to 1 in descending order.

for i in range(15, 0, -1):                                                                                                                                                 print(i)                                                                                                                                                                Output:==============                                                                                                                                        15                                                                                                                                                                            14                                                                                                                                                                            13                                                                                                                                                                            12                                                                                                                                                                            11                                                                                                                                                                              10                                                                                                                                                                             9                                                                                                                                                                               8                                                                                                                                                                             7                                                                                                                                                                               6                                                                                                                                                                               5                                                                                                                                                                               4                                                                                                                                                                               3                                                                                                                                                                               2                                                                                                                                                                               1

In this we need to understand the range () command.

This range () function is used to provide the sequence of numbers. Range (n) gives number from 0 to n-1. Let’s suppose if we write range (15), it will return the value from 0 to 14. Now if we want to return the value from 3 to 10 then we should mention range (3, 11). Here you can also include the step size, means it will display or return te value based on step size.

Example if we write range (1, 10, 3) it will return the value like 1,3,6,9.

Example 5: Write a program to display and find the sum of a list of numbers using loop.

list = [11,12,13,14,15,16]                                                                                                                                          sum = 0                                                                                                                                                                    for i in list:                                                                                                                                                                     print(i)                                                                                                                                                                       sum=sum+i                                                                                                                                                          print("total sum is :", sum)                                                                                                                                        Output:========================                                                                                                                11                                                                                                                                                                              12                                                                                                                                                                            13                                                                                                                                                                            14                                                                                                                                                                          15                                                                                                                                                                            16                                                                                                                                                                            total sum is : 81

Infinite Loop

It is the loop that executes forever and never terminates. It can terminate when you use CTRL+C from keyboard. Below is the example that shows how to use Infinite loop.

X =1

While x<=10:

      Print(i)

Nested loop

When a loop is used inside anther loop, we call it Nested Loop. Let’s understand the nested Loop by an example.

Example 6: Write a Python Program to display * in a right angle triangle.

*

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

* * * *  * * * *


Comment

    You are will be the first.

LEAVE A COMMENT

Please login here to comment.