Loops are fundamental constructs in virtually every programming language, including Python. They allow us to execute a block of code repeatedly, which is incredibly useful when we want to perform the same operation multiple times without needing to write the same lines of code over and over again. In Python, the two primary types of loops are for
loops and while
loops. Let’s take a closer look at each.
for
LoopThe for
loop in Python is used to iterate over a sequence (that could be a list, tuple, dictionary, set, or string) and execute a block of code multiple times. This loop is particularly useful when the number of iterations is known ahead of time.
for variable in sequence: # code block to execute
Let’s consider a simple example where we want to print each number in a list:
numbers = [1, 2, 3, 4, 5] for number in numbers: print(number)
In this example, the for
loop iterates through each item in the numbers
list. The variable number
takes on the value of each item in the list during each iteration, and the print(number)
statement is executed for each one, resulting in:
1
2
3
4
5
range()
with For Loop:The range()
function can be very handy when you want to execute a loop a specific number of times.
for i in range(5): print(i)
This will print:
0
1
2
3
4
while
LoopThe while
loop is another type of loop in Python. Unlike the for
loop, the while
loop continues to execute as long as a certain condition is True
. This means that the number of iterations may not be known beforehand.
while condition: # code block to execute
Here’s an example of a while
loop that continues to prompt the user until they enter the correct password:
password = "" correct_password = "1234" while password != correct_password: password = input("Enter the password: ") print("Access granted!")
In this example, the code block inside the while
loop will keep asking the user to enter the password until they type in "1234". Once the password is correct, the loop will exit, and "Access granted!" will be printed.
Both for
and while
loops can be combined with control statements such as break
and continue
to enhance their functionality.
break
Statement: This is used to exit the loop prematurely.continue
Statement: This can skip the current iteration and proceed to the next one.break
:Here’s an example that uses the break
statement:
for i in range(10): if i == 5: break print(i)
This will print:
0
1
2
3
4
The loop terminates as soon as the value of i
reaches 5.
continue
:Now, let’s look at continue
:
for i in range(5): if i == 2: continue print(i)
This will print:
0
1
3
4
When i
equals 2, the loop skips the print(i)
statement for that iteration, hence it is not printed.
With these crucial concepts of for
and while
loops, you should now feel more confident in using them effectively in your Python programming tasks.
By mastering loops, you’ll unlock a powerful tool that will make your code more efficient, readable, and concise. Happy coding!
25/09/2024 | Python
05/11/2024 | Python
22/11/2024 | Python
08/11/2024 | Python
22/11/2024 | Python
22/11/2024 | Python
08/11/2024 | Python
08/11/2024 | Python
06/12/2024 | Python
08/12/2024 | Python
22/11/2024 | Python
21/09/2024 | Python