#đź”’ i have no idea what for and while loops do
46 messages · Page 1 of 1 (latest)
@edgy bronze
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.
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)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | 0
002 | 1
003 | 2
004 | 3
005 | 4
!e ```py
for character in "hello":
print(character)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | h
002 | e
003 | l
004 | l
005 | o
!e ```py
n = 0
while n < 5:
print(n)
n += 1
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | 0
002 | 1
003 | 2
004 | 3
005 | 4
so does the thing in between for and in matter
i though it was just a variable
for is more useful if you know “how many times you want” to perform an action, while while is about uncertainty, but not always
we can iterate over "hello" so I get each character
so is this saying until n=5 print n and add one?
!e ```py
for i in range(5):
for j in range(i):
print("*", end="")
kinda
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | *
002 | **
003 | ***
004 | ****
005 | *****
so will it do the same thing if you replace character with something else
?
you can name the variable whatever you want
ok so it just reads it as a string and knows what to do immediately?
I don't really understand what that means.
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")
:warning: Your 3.14 eval job has completed with return code 0.
[No output]
!e ```py
iter(0)
:x: Your 3.14 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m1[0m, in [35m<module>[0m
003 | [31miter[0m[1;31m(0)[0m
004 | [31m~~~~[0m[1;31m^^^[0m
005 | [1;35mTypeError[0m: [35m'int' object is not iterable[0m
!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
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | 1
002 | 2
003 | 3
004 | 100
005 | 200
006 | 300
No, it doesn't matter. That is a name, as if you had done character = . Whatever name you put after for is assigned each value in the iterable.
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.
like it sees that it’s a string and since it’s a for loop it knows to iterate so it iterates the characters?
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
yeah, since str is an object it has __iter__ method which has predifined behaviour
iter function calls this method depending on the type of the object
iter("hello") kinda converts to "hello".__iter__() , so for also does that under the hood
!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")
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | a
002 | b
003 | c
004 | ['a', 'b', 'c']
005 | ('a', 'b', 'c')
as you can see, it's used not only for loops
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.