Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-04-2013, 05:04 PM   #1
Baseband Member
 
palermo's Avatar
 
Join Date: Jan 2013
Location: United States
Posts: 81
Default Python Question?

Why does this start counting with 21? I understand that it's counting every fourth digit backward but I'm unsure why it starts with -21

x = list(range(22))
for i in x[::-4]:
print i,
__________________

palermo is offline   Reply With Quote
Old 05-05-2013, 10:36 AM   #2
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Python Question?

I'm not sure why it'd start there, let alone be negative. From what I've seen on line, you should be starting at 0 and be able to go up to 21. You should never see a 22 unless you seeded the range function with a -22 to start with.
__________________

__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Old 05-05-2013, 11:48 AM   #3
Baseband Member
 
palermo's Avatar
 
Join Date: Jan 2013
Location: United States
Posts: 81
Default Re: Python Question?

Thank you. I'll tinker with it a little more to see if I can figure it out. This one prints the same result:

i = 0
x = list(range(0,22))
x.reverse()
while i <= 21:
print x[i]
i += 4

both print: '21 17 13 9 5 1' in that order but i'm still confused why the first one starts with 21. If its range is '0-21' and I start counting backwards by four why is it starting with 21? I guess I'll be digging through 'Think Python' all day today.
palermo is offline   Reply With Quote
Old 05-05-2013, 02:05 PM   #4
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Python Question?

So when you reverse the list, it the list will then be 21, 20, 19..., 0 Since that loop starts at index 0 that's why the second code set produces those results.

Why the first code set you gave is doing the same thing, that I'm not sure..
__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 12:21 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0