While Loop and For Loop

0
8كيلو بايت

In Python, while and for loops are fundamental constructs for repeated execution of code blocks. They serve different purposes, so understanding their strengths is crucial for writing efficient and readable code.

While Loop:

  • Syntax:

Python
while condition:
    # Code to execute as long as the condition is True

  • Functionality:

    • The while loop repeatedly executes a block of code as long as a certain condition remains True.
    • The condition is evaluated at the beginning of each loop iteration.
    • If the condition becomes False, the loop terminates.
  • Example:

Python
count = 0
while count < 5:
    print(f"Count: {count}")
    count += 1  # Increment counter

This loop prints "Count:" followed by the current value of count five times. The loop continues as long as count is less than 5.

For Loop:

  • Syntax:

Python
for item in iterable:
    # Code to execute for each item in the iterable

  • Functionality:

    • The for loop iterates over elements in a sequence (like a list, tuple, or string) called an iterable.
    • In each iteration, the current element is assigned to the loop variable (item in this example).
    • The code block is executed for each element in the iterable.
  • Example:

Python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(f"I like {fruit}.")

This loop iterates over the fruits list. In each iteration, the current fruit (e.g., "apple") is assigned to fruit, and the message is printed.

Choosing the Right Loop:

  • Use a while loop when you don't know the exact number of iterations beforehand, and the loop continues based on a condition.
  • Use a for loop when you need to iterate over a sequence of elements in a known order. It's generally more concise and readable for this purpose.

Additional Considerations:

  • You can use the break statement to exit a loop prematurely.
  • The continue statement skips the current iteration and moves to the next one.
  • for loops can sometimes be rewritten as while loops (and vice versa), but using the appropriate loop for the situation improves code clarity.
البحث
الأقسام
إقرأ المزيد
Technology
Microsoft Access Basics & Database Fundamentals
Best Practices for Microsoft Access
بواسطة Mpatswe Francis 2024-08-14 21:45:18 0 8كيلو بايت
التعليم
MODERN AFRICAN NATIONAL HISTORY MADE EASY
https://acrobat.adobe.com/id/urn:aaid:sc:EU:4838a935-5393-417a-a9b9-a4c21d6109cb
بواسطة Landus Mumbere Expedito 2024-07-18 10:47:57 0 11كيلو بايت
Technology
Network topologies
Network topology refers to the arrangement of various elements (like nodes and links) in a...
بواسطة Mpatswe Francis 2024-10-03 22:58:40 0 9كيلو بايت
Technology
History of Python and Computer Hardware
As computer programming started to get popular and evolve in the 1960’s and 70’s,...
بواسطة Python for Everybody - Full University Python Course Code 2024-07-16 20:52:57 0 11كيلو بايت
Technology
Reasons why we should all learn Ms Excel
Learning Microsoft Excel is beneficial for several reasons:   1. **Data Management**:...
بواسطة Mpatswe Francis 2024-07-16 06:35:02 0 10كيلو بايت
Tebtalks https://tebtalks.com