#๐Ÿ”’ Python for loop range confusion

118 messages ยท Page 1 of 1 (latest)

quiet ivy
#

Hey guys n-1 concept is use in for loop
right ?
but , for i in range(5,-1,-1): as -1 -1 = -2
not make sense

uncut waspBOT
#

@quiet ivy

Python help channel opened

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.

blissful pecan
#

hello

quiet ivy
#

hello

blissful pecan
#

alright, lets just get it out of the way

#

n-1 concept does not mean anything, but let me explain how it works

blissful pecan
#

first lets get some basics down, you know cpp?

blissful pecan
#

you meantioned it in the other channel

blissful pecan
#

i ask because i want to know how much you know about other languages and python

#

so i can adjust how i explain it

quiet ivy
#

ok

blissful pecan
#

so? what do y ou know about programming?

quiet ivy
blissful pecan
#

you are not very helpful now

quiet ivy
#

i am lazy rn

blissful pecan
#

please answer

quiet ivy
blissful pecan
#

so you dont know any programming?

quiet ivy
#

hmm

blissful pecan
#

that is perfectly fine

quiet ivy
#

idk definition

blissful pecan
#

no worries, ill explain it simple and do it in steps

#

do you know what a list is?

quiet ivy
blissful pecan
#

yes, so a list of five elements can look like this

quiet ivy
#

list is a collection of elements

blissful pecan
#

[0, 1, 2, 3, 4]

quiet ivy
#

yes

blissful pecan
#

and we can get elements out of a list by indexing it

quiet ivy
#

ik

blissful pecan
#

we use square brackets as well for this

quiet ivy
#

ik

blissful pecan
#

[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

quiet ivy
#

yes

blissful pecan
#

!e

mylist = [0, 1, 2, 3, 4]
print(mylist[0:3])
uncut waspBOT
#

@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.

[0, 1, 2]
blissful pecan
#

is this part clear?

quiet ivy
blissful pecan
#

the slice has three parts

#

start, end, step

quiet ivy
#

like for ?

#

i know

blissful pecan
#

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

quiet ivy
#

ok

blissful pecan
#

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])
uncut waspBOT
#

@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.

[0, 2, 4]
blissful pecan
#

since start and end has default values, we can also call it without them

quiet ivy
#

ok

blissful pecan
#

!e

mylist = [0, 1, 2, 3, 4]
print(mylist[::2])
uncut waspBOT
#

@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.

[0, 2, 4]
blissful pecan
#

with me so far?

quiet ivy
#

ok ok

blissful pecan
#

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])
uncut waspBOT
#

@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.

5
quiet ivy
blissful pecan
#

well, i wanted the value, is it clear why it gives out the value 5 ?

#

[0] gives the first index, [-1] gives the last

blissful pecan
#

and [1] gives the second ellement, what about the second to last element?

#

what index would that be?

quiet ivy
blissful pecan
#

correct, -2

#

so the list can be indexed forward and backword

blissful pecan
#

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])
uncut waspBOT
#

@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.

[5, 4, 3, 2, 1, 0]
blissful pecan
#

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)))
uncut waspBOT
#

@blissful pecan :white_check_mark: Your 3.12 eval job has completed with return code 0.

[5, 4, 3, 2, 1, 0]
blissful pecan
#

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

blissful pecan
#

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)
uncut waspBOT
blissful pecan
#

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)
uncut waspBOT
blissful pecan
#

though, since start defaults at 0, and step defaults at 1, you can only do range(10)

blissful pecan
#

did i clear up things now?

quiet ivy
blissful pecan
#

excellent

#

anything else you wonder about?

quiet ivy
blissful pecan
#

good, its great that you revise. once you are done, you can close this channel by typing !close

quiet ivy
#

ok

#

!close

uncut waspBOT
#
Python help channel closed

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.