Exercise 4-3 Solution Python Crash Course Chapter 4: Working With Lists

 

πŸ“˜ Python Crash Course – Chapter 4: Working With Lists – Exercise 4-3 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


πŸ“ Exercise 4-3

Task:
Use a for loop to print the numbers from 1 to 20, inclusive

✅ Solution Code:


for no in range(1,21):
    print(no)

🧠 Code Explanation:

This Python code demonstrates how to use a for loop to print a range of numbers. Here's how it works:

  1. Use the range() function:

    The range(1, 21) function generates a sequence of numbers starting from 1 and ending at 20 (inclusive). The first argument specifies the starting number, and the second argument specifies the number up to which the range goes, but it excludes the second argument itself. Therefore, the range includes numbers from 1 to 20.

  2. Iterate through the range:

    A for loop is used to iterate through each number in the range. The variable no takes on each value in the range, one at a time.

  3. Print each number:

    The print() function is used inside the loop to display each number in the range on a new line.

πŸ” Output:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

πŸ“š 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 #introducingLists

“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