r/PythonLearning • u/StudentElectronic548 • Aug 05 '26
I kind of am struggling to understand loops.
I somewhat understand them on a basic level and can make like simple things using for loops now e.g a half pyramid using '#' which is commonly used as a problem for people of my skill level. However, when it comes to building something like this or the other one, I can't wrap my head around it for some reason. Since for the hashtag one here, ive been trying to brute force it and experiment but have gotten nowhere.
But for the square one, I managed to figure out the for loop that prints out all the squares but now my head is again at a wall again because im not fully sure if I can use a nested loop to print all the x * x = x^2 or if I can implement the answers from this loop into some other loop that prints out the format. (my current progress on that one is here)
for x in range(11):
print(x*x)
Any help would be great, preferably like more questions because although I don't mind like seeing a solution I want to like manage to get this on my own for the most part. fyi, ive been at these problems for about a week.


1
u/atticus2132000 Aug 05 '26
Start easier.
How would you print the numbers 1 through 10?
1
u/Longjumping-Jacket97 Aug 05 '26
First one is a loop in range(11) printing x2 Second one is either one range(25) loop or nested range(5) loop
1
u/Sea-Ad7805 Aug 05 '26 edited Aug 05 '26
What can help to better understand loops is to run a program in a debugger step by step to see the flow of control: multiplication
Nested loops (loops in loops) can be tricky in the beginning: multiplication table. Think about one for-loop at a time, decide where you do/don't want to jump to a new line, and just play around with them.
1
u/Naive_Programmer_232 Aug 05 '26 edited Aug 05 '26
Yeah you can use a similar pattern with a nested for loop. Look at how many "#" are per row and how many "#" are per column.
Think for each row, of however many rows, how many "#" would you put?
1
u/FoolsSeldom Aug 05 '26
Loops can be confusing at first, until you step back and realise you already use them in real life. Loops just repeat stuff, and if you want to repeat parts of that stuff you can also use a loop.
Imagine having a pile of old boards to repaint. In pseudocode (with some ridiculous steps to make the point):
Notice some of the plain English tasks described above, such as
paint_boardwhich in reality are complex undertakings. However, from the point of view of this high level set of instructions, I am not interested in exactly how the task is done (or even who does it), just that it gets done at the right time, in sequence. I might want to provide some guidance/requirements (perhaps the colour,paint_board("dark oak")).I might want some results back from a task, perhaps the time it took,
hours = paint_board('white paint')1
u/FoolsSeldom Aug 05 '26
You haven't said what exactly you are trying to achieve. A table of squares, perhaps?
How about if I show you a times-table? This is a simple version.
1
u/realmauer01 Aug 05 '26
If you want to use something somewhere else you have to assign it to a variable instead of printing it.
1
u/Mystic-Dragoness Aug 05 '26
Your confusion is coming more from print and strings then loops. For the first one you can use an f-string to easily insert variables in the printed output by adding an "f" before the string and surounding you variables in {brackets}:
As for the nested loop, your main issue is coming from the fact that print normally adds a new line after whatever you put in it, but you can tell it not to do that by using
print("blah blah blah", end=""). Then you can make a new line later by using an emptyprint():Of course there are other ways of doing this, but this is probably the simplest.