Python 3 While Loop tutorial



The two distinctive loops we have in Python 3 logic are the "for loop" and the "while loop." Both of them achieve very similar results, and can almost always be used interchangeably towards a goal. Many times it comes down to programmer preference, or is reliant on efficiency. Generally, the for loop can be more efficient than the while loop, but not always.

The idea of the While loop is:

While something is the case, do the following block of code.

Here is an example of a while loop:

condition = 1

while condition < 10:
	print(condition)
	condition += 1
		

In this code, we have defined a variable name condition, and condition starts at a value of 1.

Next, we specify the terms of the while statement, which are : While the condition variable is less than 10, we will print the condition variable out. After printing out the condition, we will add 1 to the current condition.

This process will continue until condition equals 10.

There is usually more complex code within the while loop. If you are not familiar with what a code block is, in the case of the while loop above, the "code block" within the while loop is:

	print(condition)
	condition += 1
		

This setup of a while loop is known as creating a "counter," since basically that is what we're doing. We're saying we just want to count 1 for every iteration and eventually stop at our limit. While loops are usually finite and defined in this sense, but while loops can also be undefined. Something like:

Assuming we had something built to detect weather:

while isRaining:
	print(condition)
		

In this case, this loop would continue running while it was raining outside. When the rain stopped, the loop would cease.

Another example is what is known as the infinite loop. This is easiest done with the help of a while loop.

while True:
	print('doing stuff!!')
		

If you actually run the above code, you can stop it by doing ctrl+c to break it. The above is an intentional infinite loop.


There exists 1 challenge(s) for this tutorial. for access to these, video downloads, and no ads.


There exists 1 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial