#๐ can someone explain this by dummying it down for me
211 messages ยท Page 1 of 1 (latest)
@onyx bramble
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.
I don't know how much more we can dumb it down - did you follow the instructions?
can you write a script that does that first built point?
no idea how
What would you like explained? These are tasks they want you to solve yourself, using range and/or len, so that you get a decent intuitive feel for their use and uses.
If you've got a specific question, ask.
Do you know what range does then?
no
does that print it inclusive
Just for the exercise, write a loop to print several numbers.
Maybe you should run it.
oh hey cameron
You can just copy/paste these text sections into the python interpreter.
whats a python interpreter
The REPL, the >>> prompt.
range(N), when used in a loop, gives the numbers from 0 to N -1. the number is stored inside the variable (that you usually name i) (that you don't necessarily use itself)
len returns the number of elements in a list
=> range(len(list)) gives 0 to the number of element in the list, which is exactly the indices you can use to get elements from that list
i dont follow
i use idle
that's the REPL we were talking about
whats REPL
that window
this is asking you to practice loops
how do you make a loop that goes from 1 to 100 ?
good question
i have no clue
.
Can you write a for-loop at all?
show us
idk if im supposed to put a variable to 100
Make a list: L = [1,2,3]
or like
can you write a loop to print out its members?
so am i suppposed to put 100 in a list
no
nono not yet
close but no
Run it - what does it do?
im lost
prints the list 3 times
for some weird reason
So: how many times did you expect the loop to run the print statement?
no
i guess 100 times
How many items in the list itself?
not in your code
Try printing len(L).
for x in [1, 2, 3]:
print(...)
fill in the ..., what do you want to print to have 1, 2 and 3 on the screen ?
If L=[1,2,3], then each of 1, 2 and 3 is an item from the list.
do i do
imagine a list as a street, each house in that street is an entry/element to said list and every house in that street has an address, said address is what we call an index
print([0,1,2])?
Your list is [0,100,1] What are the items? Also called elements or list members - these are just description English terms, not formal things.
good analogy
ill try to remember that
if i can remember correctly, [0 = start, 100 is end, 1 is how much i want it to increase by
so i guess 0 - 99
which i shouldve changed it to 0,101
Ok. You're confsing a list with a range.
A list is like the street: it has houses, each house containing a value. The street address is the index, the contents of the house are the value/item.
A range indicates a range of values in an arithmetic progression. range(0,100,1) represents the values 0, 1, 2, ... up to 99.
101 is indeed what you'd pick as the upper bound to include 100.
range(N) is a street with N houses each of them having as adress a number from 0 to N-1, and each element being equal to the adress
i see
so in this case i'm supposed to use range?
print(range(100))?
if you do that, you print only one object, the range
and you want to print all the elements inside
it's like if I asked a picture of each house and you send me a picture of the whole street
(from a distance, not showing each house's contents)
i see
i love how every1 adding onto this house/street list analogy
its helping alot
range needs to be called with ()
range itself isn't really a thing
like we talked about previously
oh
i = 100
for i in range(100):
print(i)
i guess i got it
do i necessarily need the i = 100?
:white_check_mark: Your 3.13 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
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/257PRHWLWSRNPONRGL6EQF5VA4
!e
i = 100
for i in range(100):
print(i)
:white_check_mark: Your 3.13 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
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/E5PNQQ4G4XVIZUVPVMBSDDOB7M
as u can see
you can always try and see :)
same result
i see
man i feel dumb for not knowing something so simple
i guess range index list they confuse me
we all start somewhere
Yah. A list is just a container for storing values.
You can get at a particular value by its index. Like, the third value has index 2 (since we count from 0).
A range() is for specifying a range of values equally spaced. So range(10) specifies 0,1,2,3,4,5,5,6,7,8,9. What you use those values for is up to you. Maybe they're just numbers to print. May you will use them to index a list. And so on. They're not inherently list indices or anything - they're just numbers.
... and the point of these exercises up the top it to use these things in a few ways. Once you've done that, the confusion should be less.
what are indices
The plural of index. One index, several indices.
(a) write a for loop which iterates 100 times and (b) print your name once as the body of the loop
ig another way to explain lists is a bucket list if u know what that is 
So how would you write a loop to do anything 100 times?
Uh
for i in range(100):
Ok. Now, how does that work?
Ok. Now, how does that work?
oh hmm
name = "John"
for name in range(101):
print(name)
this just printed the 1 - 100 again
it printed 1 - 100
That's because you used name as the loop variable.
So each value from the range was assigned to name, overwriting your name="John"
i see
But I was actually asking: how does for i in range(100) run 100 times? What causes it to run 100 times?
But why?
i gets each number because it is the variable name used in the for-loop.
But where do the numbers come from?
from range(100)
Right, then range.
So a range is a range object. How does that turn into the numbers given to i?
isnt that 0-99
Yes.
That's what it represents.
How does the for-loop know what values to put in i?
what do u mean
name = "John"
if True:
print("John"* 100)
i know this is really shitty code
but this is one way to print it 100 times i guess
!e
I mean this:
for obj in (1,2,3), [4,5,6], "abc", range(3):
print(obj, ', for i in obj ->', end='')
for i in obj:
print(" ", i, end='')
print()
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | (1, 2, 3)
002 | 1
003 | 2
004 | 3
005 | [4, 5, 6]
006 | 4
007 | 5
008 | 6
009 | abc
010 | a
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/VM7EWNNN5RXGIGEWADM5O3T3MA
what about this
You should follow the link to see the full output. https://paste.pythondiscord.com/VM7EWNNN5RXGIGEWADM5O3T3MA
saw it
what about this
!e
Let me make it more compact:
for obj in (1,2,3), [4,5,6], "abc", range(3):
print(obj, ', for i in obj ->', end='')
for i in obj:
print(" ", i, end='')
print()
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | (1, 2, 3) , for i in obj -> 1 2 3
002 | [4, 5, 6] , for i in obj -> 4 5 6
003 | abc , for i in obj -> a b c
004 | range(0, 3) , for i in obj -> 0 1 2
Here we see the values given to i in each of these for-loops.
Where do those values come from?
from the variable obj?
Each of these objects is "iterable" - you can iterate over the object and it gets you values.
So the for-loop goes iter(obj) to get an "iterator" which is a object which yields values.
Then the for-loop iterates over the iterator by calling next() on it, which yields each value in turn, once every time it is called.
right
i get the concept of for loop
Technically, in python, "iterable" means an object you can call iter() on. But the effect is as you describe.
Right.
So when you go: for i in range(100), the call range(100) gets you a range object. The for-loop calls iter() on that object, getting an iterator. That iterator yields the values 0, 1,2,3 up to 99.
And because those are the values it yields, the for-loop runs that many times.
right
The point here is that the for-loop knows nothing specific about what object you give it - a string, a range object, a list, whatever.
It just asks to iterate it, and uses whatever the object produces.
but can u iterate it over a string?
Yes, it gets you the letters.
so can u print a name 100 times
using the for loop
cuz im thinking the code is actually simple
you just * 100 the string
Yes. You give it an object which yields 100 values (doesn't matter what they are) and have the loop body print the name once.
What, this?
for i in range(100):
print(name)
Oh. That does something a bit different. Try running it.
It computes a new string with the text John in it repeated 100 times.
The output's different.
You write the code to produce the result you want.
If you wanted John printed 100 times, each on its own line, the simple way is the:
for i in range(100:
print(name)
way (or literally put "John" in there instead of name).
oh that works too?
What you could do is say:
- to print
Johnon a line you write the lettersJ o h nand then a newline character (to go to the next line) - make a string containing that, 100 times:
john_lines = "John\n" * 100 - print that single string:
print(john_lines)
i feel like what u just did earlier is so much easier
print() whatever expressions you give it.
which one is called "efficient" coding
The for-loop with a single print() call inside it? Yes, which is why we usually wite it that way.
Porbably the for-loop with the single print().
i see. i'll stick with that
It is more important for code to be clear and understandable than to be as "efficient" as possible.
Particularl since "efficientcy" often depends on internals of the language. There may be times when makes one big string is "faster". But I'd imagine they're rare, and it depends on how expensive "making a big string" is. Which we don't knwo without measuring it.
Better to write something clear, and go after performance it that turns out to be important later.
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.