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

For Loops

The document explains how to use for loops in Python to iterate over lists, including printing company names and calculating total revenues. It also demonstrates how to loop through characters in a string and provides a practice opportunity for multiplying elements in a list. The solution to the practice problem is included, showcasing the use of both a for loop and the math module's product function.

Uploaded by

Amrita Priyam
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 views3 pages

For Loops

The document explains how to use for loops in Python to iterate over lists, including printing company names and calculating total revenues. It also demonstrates how to loop through characters in a string and provides a practice opportunity for multiplying elements in a list. The solution to the practice problem is included, showcasing the use of both a for loop and the math module's product function.

Uploaded by

Amrita Priyam
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 Loops about:srcdoc

# Let's define a python list that contains 4 different company names - A, B, C, and D
company_names = ['Company A', 'Company B', 'Company C', 'Company D']
company_names

['Company A', 'Company B', 'Company C', 'Company D']

# Now we want to print all company names listed in "company_names" list


# We can repeat the print function several times as shown below
# This strategy involves writing many lines of code that perform the same "print" operation
# Recall that python lists start with index = 0
print(company_names[0])
print(company_names[1])
print(company_names[2])
print(company_names[3])

Company A
Company B
Company C
Company D

# Alternatively, we can use loops to generate the same output


# We can loop over company_names list and print them to the screen using for loops
# Note that "i" is a temporary variable that is used within the "For" loop to carry company names
for i in company_names:
print(i)

1 of 3 08/04/26, 3:36 pm
For Loops about:srcdoc

Company A
Company B
Company C
Company D

# We can also loop over a Python list that contains integers or floating points
# Let's define another python list that contains revenues from companies listed in "company_names" list
# i.e.: Company A revenue is 600000
# i.e.: Company D revenue is 1100000
company_revenues = [600000, 900000, 1000000, 1100000]
company_revenues

[600000, 900000, 1000000, 1100000]

# Let's loop over all company revenues and add them up


# We can do this using "For" loops as well

# Define an accumulator and initialize it to zero


total_revenue = 0

for i in company_revenues:
total_revenue = total_revenue + i

total_revenue

3600000

# Note that alternatively we can use the sum function to sum up all elements in the list
sum(company_revenues)

3600000

# We can also use "For" loops to iterate over characters of a Python string
message = 'Welcome to Python Programming Fundamentals Course'
for character in message:
print(character)

W
e
l
c
o
m
e

t
o

P
y
t
h
o
n

P
r
o
g
r
a
m
m
i
n
g

F
u
n
d
a
m
e
n
t
a
l
s

C
o
u
r
s
e
PRACTICE OPPORTUNITY:

• Write a python code that performs the following tasks:


▪ 1. Defines a list named "my_list" that contains the following values: 10, 5, 3

2 of 3 08/04/26, 3:36 pm
For Loops about:srcdoc

▪ 2. Multiplies all elements in the list using for loops


▪ 3. Confirm your answer by leveraging the multiplication operation defined in the math module [External Research is
Required]

PRACTICE OPPORTUNITY SOLUTION


PRACTICE OPPORTUNITY SOLUTION:

• Write a Python code that:


▪ 1. Defines a list named "my_list" that contains the following values: 10, 5, 3
▪ 2. Multiplies all elements in the list using For loops
▪ 3. Confirm your answer by leveraging the multiplication operation defined in the math module [External Research is
Required]

# Define the python list


my_list = [10, 5, 3]

# Initialize the accumulator to 1


product = 1

for i in my_list:
product = product * i

product

150

import math
[Link](my_list)

150

Excellent Job!

3 of 3 08/04/26, 3:36 pm

You might also like