• Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials – Company wise
      • Infosys Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Dell Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • TCS Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation – E Books
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Register
    • Login
  • [email protected]
  • ..
Login / Register
Apply Now
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
0

Cart

while loop in Python

  • January 27, 2020
  • CODE OF GEEKS
  • 0


The while loop in Python provide us the utility to execute a particular set of statements repeatedly until the required condition is fulfilled.

Syntax

while(conditions):
    Statements

How it works

Firstly, Python interpreter looks out for the condition, if the given condition is True, then it will execute all the statements inside the loop. Once all the statements gets executed, it will go back and check the condition again. It will keep doing so, as long as condition remains True. And when this condition gets false, control is send out of the loop.

Let us code an example to perform the implementation of while loop.

Program : Program to display the counting from 1 to 10 using while loop.

i = 1
# loop to print counting from 1 to 10
while i <= 10:
    print(i, end=' ')
    i += 1
click to zoom

Let us consider one more program using while loop

Program : To evaluate the sum of digits of a number.

n = 23417
sums = 0
# loop to traverse each digit of a number
while n > 0:
    # finding remainder when number is divided by 10
    rem = n % 10
    # adding the remainder obtained 
    sums = sums + rem
    # dividing the number by 10 to access next digit of a number
    n //= 10
print("Sum of digits : ", sums)
click to zoom

Explanation

Let the number be 23417 so our task is to evaluate 2+3+4+1+7. We can do so by finding the remainder of a number(adding it to sum),divide it with 10 until the number gets 0. For this we need a ‘while’ loop which will run till ‘n>0’.In each iteration we perform three steps :
1. rem=n%10; //rem=23417%10=7
2. sum=sum+rem; //sum=7
3. n=n/10; //num=23417/10=2341
Hence on repeating these steps(till n>0), we get the sum of digits of a number.

Hope you now got the complete picture.

Do you know ?

We can make use of continue statements to skip the current iteration of loop, pass statements to simply pass the iteration, or even break statements to pull the control out of the loop for a given condition, all inside while loop. We will see them in upcoming lessons.

Cook This !

Write a Python program to check whether a given year is a leap year or not. Try to take the input from the user itself.

That’s all, see you in the next lecture.





Tags: different loops in pythonPython while loopPython while loopswhile loop in Pythonwhile loop pythonworking of while loop in pythonworking of while loop python
  • Previous For loop in Python
  • Next Looping in Python

Leave a Reply Cancel reply

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

Table of Content – Python

  1. Introduction to Python Programming
    • Python and its features
    • Key Differences : C vs Python
    • Key Differences : Java vs Python
    • Python Flavors
    • PVM and Memory Management in Python

  2. Python Programming Fundamentals
    • 'Hello World' using Python
    • Comments in Python
    • Variables and Garbage Collection in Python
    • Datatypes in Python
    • Determining the Datatype of Literal | type() method
    • Tokens in Python
    • Keywords in Python
    • Identifiers and Naming Convention in Python
    • Literals in Python

  3. Python Operators
    • Operators in Python
    • Python Mathematical Functions

  4. Python User Inputs
    • Taking Inputs | input() function in Python

  5. Python Control Statements
    • Control Statements in Python
    • If-elif-else Statements in Python
    • Looping in Python
    • while loop in Python
    • for loop in Python
    • Infinite loops in Python
    • Nested loops in Python
    • Break Statement in Python
    • Continue Statement in Python
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

What is Cloud Computing | Cloud Computing Applications | Cloud Development Models| SaaS vs PaaS vs IaaS
  • October 23, 2021
What is Amazon Web Services | AWS Regions & AZs | Different AWS Services
  • October 19, 2021

Copyright 2021 CODE OF GEEKS. All Rights Reserved.

  • →