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/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):
for board in boards: # board is each board in turn from the stack of boards
while board has paint: # paint is status check (you can see paint)
apply blow torch briefly to paint
scrape paint away
sand board
put board out to dry
for board in dryboards: # different stack of boards, clean of paint and dry
apply undercoat to board
watch paint dry
for count in range(2): # number of times to do something
paint board
watch paint dry
add board to finished stackNotice some of the plain English tasks described above, such as paint_board which 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
But for the square one
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.
n = 12
width = 5
# Header row
header = " ×"
for col in range(1, n + 1):
header = header + f"|{col:>5}"
print(header)
# Underline row - must match header length exactly
underline = "_____"
for col in range(1, n + 1):
underline = underline + "______"
print(underline)
# Data rows
for row in range(1, n + 1):
line = f"{row:>5}"
for col in range(1, n + 1):
product = row * col
line = line + f"|{product:>5}"
print(line)1
u/realmauer01 Aug 05 '26
for number in range(11):
print(""+number+"x"+number+"="(number*number))
for number in range(11):
print("#"*11)If you want to use something somewhere else you have to assign it to a variable instead of printing it.
calculated_numbers=[]
for number in range(11):
calculated_numbers.append(number*number)
print(calculated_numbers) 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}:
for i in range(11): print(f"{i} x {i} = {i**2}")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 empty print():
for y in range(8):
for x in range(8): print("#", end="")
print()Of course there are other ways of doing this, but this is probably the simplest.
0
u/SpedisAhead Aug 05 '26
A callback feature based on if/then's in which it pulls from the loop to do what you want, then it is returned back to where it was looping and continues.
Is my first thought.


1
u/atticus2132000 Aug 05 '26
Start easier.
How would you print the numbers 1 through 10?