Skip to main content

Iterative loops in python for and While loop

Loops in python 


Loops in Python are used to execute a block of code repeatedly, until a certain condition is met. There are two types of loops in Python: while loops and for loops.

While Loops

A while loop in Python continues to execute a block of code as long as a certain condition is True. The basic syntax of a while loop is as follows:

while condition: # code to be executed as long as the condition is True

Here, condition is a boolean expression (i.e., it evaluates to either True or False). The code inside the indented block will be executed repeatedly, until the condition becomes False.


Here's a simple example of using a while loop to count down from 10:

count = 10 while count > 0: print(count) count -= 1 print("its bilyat")

In this example, the while loop continues to execute as long as count is greater than 0. Each iteration of the loop decrements count by 1, until it reaches 0. When the condition is False, the code inside the loop is skipped, and the message "Blast off!" is printed.


It's important to ensure that the condition in a while loop will eventually become False, otherwise the loop will continue to execute indefinitely, creating an infinite loop.



For Loops

A for loop in Python is used to iterate over a sequence of elements, such as a list, tuple, or string. The basic syntax of a for loop is as follows:

for element in sequence: # code to be executed for each element in the sequence


Here, element is a variable that takes on the value of each element in the sequence, one at a time, and the code inside the indented block is executed for each element.

Here's a simple example of using a for loop to print the elements of a list:

fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(fruit)

In this example, the for loop iterates over the elements of the list fruits, and the message fruit is printed for each element in the list.

The for loop is a more concise and convenient way to iterate over elements in a sequence, as compared to a while loop.


Conclusion

In conclusion, while loops and for loops are both important tools in Python for executing a block of code repeatedly. The while loop is used to execute a block of code as long as a certain condition is True, while the for loop is used to iterate over a sequence of elements. Understanding how to use these loops effectively is essential for writing efficient and effective Python code.



By itsbilyat

Comments

Popular posts from this blog

File Handling in Python

  File Handling in Python File handling is an essential part of programming, and Python provides several built-in functions to handle files. This blog provides an overview of file handling in Python and various file operations like reading, writing, and appending a file. What is a File? A file is a collection of data stored on a disk that has a specific name and a path. A file can contain text, images, videos, or any type of data. There are different types of files, including text files, binary files, CSV files, and JSON files, to name a few. Why do we need File Handling in Python? In real-world applications, data is stored in a file and not in memory. So, it's important to handle the data stored in a file. File handling in Python provides several built-in functions to handle files. These functions help us to perform different operations on files, such as reading, writing, and appending data. How to open a File in Python? In Python, we can open a file using the open() function. The...

Conditional statements in Python if, elif, and else.

  Conditional statements in Python if , elif , and else . Conditional statements in Python allow you to execute certain blocks of code based on whether a certain condition is met. The most commonly used conditional statements are if , elif , and else . Here's the syntax for an if statement in Python: if condition: # code to execute if condition is True If the condition in the if statement is True , the code inside the indented block following the if statement is executed. If the condition is False , the code in the block is skipped. Here's an example that demonstrates the use of an if statement: x = 5 if x > 0: print("x is positive") In this example, the variable x is assigned the value 5 . The if statement tests whether x is greater than 0, which is True , so the code inside the indented block is executed and the message "x is positive" is printed. The elif statement is used to test multiple conditions in a single if statement. Here...