0% found this document useful (0 votes)
17 views7 pages

Data Sci Lab 4

1. The document discusses while loops in Python, explaining that they allow code to be repeatedly executed as long as a condition is true. It describes the basic syntax of while loops and compares them to for loops. 2. Examples are provided to demonstrate counting from 1 to 10 using a while loop, validating user input until it meets the condition, and using nested while loops. 3. The document concludes that while loops empower programmers to efficiently handle iterative tasks, validate user input, and more, making them an essential skill for Python developers.

Uploaded by

20jzind0168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Data Sci Lab 4

1. The document discusses while loops in Python, explaining that they allow code to be repeatedly executed as long as a condition is true. It describes the basic syntax of while loops and compares them to for loops. 2. Examples are provided to demonstrate counting from 1 to 10 using a while loop, validating user input until it meets the condition, and using nested while loops. 3. The document concludes that while loops empower programmers to efficiently handle iterative tasks, validate user input, and more, making them an essential skill for Python developers.

Uploaded by

20jzind0168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

IE- DATA SCIENCE

Lab No: 04

Submitted to: Dr. Irfaan Ahmed


Submitted By: Muhammad Sharjeel khan
Muhammad Ahmad
Mudassir Hussain
Registration No: 20JZIND0168
20JZIND0162, 20JZIND0161
Department: Industrial Engineering
Semester: 7th/Fall 2023
Marks:
GPA MARKS

OBE MARKS

LAB REPORT RUBRIC 1

LAB REPORT RUBRIC 2

University of Engineering & Technology, Peshawar


Jalozai Campus
Lab #04:

“To perform the use of while loop in Python.”


Objectives:
 To know about clear explanation of what a while loop is in Python, its purpose, and how it differs
from other loop structures like for loops.
 Explain the syntax and structure of while loops in Python, including how to set up the loop,
define conditions for its execution, and write the code block to be repeated.

Introduction:
Python, known for its simplicity and versatility, offers a range of control structures to manage the flow of
a program. Among these, the "while" loop is a fundamental construct that allows you to execute a block
of code repeatedly as long as a certain condition remains true. In this article, we'll delve into the world of
"while" loops in Python, exploring their basics, working mechanism, advantages, common use cases, and
practical examples.

Differences Between While Loops and For Loops:


Python also provides "for" loops for iteration. The key difference is that "for" loops are typically used
when you know the number of iterations beforehand, while "while" loops are employed when you need to
loop until a particular condition is met.

Basics of While Loops:


Syntax:
To initiate a "while" loop in Python, you need to follow this basic syntax:

while condition:
Code to be executed if the condition is true.

Example:
Here's a simple example that prints numbers from 1 to 5 using a "while" loop:
counter = 1
while counter <= 5:
print(counter)
counter += 1
In this example, the condition counter <= 5 is evaluated before each iteration. As long as it's true, the code
block within the loop is executed.

Advantages of Using While Loops:


"While" loops offer several advantages, such as:

 Flexibility in handling complex conditions.


 The ability to execute a block of code an indefinite number of times.
Iterating through data until a specific condition is met.

The Working Mechanism of While Loops:


A "while" loop has three key components:

Initialization:
You start by initializing a variable or setting up the conditions that the loop relies on. This is usually done
before the loop itself.

Condition:
The loop continues to execute as long as the condition specified in the loop header remains true.

Iteration:
Inside the loop, you perform actions or changes that bring you closer to the condition becoming false.
This is often achieved through increments or decrements.

Best Practices for Using While Loops:


 Avoiding Infinite Loops
 Proper Indentation

Procedure:
 Describe the basic syntax of a while loop in Python.
while condition: Code to be executed repeatedly.

 Explain that the loop starts by evaluating the "condition." If the condition is True, the code block
is executed. This process continues until the condition becomes False.
 Define the initial value of variables used in the condition. False at some point to avoid infinite
loops.
 Emphasize the indentation to maintain proper code structure.
 Explain that while loops will continue to execute as long as the condition remains True.
 Describe how to ensure the loop terminates, when necessary, for example, by modifying the
condition within the loop.

Examples:
Counting with a While Loop:
Suppose you want to count from 1 to 10. You can use a "while" loop for this purpose.
count = 1
while count <= 10:
print(count)
count += 1
Fig #4.1(counting with while loop)

User Input Validation:


Using a "while" loop, you can validate user input until it meets a specific condition.
user_input = input("Enter a number greater than 5: ")
while int(user_input) <= 5:
user_input = input("Please enter a number greater than 5: ")

Fig #4.2(validating input)


Nested While Loops
rows = 5
outer_counter = 0
while outer_counter < rows:
inner_counter = 0
while inner_counter < rows:
print("*", end=" ")
inner_counter += 1
print()
outer_counter += 1

Fig #4.3(validating input)

While Loop with "else" Statement:


In this example, we use a while loop to check if a number is prime. The "else" statement is associated
with the while loop. If the loop completes without finding a divisor, the "else" block is executed,
indicating that the number is prime.
Fig #4.4(While Loop with "else" Statement)

Fig #4.5(entering number 4)


Results and discussion:
In this lab the objectives were discussed and the differences between while and for loops, emphasizing the
importance of selecting the appropriate loop for different scenarios. By providing practical examples,
we've illustrated how while loops can be applied in real-world coding situations. In conclusion, mastering
while loops in Python empowers programmers to handle iterative tasks efficiently, validating user input,
and more, making it an essential skill for any Python developer.

Conclusion:
In Python, "while" loops are a powerful tool for iterating through code if a specified condition holds true.
They provide flexibility, allowing you to handle various scenarios, including user input validation,
repetitive tasks, and more. By understanding the basics and best practices of "while" loops, you can
harness their potential for efficient and effective programming.

You might also like