0% found this document useful (0 votes)
2 views12 pages

For Loop in Python - Part 3

This document introduces loops in Python, specifically focusing on the for loop, which allows for efficient execution of code blocks multiple times over sequences like lists. It explains the syntax and provides an example of calculating total prices from a list of dishes. The document concludes with a transition to discussing while loops in future resources.

Uploaded by

nationzone27
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)
2 views12 pages

For Loop in Python - Part 3

This document introduces loops in Python, specifically focusing on the for loop, which allows for efficient execution of code blocks multiple times over sequences like lists. It explains the syntax and provides an example of calculating total prices from a list of dishes. The document concludes with a transition to discussing while loops in future resources.

Uploaded by

nationzone27
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

For Loop

in Python
Part 3
02 [Link]

Po, today we will


learn about
loops in Python.

Loops?
That sounds
like running,
Master Shifu.

Ha-ha, Po, it’s


not that kind
of loop.

It’s a
programming
concept where we
can run a block of
code several
times.

Source: Kung Fu Panda


03 [Link]

So we can write
shorter, more
concise code
that's easier to
understand and
maintain?

Yes, Po.
We can use loops to
perform repetitive
tasks, like printing
numbers.

This sounds like


a fun mission,
Master! Let’s get
started!

Source: Kung Fu Panda


04 [Link]

In Python, loops are a fundamental concept for performing repetitive tasks


efficiently. Loops allow you to execute a block of code multiple times,
which is especially useful for operations involving lists, files, or repeated
calculations. Python provides two primary types of loops: for loops and
while loops.
05 [Link]

For Loop:
The for loop is commonly used to iterate over a sequence, such as a list,
tuple, dictionary, set, or string. It executes a block of code for each item in
the sequence, making it ideal for fixed, known iterations.

Syntax:

For Loop Flow Chart:


06 [Link]

Example:

Explanation:
Dish = ["Pizza","Burger","French fries"]
Price = [100,90,30]
total_price=0

Note:
[ ] Used to access elements of the list.
index() method is used to return the position at the first occurrence of the
specified value.

Dish[1] -> Burger


[Link]("Burger") -> 1
07 [Link]

Loop for the element " Pizza " is completed


Initially total_price= 0
Now total_price = 100
We did not reach the end of the list " Dish", so now we perform a loop for
the next element "Burger"
08 [Link]

Loop for the element " Burger " is completed


Change in total_price value: 100 -> 190
We did not reach the end of the list " Dish", so now we perform a loop for
the next element "French Fries"
09 [Link]

Loop for the element " French Fries " is completed


Change in total_price value: 190 -> 220
We reached the end of the list " Dish", so now we will exit the loop
10 [Link]

Print(total_price)

Output: 220
11 [Link]

Wow! The for-loop concept is


crystal clear now—ready to
loop through every item in a
list; can we dive into the
while loop after a quick
break?

Ha Ha!
Sure, Po! I’ll
cover the while
loop in our next
resource.

Source: Kung Fu Panda

Scan the QR code for more content


join the WhatsApp Channel today!
E N A B L I N G C A R E E R S

Like this way of learning?


Check our Python learning at
[Link]

[Link]

You might also like