Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question about for loop
#1
How come in the output of this code there is an extra 4? Shouldn't it only display a single 4 value as the last value in this output?

num = 10
for num in range(5):
    print(num)
print(num)
Reply
#2
In the first print statement, you call all the numbers till 5 excluding 5, so it will print 1,2,3,4. Now the last number in that thing becomes num and therefore, an extra 4 is printed because of your print statement outside the loop
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
Note also, that the statement num = 10 is overwritten in step 2.
Reply
#4
This is just what I love about Python. It allows you to reuse a variable several times and reference different values. But that flexibility comes with a caveat. If not careful you might introduce errors into your code and end up wondering why you got a semantic error.
Reply
#5
I'm confused about what you don't understand; what print does or how loops work.
for num in range(5):
    print(num)
This is a loop. Loops are used when you want to execute the same code multiple times. This part controls how often the loop executes and also provides a value for num:
for num in range(5)
This is the code that is executed multiple times:
    print(num)
The loop is essentially doing this:
print(0)
print(1)
print(2)
print(3)
print(4)
print is a function that evaluates the statement inside the parenthesis and prints it to standard output (or a different destination if specified.
Reply
#6
As you can go forwards, you can also go backwards. That makes your programming flexible and convenient. Here is a code that moves through the above number line backwards. Just make the stop a negative value.
for i in range(4, -1, -1) :
    print(i)
what it prints out is:
4
3
2
1
0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A question about 'Event loop is closed' fc5igm 2 2,201 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,649 Sep-30-2021, 12:46 PM
Last Post: Underscore
  for loop question KEYS 1 1,723 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,311 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  while loop question KEYS 2 2,013 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 1,633 Sep-25-2020, 04:32 PM
Last Post: Larz60+
  while loop question spalisetty06 2 1,846 Aug-13-2020, 04:18 PM
Last Post: buran
  Question about for loop not creating an infinite loop. FWendeburg 1 2,117 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Loop Condition Question malonn 6 3,468 Aug-01-2018, 01:56 PM
Last Post: malonn
  Beginner Loop question BigDisAok 5 3,726 Jul-24-2018, 02:04 PM
Last Post: BigDisAok

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020