#๐Ÿ”’ help

40 messages ยท Page 1 of 1 (latest)

bright copper
#

What if for lopp

supple radishBOT
#

@bright copper

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.

bright copper
#

What is for loop

umbral helm
#

Have you covered the concept of assignment?

bright copper
umbral helm
#

var = ...

#

Okay, great.

#

Do you have a decent concept of what a sequence is?

#

For example, the string, 'abc' is a sequence of three characters, the strings 'a', 'b' and 'c', right?

umbral helm
#

Or the list ['apple', 'pear', 'orange']

#

That's a sequence of length 3

#

A for loop is a repeated assignment operation, assigning a variable or structure of variables to each element of a sequence.

#

I will demonstrate.

#

!e py for letter in 'abc': print(letter)

supple radishBOT
#

@umbral helm :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | a
002 | b
003 | c
umbral helm
#

This is similar to...

#
letter = 'a'
print(letter)
letter = 'b'
print(letter)
letter = 'c'
print(letter)```
#

It's taking the variable, letter, and pointing it at each element of the iterable, here the string, that I feed to the for loop.

#

!e py for fruit in ['apple', 'pear', 'orange']: print(fruit)

supple radishBOT
#

@umbral helm :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | apple
002 | pear
003 | orange
umbral helm
#
fruit = 'apple'
print(fruit)
fruit = 'pear'
print(fruit)
fruit = 'orange'
print(fruit)```
#

Some object types are iterable. Others are not.

#

!e py for number in 123: print(number)

supple radishBOT
#

@umbral helm :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     for number in 123:
004 | TypeError: 'int' object is not iterable
umbral helm
#

Integer objects are not iterable.

#

Sets, dicts, tuples, lists, strings are iterable.

#

Many objects are iterable, the only requirement is that their class implements the __iter__ special method, but you probably don't need to worry about that just now.

#

Many objects are not iterable.

#

It comes down to what type of object they are.

#

Sometimes, you'll want to iterate over a range instance.

#

!e py for i in range(3): print(i)

supple radishBOT
#

@umbral helm :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0
002 | 1
003 | 2
umbral helm
#

!e py for _ in range(3): print('Hello, world.')Sometimes you'll want to use this simply to do something a given number of times.

supple radishBOT
#

@umbral helm :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Hello, world.
002 | Hello, world.
003 | Hello, world.
umbral helm
#

@bright copper Any questions?

#

Making sense?

#

I'm leaving now. Feel free to ask any questions before your post expires and I'll check back in later.

supple radishBOT
#
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.