Lädt...


🔧 Python While Loop tutorial


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The while loop in Python allows you to execute a block of code repeatedly as long as a certain condition is true. The syntax for a while loop is as follows:

while condition:
    # block of code

The block of code will be executed as long as the condition is true. Once the condition becomes false, the program will move on to the next line of code after the while loop.

Example - Counting with a While Loop

One common use case for a while loop is to count up to a certain number. Here's an example:

count = 1

while count <= 5:
    print(count)
    count += 1

In this example, we initialize the count variable to 1. Then, we enter a while loop with the condition count <= 5. As long as count is less than or equal to 5, the block of code will execute.

Inside the block, we print the current value of count and then increment it by 1 using the += operator. When count becomes 6, the condition becomes false and the program exits the while loop.

The output of this program will be:

1
2
3
4
5

Example - User Input with a While Loop

Another use case for a while loop is to repeatedly ask the user for input until they provide a valid response. Here's an example:

name = ""

while not name:
    name = input("What is your name? ")

print("Hello, " + name + "!")

In this example, we initialize the name variable to an empty string. Then, we enter a while loop with the condition not name. As long as name is an empty string, the block of code will execute.

Inside the block, we use the input() function to ask the user for their name. If the user enters a non-empty string, the condition not name becomes false and the program exits the while loop.

Finally, we print a greeting message using the name variable.

The output of this program will depend on the user's input.

...

🔧 Python While Loop Tutorial #Day3


📈 32.19 Punkte
🔧 Programmierung

🔧 Python While Loop Tutorial #Day3


📈 32.19 Punkte
🔧 Programmierung

🔧 Python While Loop tutorial


📈 32.19 Punkte
🔧 Programmierung

🕵️ Google TensorFlow up to 2.1.3/2.2.2/2.3.2/2.4.1 lite/kernels/while.cc While infinite loop


📈 27.8 Punkte
🕵️ Sicherheitslücken

🎥 How To Use META AI (Complete Tutorial) Beginner Tutorial (LLAMA 3 Tutorial)


📈 24.65 Punkte
🎥 Video | Youtube

🔧 Python Day-9 Predefined modules,While loop,Task


📈 23.97 Punkte
🔧 Programmierung

🔧 Python One Line While Loop


📈 23.97 Punkte
🔧 Programmierung

🐧 Python: While-Loop


📈 23.97 Punkte
🐧 Server

🔧 Python While Loop Examples


📈 23.97 Punkte
🔧 Programmierung

🔧 Python While Loop Examples


📈 23.97 Punkte
🔧 Programmierung

🍏 Grab up to 70% off Solo Loop and Braided Solo Loop Apple Watch bands


📈 19.65 Punkte
🍏 iOS / Mac OS

🔧 Day 16 - Slicing,for loop and nested loop


📈 19.65 Punkte
🔧 Programmierung

🔧 Dynamic Tests in Cypress: To Loop or Not To Loop


📈 19.65 Punkte
🔧 Programmierung

🪟 What is the new ‘Board visualization’ for Loop Tables in Microsoft Loop?


📈 19.65 Punkte
🪟 Windows Tipps

📰 (g+) Microsoft Loop im Test: Mit Loop-Elementen zur besseren Zusammenarbeit


📈 19.65 Punkte
📰 IT Nachrichten

📰 Microsoft Loop: Was genau ist die Loop Komponente?


📈 19.65 Punkte
🖥️ Betriebssysteme

🕵️ QEMU 5.0.0 TD List hw/usb/hcd-ohci.c Infinite Loop infinite loop


📈 19.65 Punkte
🕵️ Sicherheitslücken

📰 The Loop Kodi Addon: How to Install Loop for Kodi


📈 19.65 Punkte
🖥️ Betriebssysteme

🔧 How to Check the Python Version While Working with Python


📈 19.3 Punkte
🔧 Programmierung

🔧 Exploring While and DoWhile in Java: Master Loop Structures with Practical Examples


📈 18.81 Punkte
🔧 Programmierung

🔧 No more for/while loop in JavaScript


📈 18.81 Punkte
🔧 Programmierung

🔧 While Loop


📈 18.81 Punkte
🔧 Programmierung

🔧 while loop - task


📈 18.81 Punkte
🔧 Programmierung

🔧 do while loop


📈 18.81 Punkte
🔧 Programmierung

🔧 While loop.


📈 18.81 Punkte
🔧 Programmierung

🔧 31.do while Loop


📈 18.81 Punkte
🔧 Programmierung

🔧 While loop C++


📈 18.81 Punkte
🔧 Programmierung

matomo