SlideShare a Scribd company logo
Python Programming
Bootcamp - Week 2,
Day 2
Loops – Introduction to for Loops
Atta Muhammad
attapanhyar@quest.edu.pk
+923331971110
Quaid e Awam University of Engineering,
Science and Technology
02/03/2025 2
Learning Objectives
 Understand how for loops work in Python.
 Learn the syntax of for loops and how to use them for iteration.
 Explore practical uses of for loops to automate repetitive tasks.
 Differentiate between while loops and for loops.
Atta Muhammad Panhyar
02/03/2025 3
What is a for Loop?
 Definition: A for loop is used to iterate over a sequence (such as a list, string, or
range) and execute a block of code for each element in the sequence.
 Why Use for Loops? Simplifies code when you need to repeat actions for every
item in a sequence or range of numbers.
Atta Muhammad Panhyar
02/03/2025 4
for Loop Syntax and Example
 Syntax:
 Example:
Atta Muhammad Panhyar
02/03/2025
Atta Muhammad Panhyar
5
How for Loops Work
 Visualize the Iteration:
 The for loop picks the first item from the sequence, assigns it to the loop variable,
runs the code, then moves to the next item in the sequence.
 Example with a List:
02/03/2025
Atta Muhammad Panhyar
6
The range() Function
 What is range()? A function used to generate a sequence of numbers, commonly
used in for loops.
 range(start, stop, step)
 start: The starting number (default is 0).
 stop: The number at which the sequence stops (exclusive).
 step: The increment between numbers (default is 1).
02/03/2025
Atta Muhammad Panhyar
7
Hands-On Exercise 1
 Task: Write a program that prints all numbers from 1 to 10 using a for loop and
range().
 Hint: Use range(1, 11) to include numbers from 1 to 10.
02/03/2025
Atta Muhammad Panhyar
8
Looping Over Strings
 Explanation: Strings are sequences, so you can use a for loop to iterate over each
character in a string.
 Example:
02/03/2025
Atta Muhammad Panhyar
9
Looping Over Lists
 Example:
 Exercise: Modify the code to greet each city by adding the text "Welcome to"
before the city name.
02/03/2025
Atta Muhammad Panhyar
10
Hands-On Exercise 2
 Task: Write a program that prints the square of every number from 1 to 10 using a
for loop and range().
 Hint: Use print(num**2) inside the loop.
Nested for
Loops
 Explanation: You can place
one for loop inside another to
perform more complex tasks.
 Example:
02/03/2025
Atta Muhammad Panhyar
11
02/03/2025
Atta Muhammad Panhyar
12
Comparing for and while Loops
 while Loop:
 A while loop runs while a condition is True.
 It’s useful when you don’t know how many times you need to loop.
 for Loop:
 A for loop iterates over a known sequence or range.
 It’s useful when you know how many times you want to iterate.
02/03/2025
Atta Muhammad Panhyar
13
Comparing for and while Loops
 while Loop:
 A while loop runs while a condition is True.
 It’s useful when you don’t know how many times you need to loop.
 for Loop:
 A for loop iterates over a known sequence or range.
 It’s useful when you know how many times you want to iterate.
02/03/2025
Atta Muhammad Panhyar
14
Comparing for and while Loops
02/03/2025
Atta Muhammad Panhyar
15
Hands-On Exercise 3
 Task: Write a program that prints the multiplication table for the number 5 (from 1
to 10) using a for loop.
 Hint: Use for i in range(1, 11) and multiply i by 5 inside the loop.
02/03/2025
Atta Muhammad Panhyar
16
The break Statement in for Loops
 Explanation: The break statement allows you to exit a loop before it has looped
through all the items.
 Example:
02/03/2025
Atta Muhammad Panhyar
17
The continue Statement in for Loops
 Explanation: The continue statement skips the rest of the current iteration and
moves to the next iteration.
 Example:
