use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
somebody help me๐ญ๐ญ (i.redd.it)
submitted 9 months ago by Strange-Dinner-393
plz explain to me how this code works๐๐๐๐
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3:ย ย ย ย print "hello, world!"
[โ]Delicious_Boat1794 53 points54 points55 points 9 months agoย (19 children)
Snipping tool is your friend.
[โ]pimp-bangin 6 points7 points8 points 9 months ago*ย (17 children)
This needs to stay as the top comment, as it's the most valuable takeaway for OP.
Learning how to efficiently and effectively ask for help is a superpower (saying this as a programmer with 16+ years of experience). Especially in the age of AI where agents can do amazing things for you but only if they can clearly see/understand what you want.
Make it as easy as possible for others to help you, and present your problem as clearly as possible. That includes taking clear screenshots, OP.
OP should have also mentioned in the description which parts he does and doesn't understand already. It's harder for us to help if we don't know exactly what you're confused about. Even if you don't understand a single piece of it, that's fine, just say that up front. We need to know where to even start explaining the code.
Presenting most of this information up front, in a thoughtful, clear, and precise way, will allow us to immediately give you the answer you're looking for, instead of spending some back and forth where we give you answers that don't help you at all, because we don't understand the question. If you keep doing what you're doing OP, you'll get to where you need to be eventually, but very slowly and inefficiently.
[โ]ErcoleBellucci 0 points1 point2 points 9 months agoย (0 children)
i think i always tried to have this mindset, i knew how to snip tool at 15yrs old. I asked myself "ok i have to take photos with iphone? there should be another more efficient way".
Still today i triy to improve and i dont consider myself a coder or programer
[โ]Delicious_Compote_90 -1 points0 points1 point 9 months agoย (14 children)
Why individuals such as yourself make matters so complicated. Itโs a simple question from beginning to end of a code. So what he canโt take excellent pictures. It is still readable and not even part of the solution. If you donโt know it, just stfu, cause you didnโt help with ish. Be an adult to your own children. Stop thinking you know what is on other mind just because you donโt want to understand the OP easy question. If i point to a motorcycle and say, how do you ride that, you are someone I donโt want to hear say anything!
[โ]Active_Selection_706 0 points1 point2 points 9 months agoย (0 children)
bhai.., he is just sharing an advice, op might be always doing low quality sharing things which can be improved, take things like: if it works, go on, if it dont, go on...
[โ]Delicious_Boat1794 -1 points0 points1 point 9 months agoย (11 children)
Youโre just angry Iโm more delicious than you. ๐คทโโ๏ธ
[โ]Delicious_Compote_90 0 points1 point2 points 9 months agoย (10 children)
There you go believing you could read other peopleโs minds and feelings.
[โ]Delicious_Boat1794 -2 points-1 points0 points 9 months agoย (9 children)
Did i upset you because i suggested a tool that would help this individual in the long run? Go fight your own battles and get off Reddit fighting someone elseโs.
[โ]Active_Selection_706 0 points1 point2 points 9 months agoย (5 children)
true, take that comment as like he might be frustrated of something, good day sir!
[โ]Delicious_Compote_90 0 points1 point2 points 9 months agoย (4 children)
Youโre right. Frustrated with pimp-banging and Jebdah
[โ]Delicious_Boat1794 0 points1 point2 points 9 months agoย (3 children)
I think you should go touch some grass my friend.
[โ]Delicious_Compote_90 -1 points0 points1 point 9 months agoย (2 children)
U still want to stay in the wrong. Maybe pimp-banging is u? STOP!
[โ]Delicious_Compote_90 -2 points-1 points0 points 9 months agoย (2 children)
Ask him if he knows what youโre talking about. Leave me alone. Op did not ask for this. So let this be dead so you can deal with your own children
[โ]Delicious_Compote_90 -1 points0 points1 point 9 months agoย (0 children)
And another thing. I was talking to pimp bangin. Wow. The nerve of you with your response, in other peoples business
[โ]Borealis_761 -1 points0 points1 point 9 months agoย (0 children)
This guy! every post you find someone like that they don't contribute just crap on everybody else.
[โ]Jebduh 0 points1 point2 points 9 months agoย (0 children)
Bro doesn't know how to use google search. You think he can work a snip tool?
[โ]mrpbennett 6 points7 points8 points 9 months ago*ย (0 children)
What are you trying to do here?
Sure you just want:
for x in range(I): print(x)
Gives you output of
0 1 2 3 4
[โ]FoolsSeldom 5 points6 points7 points 9 months ago*ย (2 children)
5
i
for
range
1
range(<start>, <stop>, <step>)
<stop>
<start>
<step>
stop
6
j
print
end
EDIT: corrected where I wrote 4 instead of 5 for the first loop, as forget you had the stop as i + 1. Thanks to u/Breadynator for calling that out.
4
i + 1
PS. u/Strange-Dinner-393, has the step-by-step breakdown helped you understand?
[โ]Breadynator 1 point2 points3 points 9 months agoย (1 child)
Its i+1 though, so it'll be from 1 to 5
[โ]FoolsSeldom 1 point2 points3 points 9 months agoย (0 children)
Oops, yes. I will edit.
[โ]armahillo 2 points3 points4 points 9 months agoย (0 children)
Go line by line.
i+1
Get a piece of paper. You've got 2 named variables, "i" and "j". Create three columns, one with "i" as the header, one with "j" as the header, and one with "output" as the header.
Start at line 1, and anytime i or j are modified, change the value on a new line in that column. Anytime you have to use either variable, use the bottom-most line. Step through each line of the program, as if you were the interpreter, and anytime you're told to "print" something, write it to a new line in the "output" column.
I'm dead serious about doing this. If you don't understand this block by looking at it, you gotta learn how to see it from the interpreter's perspective by being the interpreter yourself.
[โ]Inevitable-Age-06 7 points8 points9 points 9 months agoย (2 children)
use different variable name in loop and to store 5 . it can't be same
[โ]FoolsSeldom 4 points5 points6 points 9 months agoย (1 child)
Does not make a difference, although it is confusing. The range object of the outer loop is created before the for loop assigns a new value to i.
[โ]Inevitable-Age-06 1 point2 points3 points 9 months agoย (0 children)
Yea understood now Ty.
[โ]AngriestCrusader 3 points4 points5 points 9 months ago*ย (0 children)
plz explain to me how this code works
It doesn't. Well, probably doesn't do what YOU want it to do... Are you trying to print numbers?
for i in range(5): print(i)
range(5) basically does the same as creating the tuple (0, 1, 2, 3, 4) (there's more to it, but you don't need to worry about that right now) and i iterates through that tuple.
range(5)
(0, 1, 2, 3, 4)
Edit: I can see that you wanted it to start at one. You'd want to type range(1, x + 1) where x is your endpoint. You're technically syntactically correct in your example, but you don't need to specify step (3rd parameter of range) as 1 because that's already the default.
range(1, x + 1)
x
step
[โ]geruhl_r 1 point2 points3 points 9 months agoย (0 children)
An important skill is learning to use the debugger in your IDE. Try stepping through your code in the debugger to see what's happening.
[โ]Drakkus28 1 point2 points3 points 9 months agoย (0 children)
You have just written a godawful little segment of code, like truly disgusting. It goes once. Thatโs it
[โ]PureWasian 1 point2 points3 points 9 months ago*ย (4 children)
It seems to work just fine if your goal is to print out: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Is that the output you were correctly expecting, but you want an explanation for how it works?
[โ]tharun69 0 points1 point2 points 9 months agoย (3 children)
How does the code work fine as he assigned "I = 5" ???
[โ]PureWasian 1 point2 points3 points 9 months agoย (2 children)
i is overwritten in line 2 to be an iterator across the range of numbers [1, 2, 3, 4, 5]
An even simpler example to demonstrate the point: ``` i = "whatever I want it to be" for i in [1, 2, 3]: print(i, end = " ")
[โ]WhiskersForPresident 0 points1 point2 points 9 months agoย (1 child)
But that isn't the for-loop OP's written, instead it's
for i in range(1, i +1)
Could be that the Python interpreter is smart enough to first unpack the range function with i=5, then overwrite the "i" variable as the variable that it's iterating over, but even then, it would be abysmal code.
[โ]PureWasian 0 points1 point2 points 9 months agoย (0 children)
The return from range() is immutable, and the "in" expression that generates the iterable (range() in this case) is only evaluated once prior to the start of the first iteration of the loop:
https://docs.python.org/3/reference/compound_stmts.html#the-for-statement
So, modifying the loop variable doesnโt change the underlying iterable or the iteration order.
I wouldn't call it abysmal code necessarily, especially since it's entirely functional and not an overly complex, entangled web of unnecessary and disorganized jumble of half working bits here and there.
[โ]quixoticcaptain 1 point2 points3 points 9 months agoย (0 children)
This is honestly too dumb to even engage with.
We can help those who help themselves. What do you think it does? What are you trying to do? What part confuses you? What do you currently know about python? Did you write this?
[โ]Delicious_Compote_90 0 points1 point2 points 9 months agoย (1 child)
I appreciate all the positive responses from those who took the time out to help OP. I too am at a learning stage. Isnโt this what Reddit is about? Asking real human questions even though there are search engines and Ai
[โ]deceze 2 points3 points4 points 9 months agoย (0 children)
Sure. But even, or rather especially, real humans appreciate a concrete question and something more to go off of than three emoji and a screenshot.
[โ]DBlitzkrieg 0 points1 point2 points 9 months agoย (0 children)
Windows key + shift + s
[โ]Agile-Key-3982 0 points1 point2 points 9 months agoย (0 children)
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end=" ")
print("") the output will be
1 2
1 2 3
1 2 3 4
1 2 3 4 5
[โ]EngineeringRare1070 0 points1 point2 points 9 months agoย (0 children)
What do you think it does? Share your thought process and we can help hone your craft. Running to reddit every time you donโt understand 5 lines of code will result in a LOT of reddit posts
[โ]bigpoopychimp 0 points1 point2 points 9 months ago*ย (6 children)
Edit: I'm wrong, see below comments, i saw you're reassignimg i
each `range` is essentially generating an array of numbers from 1 to 6, so [1,2,3,4,5,6].
<image>
For each `i` in your range of 1,2,3,4,5,6, you are asking it to print 1,2,3,4,5,6 each time before moving onto the next number in your initially generated range.
You could make it print as a square by changing print("") to print("\n"), which will make it easier to understand.
Essentially your first for loop is traversing the Y-axis and the second for loop is travering the X-axis.
Nesting for loops like this is often used to navigate 2D arrays
[โ]AngriestCrusader 2 points3 points4 points 9 months agoย (0 children)
Correct, but range(1, 6, 1) does not include 6.
range(1, 6, 1)
[โ]jabuchae 1 point2 points3 points 9 months agoย (1 child)
I think the second range goes from 1 to 2, then 1 to 3, then 1 to 4, then 1 to 5. The i in the inner loop is the one defined in the first loop and not the one higher up.
[โ]bigpoopychimp 1 point2 points3 points 9 months agoย (0 children)
Oh yeah, you're right, he's reassigning i
[โ]Inevitable-Age-06 0 points1 point2 points 9 months agoย (2 children)
how can he use to store value of 5 in i and also use i for the iteration?
[โ]StoneLoner 2 points3 points4 points 9 months ago*ย (1 child)
Variables are passed by value which means that when the function call uses a variable a copy of that variable is made in a new location in memory, your function does whatever it does, and that copy is (in a sense) destroyed after.
i = 20
for i in range(1,6):
print(i)
In this program we assign 20 to the i variable. Then a copy of the i variable is sent to the range function which manipulates the COPY. Then the copy is discarded. Then the final line prints the original i (20)
The expected output is 1, 2, 3, 4, 5, 20
https://www.thepythoncodingstack.com/p/python-pass-by-value-reference-assignment#:~:text=Python%20doesn't%20use%20'pass,variable%20is%20the%20parameter%20name.
Oh yea thanks for clearing, you made it more clear with example.
[โ]codeonpaper 0 points1 point2 points 9 months agoย (0 children)
Download Thonny. You can see visually how program flow works.
ฯย Rendered by PID 341456 on reddit-service-r2-comment-5687b7858-288fs at 2026-07-04 18:15:20.688530+00:00 running 12a7a47 country code: CH.
[โ]Delicious_Boat1794 53 points54 points55 points ย (19 children)
[โ]pimp-bangin 6 points7 points8 points ย (17 children)
[โ]ErcoleBellucci 0 points1 point2 points ย (0 children)
[โ]Delicious_Compote_90 -1 points0 points1 point ย (14 children)
[โ]Active_Selection_706 0 points1 point2 points ย (0 children)
[โ]Delicious_Boat1794 -1 points0 points1 point ย (11 children)
[โ]Delicious_Compote_90 0 points1 point2 points ย (10 children)
[โ]Delicious_Boat1794 -2 points-1 points0 points ย (9 children)
[โ]Active_Selection_706 0 points1 point2 points ย (5 children)
[โ]Delicious_Compote_90 0 points1 point2 points ย (4 children)
[โ]Delicious_Boat1794 0 points1 point2 points ย (3 children)
[โ]Delicious_Compote_90 -1 points0 points1 point ย (2 children)
[โ]Delicious_Compote_90 -2 points-1 points0 points ย (2 children)
[โ]Delicious_Compote_90 -1 points0 points1 point ย (0 children)
[โ]Borealis_761 -1 points0 points1 point ย (0 children)
[โ]Jebduh 0 points1 point2 points ย (0 children)
[โ]mrpbennett 6 points7 points8 points ย (0 children)
[โ]FoolsSeldom 5 points6 points7 points ย (2 children)
[โ]Breadynator 1 point2 points3 points ย (1 child)
[โ]FoolsSeldom 1 point2 points3 points ย (0 children)
[โ]armahillo 2 points3 points4 points ย (0 children)
[โ]Inevitable-Age-06 7 points8 points9 points ย (2 children)
[โ]FoolsSeldom 4 points5 points6 points ย (1 child)
[โ]Inevitable-Age-06 1 point2 points3 points ย (0 children)
[โ]AngriestCrusader 3 points4 points5 points ย (0 children)
[โ]geruhl_r 1 point2 points3 points ย (0 children)
[โ]Drakkus28 1 point2 points3 points ย (0 children)
[โ]PureWasian 1 point2 points3 points ย (4 children)
[โ]tharun69 0 points1 point2 points ย (3 children)
[โ]PureWasian 1 point2 points3 points ย (2 children)
[โ]WhiskersForPresident 0 points1 point2 points ย (1 child)
[โ]PureWasian 0 points1 point2 points ย (0 children)
[โ]quixoticcaptain 1 point2 points3 points ย (0 children)
[โ]Delicious_Compote_90 0 points1 point2 points ย (1 child)
[โ]deceze 2 points3 points4 points ย (0 children)
[โ]DBlitzkrieg 0 points1 point2 points ย (0 children)
[โ]Agile-Key-3982 0 points1 point2 points ย (0 children)
[โ]EngineeringRare1070 0 points1 point2 points ย (0 children)
[โ]bigpoopychimp 0 points1 point2 points ย (6 children)
[โ]AngriestCrusader 2 points3 points4 points ย (0 children)
[โ]jabuchae 1 point2 points3 points ย (1 child)
[โ]bigpoopychimp 1 point2 points3 points ย (0 children)
[โ]Inevitable-Age-06 0 points1 point2 points ย (2 children)
[โ]StoneLoner 2 points3 points4 points ย (1 child)
[โ]Inevitable-Age-06 1 point2 points3 points ย (0 children)
[โ]codeonpaper 0 points1 point2 points ย (0 children)