While Loops

A while loop in Python is a control flow statement that allows us to repeat a block of code as long as a certain condition is met. The basic syntax for a while loop is as follows:

while condition:
    # code block to be executed

The code block inside the while loop will be executed as long as the condition is True. When the condition becomes False, the while loop will stop executing.

Here's an example of a while loop that counts from 1 to 10:

# initialize the variable i to 1
i = 1

# while the variable i is less than or equal to 10
while i <= 10:
    # print the value of i
    print(i)
    # increment i by 1
    i = i + 1

In this example, the while loop will execute 10 times, printing the numbers 1 through 10.

It's important to make sure that the condition in the while loop will eventually become False, otherwise the loop will run indefinitely and you'll have a so-called "infinite loop."

You can use the break keyword to exit a while loop prematurely. For example:

# initialize the variable i to 1
i = 1

# while the variable i is less than or equal to 10
while i <= 10:
    # print the value of i
    print(i)

    # if i is equal to 5, exit the while loop
    if i == 5:
        break

    # increment i by 1
    i = i + 1

In this example, the while loop will only execute 5 times before the break statement is reached and the loop is exited.

While loops are incredibly useful for creating menu systems.

# initialize the variable user_input to an empty string
user_input = ""

# while the user input is not "q"
while user_input != "q":
    # prompt the user for input
    user_input = input("Enter something (q to quit): ")

    # if the user input is not "q", print it
    if user_input != "q":
        print(f"You entered: {user_input}")

In this example, the while loop will continue to prompt the user for input and print the input as long as the user doesn't enter "q". When the user enters "q", the loop will exit. While loops can be used in a variety of different situations to repeat a code block as long as a certain condition is met. See you in the next lesson!

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete