This Jupyter Notebook contains various examples demonstrating the use of while
loops in Python. Each example illustrates different use cases, including summation, iteration, conditional checks, and list manipulation.
-
Summation from 1 to 50:
- A
while
loop is used to calculate the cumulative sum of numbers from 1 to 50. - The result is printed at each step.
- A
-
Print Numbers from 15 to 30:
- A simple
while
loop prints numbers from 15 to 30.
- A simple
-
Summation from 50 to 100:
- This example calculates the cumulative sum of numbers from 50 to 100 using a
while
loop. - The result is printed at each step.
- This example calculates the cumulative sum of numbers from 50 to 100 using a
-
Print Even Numbers from 1 to 20:
- A
while
loop is used to iterate through numbers from 1 to 20, printing only the even numbers.
- A
-
Factorial Calculation:
- The user inputs a number, and the program calculates its factorial using a
while
loop.
- The user inputs a number, and the program calculates its factorial using a
-
Sum of Digits:
- The user inputs a number, and the program calculates the sum of its digits using a
while
loop.
- The user inputs a number, and the program calculates the sum of its digits using a
-
List Manipulation:
- Various examples of list manipulation using
while
loops, including:- Accessing and modifying list elements.
- Reversing a list.
- Finding the maximum value in a list.
- Various examples of list manipulation using
-
Infinite Loop with Break and Continue:
- Examples of using
break
andcontinue
statements within an infinitewhile
loop to control the flow of execution.
- Examples of using
-
Run the Notebook:
- Open the notebook in a Jupyter environment (e.g., Jupyter Notebook, JupyterLab, or Google Colab).
- Run each cell sequentially to see the output of each example.
-
Modify and Experiment:
- Feel free to modify the code in each cell to experiment with different inputs and conditions.
- For example, change the range of numbers or the conditions in the loops to see how the output changes.
- Python 3.x
- Jupyter Notebook or any compatible environment (e.g., Google Colab)
- Mahin
- Date: 06/11/2024