Skip to main content

Loops in Python - For Loop, While Loop and Nested Loops

Understanding Loops in Python: A Beginner's Guide:

Loops are a fundamental concept in programming, allowing you to repeat a block of code multiple times without writing it out repeatedly. This guide will introduce you to loops in Python, focusing on two main types: for loops and while loops. We'll also explore some practical examples to solidify your understanding.

What Are Loops?

In programming, loops are used to automate repetitive tasks. Instead of writing the same code multiple times, you can use a loop to execute it repeatedly until a certain condition is met.

1. For Loops:

A for loop in Python iterates over a sequence (such as a list, tuple, string, or range) and executes a block of code for each item in the sequence. Here's the basic syntax:



Example 1: Iterating Over a List


Output:


Example 2: Using the Range Function

The range() function generates a sequence of numbers, which is particularly useful in loops.


Example 3. Nested For Loops:

You can use a loop inside another loop, which is called a nested loop.


2.While Loops:

Basic Syntax

A while loop in Python repeats as long as a condition is true.

Syntax:

while condition:

    # code to execute

Example 1: Basic While Loop



Example 2: Using Break Statement

The break statement exits the loop immediately.


Example 3: Using Continue Statement

The continue statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration.


3.Combining For and While Loops

You can combine for and while loops to create more complex loops. Here's an example where a for loop is nested inside a while loop.

Example: Nested For and While Loop



Conclusion

Loops are an essential part of programming, allowing us to execute code repeatedly without rewriting it. Understanding for and while loops in Python, along with concepts like nested loops and loop control statements, can significantly enhance your coding skills. Practice writing different types of loops to get comfortable with them, and soon you'll be able to implement them in various programming tasks with ease.

Happy coding!


Comments

Popular posts from this blog

Jupyter Notebook Edit Mode and Command Mode

In Jupyter Notebook, there are two main modes that you can switch between: Edit Mode and Command Mode . The "ln" command is used to switch between "Full", "Edit", and "Mode" - "Mode" is the default and least restrictive mode, "Edit" allows the user to edit non-executable cells, and "Full" prevents all editing by default. You can also use the "Flip Full/Edit/Mode" shortcut (F3) to quickly toggle between these modes. Jupyter Notebook  Edit Mode: Jupyter notebook Edit Mode In a Jupyter Notebook, edit mode refers to the state where cells can be edited, and changes can be made to previously typed code or text. Itis state where you can modify the content of a cell, such as adding or editing code or text. When a cell is in edit mode, the cell's border will be green, and a cursor will appear, allowing you to type and make changes. Example: Entering Edit Mode Double-click on a cell, or press Enter while the c...

Data Analytics - What is Data Analytics, Steps in data Analytics, Purpose of Data Analytics

  Data Analytics - What is Data Analytics - Steps in Data Analytics - Purpose of Data Analytics What is Data Analytics? Data analytics is a subfield of  Data Science . Data analytics is the process of using statistical methods , algorithms and visualization techniques to analyze data to extract meaningful insights, identify different trends and answer specific questions. It helps companies, organizations and different industries to know market trends , their performance, and relationships in their data from which they can make decision making . Basically, Data analysis is done for decision-making. Process of Data Analytics: The steps included in data analytics are  Collecting the Data  Cleaning the Data  Transforming the Data Data Analysis Data Visualization Decision Making Steps Of Data Analytics 1:Collecting the Data: Collecting the data is the First step, to which we collect data from different platforms for further analysis.  2:Cleaning the Data: Clean...