#๐ Python for loop range confusion
118 messages ยท Page 1 of 1 (latest)
@quiet ivy
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
hello
hello
alright, lets just get it out of the way
n-1 concept does not mean anything, but let me explain how it works
first lets get some basics down, you know cpp?
you meantioned it in the other channel
yes
i ask because i want to know how much you know about other languages and python
so i can adjust how i explain it
ok
so? what do y ou know about programming?
everything
you are not very helpful now
please answer
ok , programming is way to communicate to computer
so you dont know any programming?
hmm
that is perfectly fine
idk definition
list[]
yes, so a list of five elements can look like this
list is a collection of elements
[0, 1, 2, 3, 4]
yes
and we can get elements out of a list by indexing it
ik
we use square brackets as well for this
ik
[0, 1, 2, 3, 4][3] gives us 3
we can also slice the list into parts
we can get [0, 1, 2] from the list in this way
it looks like this
yes
!e
mylist = [0, 1, 2, 3, 4]
print(mylist[0:3])
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
[0, 1, 2]
is this part clear?
yes
start,. end and step all have default values
start will by default start at the beginning
end will by default be the end or last element
and the step is by default 1
ok
if we want to get every even element from a list, we can use [0, 5, 2] as our slice
!e
mylist = [0, 1, 2, 3, 4]
print(mylist[0:5:2])
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
[0, 2, 4]
since start and end has default values, we can also call it without them
ok
!e
mylist = [0, 1, 2, 3, 4]
print(mylist[::2])
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
[0, 2, 4]
with me so far?
ok ok
alright
lets look how we can do this in reverse
lets inspect what negative indexes look like
mylist[-1] what is this value?
any idea?
!e
mylist = [0, 1, 2, 3, 4, 5]
print(mylist[-1])
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
5
len-1
well, i wanted the value, is it clear why it gives out the value 5 ?
[0] gives the first index, [-1] gives the last
yes crystal clear
and [1] gives the second ellement, what about the second to last element?
what index would that be?
yes negative indexing i remember
len-2
-2
that means that you can use the step in a slice to be negative
!e
mylist = [0, 1, 2, 3, 4, 5]
print(mylist[::-1])
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
[5, 4, 3, 2, 1, 0]
0,len,-1
ok
so this new list goes from five to zero
if we want to write that using range, we have to keep the order in mind
range(5, -1, -1) in other words
!e
print(list(range(5, -1, -1)))
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
[5, 4, 3, 2, 1, 0]
i used list here to see the same result as we have been talking about
you normally would never do that when your using range
range just like a slice has start, stop and step
the rules are the same, so if you want to go backwards, you have to start at five, end up negative one with the step of negative one
and since range is a sequence, you can iterate over it
!e
my_range = range(10, -1, -1)
for num in my_range:
print(num)
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 10
002 | 9
003 | 8
004 | 7
005 | 6
006 | 5
007 | 4
008 | 3
009 | 2
010 | 1
011 | 0
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/ZD7VYHCE6BT36H2QAGG22OOWCU
if you want the range to go the other, normal way
!e
my_range = range(0, 11, 1)
for num in my_range:
print(num)
@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0
002 | 1
003 | 2
004 | 3
005 | 4
006 | 5
007 | 6
008 | 7
009 | 8
010 | 9
011 | 10
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/EDI6L4HC7CIVM25ZNE7FRHNR4A
though, since start defaults at 0, and step defaults at 1, you can only do range(10)
ok
ok
did i clear up things now?
yes
no , i revise everything
good, its great that you revise. once you are done, you can close this channel by typing !close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.