02/03/2025
Atta Muhammad Panhyar
18
Hands-On Exercise 4
 Task: Write a program that prints all odd numbers between 1 and 20 using a for
loop and the continue statement.
 Hint: Skip the even numbers using continue
02/03/2025
Atta Muhammad Panhyar
19
Looping Over a Dictionary
 Explanation: You can loop over dictionaries to access keys and values.
 Example:
02/03/2025
Atta Muhammad Panhyar
20
Group Activity
 Task: Create a program that generates a list of 10 random numbers (between 1
and 100) and prints the sum and average of the numbers using a for loop.
 Hint: Use sum() and len() to calculate the average after generating the list.
02/03/2025
Atta Muhammad Panhyar
21
Common Mistakes in for Loops
 Forgetting to update the loop variable:
 In cases where a manual update is needed (like in custom ranges), ensure that the loop
variable is updated.
 Infinite loops: Avoid creating infinite loops with incorrect ranges or loop
conditions.
02/03/2025
Atta Muhammad Panhyar
22
Recap and Key Takeaways
 Key Takeaways:
 for loops are powerful for iterating over sequences like lists, strings, and ranges.
 Use range() to generate sequences of numbers.
 break and continue statements allow for more control within loops.
 Nested for loops can be used for multi-dimensional tasks.
 Next Session: Combining for loops with other structures like lists, dictionaries, and
functions.
02/03/2025
Atta Muhammad Panhyar
23
Q&A and Wrap-Up
 Any questions or clarifications.
 Recap
02/03/2025
Atta Muhammad Panhyar
24
MATCH THE SOLUTION 😒
02/03/2025
Atta Muhammad Panhyar
25
Exercise 😉
02/03/2025
Atta Muhammad Panhyar
26
Exercise ‍
♂️
‍
️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️
‍
♂️ ‍
♂️
Exercise 🤗
02/03/2025 Atta Muhammad Panhyar 27
Exercise 🤐
02/03/2025 Atta Muhammad Panhyar 28
Exercise 💎
02/03/2025
Atta Muhammad Panhyar
29

More Related Content

PPTX
Python for loop
PPTX
Python programming –part 3
PPTX
python_computer engineering_semester_computer_language.pptx
PDF
While-For-loop in python used in college
PPTX
While_for_loop presententationin first year students
PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
PPTX
Loops in python including control statements and various test cases
PDF
From Beginner to Developer: Understanding Loops in Python
Python for loop
Python programming –part 3
python_computer engineering_semester_computer_language.pptx
While-For-loop in python used in college
While_for_loop presententationin first year students
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
Loops in python including control statements and various test cases
From Beginner to Developer: Understanding Loops in Python

Similar to Python Development Workshop DAY 06 QUEST (20)

PPTX
python ppt.pptx
PPTX
Q-SPractical_introduction_to_Python.pptx
PPTX
Q-Step_WS_02102019_Practical_introduction_to_Python.pptx
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PPTX
PPT
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PDF
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
PPTX
Mastering Python lesson3b_for_loops
PPTX
Python_Loops.pptxpython loopspython loopspython loopspython loopspython loops...
PPTX
ForLoops.pptx
PPTX
The document discusses for loops in Python. It explains that for loops are us...
PDF
Python_Module_2.pdf
PDF
2 Python Basics II meeting 2 tunghai university pdf
DOCX
iterations.docx
PPTX
loopsnote................, b .pptx
PDF
PPTX
Python programming workshop session 2
PPTX
introductionpart1-160906115340 (1).pptx
PPTX
python programming control structures.pptx
PPTX
Introduction to python programming ( part-3 )
python ppt.pptx
Q-SPractical_introduction_to_Python.pptx
Q-Step_WS_02102019_Practical_introduction_to_Python.pptx
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Mastering Python lesson3b_for_loops
Python_Loops.pptxpython loopspython loopspython loopspython loopspython loops...
ForLoops.pptx
The document discusses for loops in Python. It explains that for loops are us...
Python_Module_2.pdf
2 Python Basics II meeting 2 tunghai university pdf
iterations.docx
loopsnote................, b .pptx
Python programming workshop session 2
introductionpart1-160906115340 (1).pptx
python programming control structures.pptx
Introduction to python programming ( part-3 )
Ad

