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

 

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

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


📝 Exercise 4-4

Task:
Make a list of the numbers from one to one million, and then use a for loop to print the numbers . (If the output is taking too long, stop it by pressing ctrl-C or by closing the output window .

✅ Solution Code:


num = list(range(1,1000001))
for numbers in num:
    print(numbers)

🧠 Code Explanation:

This Python code demonstrates how to create a large list of numbers and iterate through it using a for loop. Here's how it works:

  1. Create a list of numbers:

    The range(1, 1000001) function generates a sequence of numbers starting from 1 and ending at 1,000,000 (inclusive). The list() function converts this range into a list, which is stored in the variable num.

  2. Iterate through the list:

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

  3. Print each number:

    The print() function is used inside the loop to display each number in the list on a new line. Since the list contains one million numbers, this will produce a very large output.

Note: Printing one million numbers may take a long time and can overwhelm the output window. If needed, you can stop the execution by pressing Ctrl+C or closing the output window.

🔍 Output:


1
2
3
4
5
6
7
8
9
10
11
It Goes on till 1000000 So Press Ctrl C to stop this.

📚 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

Previous Post Next Post

Contact Form