Using While Loop | Development | Online Course With Appy Pie Academy

using while loop

Lesson Details:
June 29, 2020

I: Introduction

A: Learn the basics of programming languages with Python

Python is a high-level programming language. It is an easy to learn language. It has a very simple syntax. It is also quite readable. It is also case sensitive which means that uppercase and lowercase letters are treated differently. It was developed in 1991 by Guido Van Rossum.

II: Body

A: Using while loop

A while loop is used to execute a block of statements repeatedly based on a boolean condition. The boolean condition is checked at the beginning of each iteration. If it evaluates to True, the statements are executed one after another, otherwise, next iteration begins.

Boolean values are either True or False. A boolean variable can be used to hold an evaluation value of a boolean operator, or it can be assigned directly.

Here’s an example of using a while loop: suppose we need to print “hello world” ten times on the screen. We can do this using a while loop as follows:

Example 1: Using while loop in python

#!/usr/bin/python3 # This code is contributed by #Ritesh Raj Sarraf import sys #This statement checks if there are arguments passed to this program if len(sys.argv) != 2 : print ("Usage : python test .py < string>") exit() # local variables message = sys.argv[1] message = message.rstrip("r") # Printing hello world ten times for i in range(10): print (message) # This statement checks if there are more lines in the file else if i < 9 : # This statement prints the current line in the file print(message) # opening the file f = open("test.txt", "w") # printing lines from the file until end of file f.write("Hello World" + " r

") #closing the file f.close() # opening the file f = open("test.txt", "r") # reading lines until end of file while True : line = f.readline() if not line: break print(line) f.close()

When the above code is compiled and executed, it produces the following output:

III: Conclusion

Python is derived from C programming language. It was developed in 1991 by Guido Van Rossum. It is free to use and distribute, so it is preferred by many programmers. This article explains how to use loops in python.

Course content