More from AttaMohammadPanhyar (6)

PPTX
Lec 09-10- -Regression Implementation.pptx
PPTX
Lec 01 Introduction to Machine Learning.pptx
PPTX
Lec 02-03 Machine learning understanding key concepts.pptx
PPTX
Lec 04 -Visualization dataset using Python - Copy.pptx
PPTX
Lec 05-06- -Regression Implementation.pptx
PPTX
Lec 07-08- -Regression Implementation.pptx
Lec 09-10- -Regression Implementation.pptx
Lec 01 Introduction to Machine Learning.pptx
Lec 02-03 Machine learning understanding key concepts.pptx
Lec 04 -Visualization dataset using Python - Copy.pptx
Lec 05-06- -Regression Implementation.pptx
Lec 07-08- -Regression Implementation.pptx
Ad

Recently uploaded (20)

PPT
Drone Technology Electronics components_1
PPTX
Geodesy 1.pptx...............................................
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPT
Project quality management in manufacturing
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Practice Questions on recent development part 1.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
AgentX UiPath Community Webinar series - Delhi
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPT
Chapter 6 Design in software Engineeing.ppt
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PDF
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
Drone Technology Electronics components_1
Geodesy 1.pptx...............................................
ETO & MEO Certificate of Competency Questions and Answers
Simulation of electric circuit laws using tinkercad.pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Project quality management in manufacturing
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Practice Questions on recent development part 1.pptx
CH1 Production IntroductoryConcepts.pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
AgentX UiPath Community Webinar series - Delhi
UNIT-1 - COAL BASED THERMAL POWER PLANTS
anatomy of limbus and anterior chamber .pptx
Structs to JSON How Go Powers REST APIs.pdf
Chapter 6 Design in software Engineeing.ppt
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
OOP with Java - Java Introduction (Basics)
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...

