Skip to main content

Loop Statements


Loop STATEMENTS
The Loop statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements.


THE FOR LOOP
The for loop is the easiest to understand of the python loops.

Syntax:

For I in range(lowerlimit,upperlimit):

Loop Body
The program will run until I value becomes equal to upperlimit – 1.
Example:
For I in range(1,5):
Print(“Hello”)

This program will print Hello 4 times, ie from 1 to 4 (upper limit-1) 
I value will start as 1 then loop body will get executed and I value will be incremented. This keeps on taking place until I ==upper limit-1 . After that
the program comes out of loop body.

THE WHILE LOOP

The second loop available in python is the while loop. The while loop is an entry controlled loop.
The syntax of a while loop is
while (expression):
Loop-body
Where loop body keep on getting executed until the expression becomes false. When the expression becomes false, the program control passes to
the line after the loop-body code.
When to use FOR LOOP? 
FOR LOOP is appropriate when you know in advance how many times loops will be executed
When to use WHILE LOOP?
WHILE loop should be preferred when you don’t know how many times
the loop will be executed.

Download Python Learning app from APK Monster Z

Comments

Popular posts from this blog

Features of python

  Python   is a dynamic, high-level, free open source, and interpreted programming language. It supports object-oriented programming as well as procedural-oriented programming. In Python, we don’t need to declare the type of variable because it is a dynamically typed language. For example, x = 10 Here, x can be anything such as String, int, etc. Features in Python There are many features in Python, some of which are discussed below – 1. Easy to code: Python is a high-level programming language. Python is very easy to learn language as compared to other languages like C, C#, Javascript, Java, etc. It is very easy to code in the python language and anybody can learn python basics in a few hours or days. It is also a developer-friendly language. 2. Free and Open Source: Python language is freely available on the official website and you can download it from the given download link below click on the  Download Python  keyword. Download Python Since it is open-source, thi...

Using input command in python

Today i will show you that how can you use input command in python language, so we will write a simple program that will ask for any name or value and then print that name or value. So here is our simple program. a= input ( "Enter your name" ) print (a) as you can see we have declared "a" a variable that will ask for input "Enter your name" and then print command will print the variable "a". So anything we will type will be printed. As show in the picture below. But here you have to remember, if you are working with integers for some mathematical work than you have to declare input type as integer because then python will take input type as integer and not as word. As show in the picture below. As you can see the syntax that how can we declaring the input type,,int, we have to write int then in brackets we have to enter the input command, int stands for integer and it will declare our input as integer, we have and in print command we have instruc...

Why Learn Python?

Python is an easy-to-learn programming language that has some really useful features for a beginning programmer. The code is quite easy to read when compared to other programming languages, and it has an interactive shell into which you can enter your programs and see them run.  In addition to its simple language structure and an interactive shell with which to experiment, Python has some features that greatly augment the learning process and allow you to put together simple animations for creating your own games. One is the turtle module, inspired by Turtle graphics (used by the Logo programming language back in the 1960s) and designed for educational use. Another is the tkinter module, an interface for the Tk GUI toolkit, which provides a simple way to create programs with slightly more advanced graphics and animation.