#đź”’ i have no idea what for and while loops do

46 messages · Page 1 of 1 (latest)

edgy bronze
#

sorry if this is really basic but i can’t wrap my head around what for…in…. when means and what while does

it’s like my brain gets so close and then forgets everything

chatgpt isn’t helping

wet mortarBOT
#

@edgy bronze

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.

modest yacht
#

for executes a block of code a specified number of times
while executes as long as the condition is true

#

!e ```py
for i in range(5):
print(i)

wet mortarBOT
modest yacht
#

!e ```py
for character in "hello":
print(character)

wet mortarBOT
modest yacht
#

!e ```py
n = 0
while n < 5:
print(n)

n += 1 
wet mortarBOT
edgy bronze
modest yacht
#

for is more useful if you know “how many times you want” to perform an action, while while is about uncertainty, but not always

modest yacht
edgy bronze
modest yacht
modest yacht
wet mortarBOT
edgy bronze
#

?

modest yacht
edgy bronze
modest yacht
#

Using for, we can iterate over iterable objects.
To determine whether an object is iterable, we can call the iter function. If the object is not iterable, Python will return an error.

#

!e ```py
iter("hello")

wet mortarBOT
modest yacht
#

!e ```py
iter(0)

wet mortarBOT
# modest yacht !e ```py iter(0) ```

:x: Your 3.14 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     iter(0)
004 |     ~~~~^^^
005 | TypeError: 'int' object is not iterable
modest yacht
#

!e ```py
iter([1, 2, 3]) # no error, so we can iterate over this list

for number in [1, 2, 3]:
print(number)

1

2

3

or even do something useful

for number in [1, 2, 3]:
print(number * 100)

100

200

300

wet mortarBOT
cedar flicker
delicate bison
#

for i in x does something, literally, "for" everything in x. if x is something that holds 10 things, it runs 10 times. if it holds 100 things, it runs 100 times.

the purpose of i, or whatever you name it, is to be what we call an "iterator variable." its fancy jargon to say "the name of whatever the current item in 'x' is"

lets say you want to multiply every number in a list by 2. that means you want to multiply the first thing in the list by two, the second thing, the third thing and so on.

so if you simply write i*2, on the first loop you will see the first element times two, because i is representing the first element. on the second loop, youll see the second element times two, and so on

#

heres a good way to think about it. i have a box of ingredients i want to turn into a cake. lets say i do that.

now i have two boxes to turn into two cakes. so i go to the first box, make a cake with whats inside, then i go to the second box and do the same.

the process is the same every time. lets say we had a whole room lined with these boxes. we might say something like this: "FOR any given BOX, IN the ROOM, make a cake"

hence you get the program version:

for box in room: 
    makeCake()
#

the general idea is, if you have something you can do to one thing, and you need to apply that to lots of things, you use a for loop.

edgy bronze
delicate bison
#

yes

#

certain things are "iterable" and have defined behavior for when you "iterate" over them

#

strings go through the characters

#

lists go through whatever is in the list

#

ranges go through the numbers

edgy bronze
#

ohhhh

#

i see now

thank you

modest yacht
#

!e ```py
class foo:
def iter(self):
yield "a"
yield "b"
yield "c"

for letter in foo():
print(letter)

a

b

c

print(list(foo())) # ["a", "b", "c"]
print(tuple(foo())) # ("a", "b", "c")

wet mortarBOT
modest yacht
#

as you can see, it's used not only for loops

wet mortarBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.