Python Development Workshop DAY 06 QUEST

  • 1. Python Programming Bootcamp - Week 2, Day 2 Loops – Introduction to for Loops Atta Muhammad [email protected] +923331971110 Quaid e Awam University of Engineering, Science and Technology
  • 2. 02/03/2025 2 Learning Objectives  Understand how for loops work in Python.  Learn the syntax of for loops and how to use them for iteration.  Explore practical uses of for loops to automate repetitive tasks.  Differentiate between while loops and for loops. Atta Muhammad Panhyar
  • 3. 02/03/2025 3 What is a for Loop?  Definition: A for loop is used to iterate over a sequence (such as a list, string, or range) and execute a block of code for each element in the sequence.  Why Use for Loops? Simplifies code when you need to repeat actions for every item in a sequence or range of numbers. Atta Muhammad Panhyar
  • 4. 02/03/2025 4 for Loop Syntax and Example  Syntax:  Example: Atta Muhammad Panhyar
  • 5. 02/03/2025 Atta Muhammad Panhyar 5 How for Loops Work  Visualize the Iteration:  The for loop picks the first item from the sequence, assigns it to the loop variable, runs the code, then moves to the next item in the sequence.  Example with a List:
  • 6. 02/03/2025 Atta Muhammad Panhyar 6 The range() Function  What is range()? A function used to generate a sequence of numbers, commonly used in for loops.  range(start, stop, step)  start: The starting number (default is 0).  stop: The number at which the sequence stops (exclusive).  step: The increment between numbers (default is 1).
  • 7. 02/03/2025 Atta Muhammad Panhyar 7 Hands-On Exercise 1  Task: Write a program that prints all numbers from 1 to 10 using a for loop and range().  Hint: Use range(1, 11) to include numbers from 1 to 10.
  • 8. 02/03/2025 Atta Muhammad Panhyar 8 Looping Over Strings  Explanation: Strings are sequences, so you can use a for loop to iterate over each character in a string.  Example:
  • 9. 02/03/2025 Atta Muhammad Panhyar 9 Looping Over Lists  Example:  Exercise: Modify the code to greet each city by adding the text "Welcome to" before the city name.
  • 10. 02/03/2025 Atta Muhammad Panhyar 10 Hands-On Exercise 2  Task: Write a program that prints the square of every number from 1 to 10 using a for loop and range().  Hint: Use print(num**2) inside the loop.
  • 11. Nested for Loops  Explanation: You can place one for loop inside another to perform more complex tasks.  Example: 02/03/2025 Atta Muhammad Panhyar 11
  • 12. 02/03/2025 Atta Muhammad Panhyar 12 Comparing for and while Loops  while Loop:  A while loop runs while a condition is True.  It’s useful when you don’t know how many times you need to loop.  for Loop:  A for loop iterates over a known sequence or range.  It’s useful when you know how many times you want to iterate.
  • 13. 02/03/2025 Atta Muhammad Panhyar 13 Comparing for and while Loops  while Loop:  A while loop runs while a condition is True.  It’s useful when you don’t know how many times you need to loop.  for Loop:  A for loop iterates over a known sequence or range.  It’s useful when you know how many times you want to iterate.
  • 15. 02/03/2025 Atta Muhammad Panhyar 15 Hands-On Exercise 3  Task: Write a program that prints the multiplication table for the number 5 (from 1 to 10) using a for loop.  Hint: Use for i in range(1, 11) and multiply i by 5 inside the loop.
  • 16. 02/03/2025 Atta Muhammad Panhyar 16 The break Statement in for Loops  Explanation: The break statement allows you to exit a loop before it has looped through all the items.  Example:
  • 17. 02/03/2025 Atta Muhammad Panhyar 17 The continue Statement in for Loops  Explanation: The continue statement skips the rest of the current iteration and moves to the next iteration.  Example:
  • 18. 02/03/2025 Atta Muhammad Panhyar 18 Hands-On Exercise 4  Task: Write a program that prints all odd numbers between 1 and 20 using a for loop and the continue statement.  Hint: Skip the even numbers using continue
  • 19. 02/03/2025 Atta Muhammad Panhyar 19 Looping Over a Dictionary  Explanation: You can loop over dictionaries to access keys and values.  Example:
  • 20. 02/03/2025 Atta Muhammad Panhyar 20 Group Activity  Task: Create a program that generates a list of 10 random numbers (between 1 and 100) and prints the sum and average of the numbers using a for loop.  Hint: Use sum() and len() to calculate the average after generating the list.
  • 21. 02/03/2025 Atta Muhammad Panhyar 21 Common Mistakes in for Loops  Forgetting to update the loop variable:  In cases where a manual update is needed (like in custom ranges), ensure that the loop variable is updated.  Infinite loops: Avoid creating infinite loops with incorrect ranges or loop conditions.
  • 22. 02/03/2025 Atta Muhammad Panhyar 22 Recap and Key Takeaways  Key Takeaways:  for loops are powerful for iterating over sequences like lists, strings, and ranges.  Use range() to generate sequences of numbers.  break and continue statements allow for more control within loops.  Nested for loops can be used for multi-dimensional tasks.  Next Session: Combining for loops with other structures like lists, dictionaries, and functions.
  • 23. 02/03/2025 Atta Muhammad Panhyar 23 Q&A and Wrap-Up  Any questions or clarifications.  Recap
  • 26. 02/03/2025 Atta Muhammad Panhyar 26 Exercise ‍ ♂️ ‍ ️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️ ‍ ♂️
  • 27. Exercise 🤗 02/03/2025 Atta Muhammad Panhyar 27
  • 28. Exercise 🤐 02/03/2025 Atta Muhammad Panhyar 28