0% found this document useful (0 votes)
3 views13 pages

Day 11 Python For Loop

The document provides an overview of for loops in Python, explaining their purpose and syntax for iterating over sequences like lists, strings, and dictionaries. It highlights the importance of for loops for data analysts in tasks such as data cleaning, summarizing, and automating repetitive tasks. Several practical examples and assignments are included to illustrate the application of for loops in real-world scenarios.

Uploaded by

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

Day 11 Python For Loop

The document provides an overview of for loops in Python, explaining their purpose and syntax for iterating over sequences like lists, strings, and dictionaries. It highlights the importance of for loops for data analysts in tasks such as data cleaning, summarizing, and automating repetitive tasks. Several practical examples and assignments are included to illustrate the application of for loops in real-world scenarios.

Uploaded by

gloryofpast195
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Day - 11

Python - For Loop

A.I. Powered 30 Days Python Micro Course By Satish Dhawale ( Microsoft Certified Trainer )
What is a Loop?
• A loop means doing the same work again
and again automatically.

• Example:
• f you want to say “Hello” 10 times, you don't
type print("Hello") 10 times. You use a loop.

• A loop repeats a task for you — no


manual repetition.
Technical Definition
• A for loop is used to iterate (repeat) over a sequence such as:
• list
• string
• range
• tuple
• dictionary
• Python executes the loop block for each item in the sequence.

• Syntax of For Loop


for variable in sequence:
# repeated code
Why For Loop is Important for Data Analysts?
Process • Process each row in a dataset

Clean • Clean data in loops

Apply • Apply operations on multiple values

Split • Split large files

Summarize • Summarize values

Replace • Replace multiple wrong spellings

Read • Read CSV lines

Automate • Automate repetitive tasks


Example
Print 1 to 5

• for i in range(1, 6):


• print(i)

Print “Hello” 5 Times

• for i in range(5):
• print("Hello")

Looping Over a List

• fruits = ["Apple", "Banana", "Mango"]


• for item in fruits:
• print(item)
Exmaple

Looping Over a String (Character by Character)

• word = "Python"
• for letter in word:
• print(letter)

Loop with Range (Start, End, Step)

• for n in range(0, 20, 2):


• print(n)
Practical Example – Total Marks Calculation
marks = [78, 82, 90, 69]

total = 0
for m in marks:
total += m

print("Total Marks:", total)


Real Data Analyst Example – Cleaning City
Names
cities = ["mUMbai", " DELhi ", "pune", "Chennai "]

cleaned = []

for c in cities:
[Link]([Link]().title())

print(cleaned)
Data Analyst Example – Fixing Spelling Mistakes
wrong_list = ["Bengluru", "Mombai", "Kolkatta"]
correct_list = []

for city in wrong_list:


correct_list.append(
[Link]("Bengluru", "Bengaluru")
.replace("Mombai", "Mumbai")
.replace("Kolkatta", "Kolkata")
)

print(correct_list)
For Loop with If Condition
numbers = [5, 12, 3, 18, 7]

for n in numbers:
if n > 10:
print(n, "is greater than 10")
Looping Through Dictionary
student = {"name": "Satish", "age": 25, "city": "Pune"}

for key, value in [Link]():


print(key, ":", value)
Advanced Example – Extract Last 4 Digits of
Each ID
ids = ["EMP-001122", "EMP-889900", "EMP-550012"]

for emp in ids:


print(emp[-4:])
Assignments for Students

Basic Intermediate Advanced

Print numbers from 1 Clean city names list Extract last 3 digits
to 20 Count vowels in a from multiple IDs
Print each letter of word Calculate total,
your name average from marks
list

You might also like