#๐Ÿ”’ can someone explain this by dummying it down for me

211 messages ยท Page 1 of 1 (latest)

onyx bramble
lost abyssBOT
#

@onyx bramble

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.

boreal imp
# onyx bramble

I don't know how much more we can dumb it down - did you follow the instructions?

fervent cove
onyx bramble
native dust
#

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.

boreal imp
onyx bramble
#
print(range(100))
#

?

wintry moon
#

no

onyx bramble
#

does that print it inclusive

native dust
#

Just for the exercise, write a loop to print several numbers.

#

Maybe you should run it.

onyx bramble
native dust
#

You can just copy/paste these text sections into the python interpreter.

onyx bramble
native dust
#

The REPL, the >>> prompt.

wintry moon
#

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

onyx bramble
wintry moon
#

take a screenshot

#

of whatever you use to code

onyx bramble
#

i use idle

wintry moon
#

the bottom right shell is the repl

#

the big left part is a text editor

onyx bramble
#

so u want me to print it here like this?

wintry moon
onyx bramble
wintry moon
#

that window

wintry moon
#

how do you make a loop that goes from 1 to 100 ?

onyx bramble
#

i have no clue

native dust
#

Can you write a for-loop at all?

onyx bramble
#

i can

#

but like

wintry moon
#

show us

onyx bramble
#

idk if im supposed to put a variable to 100

native dust
#

Make a list: L = [1,2,3]

onyx bramble
#

or like

native dust
#

can you write a loop to print out its members?

onyx bramble
#

so am i suppposed to put 100 in a list

wintry moon
native dust
#

nono not yet

onyx bramble
#

oh

#

wait

#

i might be able to

#
L = [0, 100, 1]
for list in L:
  print(L)
#

?

wintry moon
#

close but no

native dust
#

Run it - what does it do?

onyx bramble
#

im lost

wintry moon
#

let's break it down

#

what is list ?

onyx bramble
#

for some weird reason

onyx bramble
#

i think

native dust
#

So: how many times did you expect the loop to run the print statement?

wintry moon
native dust
#

How many items in the list itself?

wintry moon
#

not in your code

native dust
#

Try printing len(L).

onyx bramble
#

3?

wintry moon
#

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 ?

native dust
#

If L=[1,2,3], then each of 1, 2 and 3 is an item from the list.

dense snow
onyx bramble
#

print([0,1,2])?

native dust
#

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.

onyx bramble
#

ill try to remember that

onyx bramble
#

so i guess 0 - 99

#

which i shouldve changed it to 0,101

native dust
#

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.

native dust
wintry moon
#

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

onyx bramble
#

print(range(100))?

wintry moon
#

if you do that, you print only one object, the range

#

and you want to print all the elements inside

onyx bramble
#

i see

#

so am i supposed to do like

wintry moon
#

it's like if I asked a picture of each house and you send me a picture of the whole street

native dust
#

(from a distance, not showing each house's contents)

onyx bramble
#
i = 100
for i in range:
  print(i)
#

?

dense snow
#

i love how every1 adding onto this house/street list analogy

onyx bramble
wintry moon
#

range needs to be called with ()

#

range itself isn't really a thing

#

like we talked about previously

onyx bramble
#

oh

#
i = 100

for i in range(100):
  print(i)
#

i guess i got it

#

do i necessarily need the i = 100?

dense snow
#

u dont need it at all no pithink

#

!e

for i in range(100):
  print(i)
lost abyssBOT
dense snow
#

!e

i = 100
for i in range(100):
  print(i)
lost abyssBOT
dense snow
#

as u can see

jolly bloom
dense snow
#

same result

onyx bramble
#

i see

#

man i feel dumb for not knowing something so simple

#

i guess range index list they confuse me

dense snow
native dust
# onyx bramble i guess range index list they confuse me

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.

native dust
#

The plural of index. One index, several indices.

onyx bramble
#

i see

#

now how would i print my name

#

100 times

native dust
#

(a) write a for loop which iterates 100 times and (b) print your name once as the body of the loop

dense snow
native dust
onyx bramble
#

for i in range(100):

native dust
#

Ok. Now, how does that work?

native dust
onyx bramble
onyx bramble
#

this just printed the 1 - 100 again

#

it printed 1 - 100

native dust
#

That's because you used name as the loop variable.
So each value from the range was assigned to name, overwriting your name="John"

onyx bramble
#

i see

native dust
#

But I was actually asking: how does for i in range(100) run 100 times? What causes it to run 100 times?

onyx bramble
#

i is running through 100

#

every number

native dust
#

But why?

onyx bramble
#

because its in the for loop

#

and i put a range of 100

native dust
#

i gets each number because it is the variable name used in the for-loop.

But where do the numbers come from?

onyx bramble
#

from range(100)

native dust
#

Right, then range.
So a range is a range object. How does that turn into the numbers given to i?

onyx bramble
#

isnt that 0-99

native dust
#

Yes.

#

That's what it represents.

#

How does the for-loop know what values to put in i?

onyx bramble
#

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

native dust
#

!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()
lost abyssBOT
native dust
native dust
# onyx bramble saw it

!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()
lost abyssBOT
native dust
#

Here we see the values given to i in each of these for-loops.

Where do those values come from?

native dust
#

Right.

#

How?

#

What's the mechanism the for-loop uses to obtain those values?

onyx bramble
#

uh

#

im not sure

#

i just know the for loop checks each variable

native dust
#

Each of these objects is "iterable" - you can iterate over the object and it gets you values.

onyx bramble
#

does iterable mean it goes over each

#

i mean goes thru each

native dust
#

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.

onyx bramble
#

i get the concept of for loop

native dust
#

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.

onyx bramble
#

right

native dust
#

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.

onyx bramble
#

but can u iterate it over a string?

native dust
#

Yes, it gets you the letters.

onyx bramble
#

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

native dust
#

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.

onyx bramble
#

but is that wrong coding

#

"inefficient"

native dust
#

What, this?

for i in range(100):
  print(name)
onyx bramble
#

no

#

the one earlier

#
name = 'John'
print(name * 100)
native dust
#

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.

onyx bramble
#

right

#

its all one wprd

#

word

#

so that not the correct way then huh

native dust
#

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).

native dust
#

What you could do is say:

  • to print John on a line you write the letters J o h n and 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)
onyx bramble
native dust
onyx bramble
#

which one is called "efficient" coding

native dust
native dust
native dust
#

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.

lost abyssBOT
#
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.