π Python Crash Course – Chapter 4: Working With Lists – Exercise 4-7 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 4-7
Task:
Make a list of the multiples of 3 from 3 to 30 . Use a for loop to
print the numbers in your list
✅ Solution Code:
l = list(range(3,31,3))
for no in l:
print(no)
π§ Code Explanation:
This Python code demonstrates how to generate a list of multiples of 3 and iterate through it using a for
loop. Here's how it works:
-
Create a list of multiples of 3:
The
range(3, 31, 3)
function generates a sequence of numbers starting from 3, ending before 31, and incrementing by 3. This ensures that only multiples of 3 between 3 and 30 are included. Thelist()
function converts this range into a list, which is stored in the variablel
. -
Iterate through the list:
A
for
loop is used to iterate through each number in thel
list. The variableno
takes on each value in the list, one at a time.
This code demonstrates how to use the third argument of the range()
function to generate a sequence with a specific step size and how to iterate through the resulting list.
π Output:
3
6
9
12
15
18
21
24
27
30
π Related Exercises from Chapter 4:
π Connect With Us:
“The only way to learn a new programming language is by writing programs in it.”
– Dennis Ritchie
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment