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

Limitations of python

 Limitations of python While Python is a powerful and flexible programming language, it does have some limitations that should be considered when deciding whether to use it for a particular project. Here are some of the limitations of Python: Performance: Python is an interpreted language, which means that the code is executed line by line, rather than being compiled into machine code before execution. This can make Python programs run slower than programs written in compiled languages like C++ or Java. For performance-critical applications, Python may not be the best choice. Memory usage: Python uses dynamic typing, which means that the type of data stored in a variable can change dynamically during the runtime of a program. This can result in higher memory usage compared to statically typed languages like C++ or Java. Lack of low-level control: Python is a high-level language that provides a high level of abstraction. This makes it easy to write code quickly, but it can also limi...

TUPLE DATA TYPE IN PYTHON

 TUPLE DATA TYPE IN PYTHON: A tuple is an ordered, immutable collection of elements in Python. Tuples are often used to store multiple related pieces of information in a single structure. Here are some key points about tuples in Python: Syntax: Tuples are defined by enclosing a comma-separated list of elements within parentheses. For example: (1, 2, 3, 4). Immutable: Once a tuple is created, its elements cannot be changed. This makes tuples ideal for storing data that should not be modified. Indexing: Tuples can be indexed just like lists, with the first element having an index of 0. For example: t = (1, 2, 3); t[1] would return 2. Slicing: Tuples can be sliced just like lists, using the square bracket syntax. For example: t = (1, 2, 3); t[0:2] would return (1, 2). Nesting: Tuples can contain elements of any data type, including other tuples. For example: t = ((1, 2), (3, 4)); t[0] would return (1, 2). Unpacking: Tuples can be unpacked into individual variables. For example: t = (1...

Continue statement in Python

Continue statement in Python   The "continue" statement in Python is used within a loop to skip the rest of the current iteration and move on to the next one. This statement can be useful in cases where you want to skip a certain condition or value during the iteration, but still want to continue processing the rest of the elements. Here is an example to illustrate the use of the "continue" statement in a for loop: python Copy code for i in range ( 10 ): if i % 2 == 0 : continue print (i) In this example, the "continue" statement is used to skip the processing of all even numbers. The loop iterates over the range from 0 to 9, and for each iteration, it checks if the current number i is divisible by 2. If it is, the "continue" statement is executed and the rest of the iteration is skipped. If i is not divisible by 2, the current number is printed. The output of this code will be: Copy code 1 3 5 7 9 As you can see, all even ...