Nothing Special   »   [go: up one dir, main page]

Data Sci Lab 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

IE- DATA SCIENCE

Lab No: 05

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 #05:

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


Objectives:
 To know about clear explanation of what a for loop is in Python, its purpose, and how it differs
from other loop structures like for loops.
 Explain the syntax and structure of for 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
“for" 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.

What is a For Loop in Python?


In Python, a for loop is used to iterate over a sequence, such as a list, tuple, string, or other iterable
objects. It allows you to perform a set of statements for each item in the sequence.

Example of a Simple For Loop:


Let's start with a simple example. Suppose we have a list of numbers, and we want to print each number
using a 'for' loop:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
This code will iterate through the 'numbers' list and print each number one by one.

Efficiency and Optimization:


It's important to optimize 'for' loops when dealing with large datasets. Using efficient algorithms and data
structures can significantly improve the performance of your code.

Common Mistakes to Avoid:


When using 'for' loops, beginners often encounter common mistakes like off-by-one errors, infinite loops,
or modifying the iterable during iteration. Be cautious and thoroughly test your code.
Procedure:
 Start with the for keyword, followed by a variable (iterator).
 Use the in keyword to specify the iterable you want to loop through.
 End the line with a colon : to define the code block.
 Indent the code block, which contains the instructions to be executed in each iteration.

Fig #5.1(simple for loop)

 Iterating Through Lists

Fig #5.2(Iterating through lists)


 Iterating Through Dictionaries

Fig #5.3(Iterating through dictionaries)

 Using the Range() Function. The range() function is commonly used to create a sequence of
numbers. It's especially useful when you want to iterate a specific number of times or generate a
range of values. This code prints numbers from 1 to 10.

Fig #5.4(range function)

 Nested 'for' loops are used to create more complex iterations. You can loop through multiple
sequences simultaneously, allowing for more intricate data processing:
Fig #5.5(Nested for loops)

Results and discussion:


The 'For' Loops in Python procedure offers a user-friendly guide for learners and experienced developers.
It explains the basics and demonstrates the versatility of 'for' loops with clear examples. It also covers
advanced techniques like the range() function and nested loops. Real-world applications illustrate
practical uses. The inclusion of common mistakes and optimization tips equips programmers with a
strong foundation for effective 'for' loop utilization in Python.

Conclusion:
'For' loops are fundamental to Python programming, offering a straightforward way to automate tasks and
process data. Mastering 'for' loops will empower you to write more efficient and effective code, making
your programming endeavors easier and more productive.

You might also like