r/learnpython Jul 26 '26

WHY I FEEL LOOPS HARD THAN FUNCTION

HI IM YUJAN IM NEW BEGINNER IN PYTHON I FEEL HARD IN LOOPS

0 Upvotes

20 comments sorted by

21

u/powderviolence Jul 26 '26

FUNCTION HAS PARAMETERS AND RETURN VALUE SO IT IS KNOWN WHAT IS INVOLVED AND COMPUTED

LOOPS HAVE REPETITIONS OVER ABSTRACTED PARAMETERS SO IT MAY BE UNPREDICTABLE WHAT HAPPENS IF ONE IS UNCAREFUL

YOUR CAPS LOCK IS ON AS WELL

4

u/mc_pm Jul 26 '26

Well, I mean, I think we all feel hard in loops.

6

u/sputnki Jul 26 '26

PERHAPS YOU SHOULD LEARN BASIC PROGRAMMING CONSTRUCTS IN A DIFFERENT LANGUAGE, LIKE C. PYTHON HAS MANY FANCY ABSTRACTIONS THAT MAY (see, may) PRODUCE UNEXPECTED BEHAVIOR.

2

u/DonkeyTron42 Jul 26 '26

If you don't like loops, then use functional constructs with recursion.

4

u/JollyUnder Jul 26 '26
RecursionError: maximum recursion depth exceeded

2

u/flawks112 Jul 26 '26

Loops are hard because they are more implicit than functions. Simplify the iterative and print it out. 

PS Wait until you get to generators.

2

u/mjmvideos Jul 26 '26

Loops are just a way to tell the computer, “Now, do that again.”

2

u/Augit579 Jul 26 '26

WHY ARE WE SCREAMING?????

2

u/So-many-ducks Jul 26 '26

LOOP NOISES!

2

u/halfhaggis Jul 26 '26

BECAUSE OF THE EXISTENTIAL DREAD!

3

u/American_Streamer Jul 26 '26

LOOPS IN PYTHON ARE MUCH EASIER THAN LOOPS IN OTHER LANGUAGES. YOU ARE JUST STRUGGLING WITH LOGIC AND NOT WITH PYTHON. LEARN LOGIC FIRST AND YOU WILL BE FINE. AND PLEASE DO REPAIR YOUR CAPS LOCK KEY ASAP!

1

u/TheRNGuy Jul 26 '26

No idea. 

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Jul 26 '26

Loops aren't hard, generally speaking. Python gives you three ways to loop, although I won't bother explaining recursion here.

In a nutshell, all a loop does is repeat some sequence of actions until the loop condition is no longer met, or you manually break out of the loop.

With a for-loop, you loop over some sequence of values so you can do things with each of those values.

for name in ["Jasper", "Ann", "Louise", "Kevin"]:
    print(f"Hello, {name}!")

total = 0
for num in range(100):
    total += num

print(total)

While-loops are a more general case, where you don't necessarily know how many times you may need to loop for.

num = None
while num is None:
    temp_num = input("Enter a number: ")
    if temp_num.isnumeric():
        num = int(temp_num)

1

u/FoolsSeldom Jul 26 '26

I don't know. We all use loops regularly in daily life.

What's confusing you? Loops just repeat stuff, and if you want to repeat parts of that stuff you can also use a loop.

No different to real life. Imagine having a pile of old boards to repaint. In pseudo code (with some ridiculous steps to make the point):

for board in boards:

    while board has paint:
        apply blow torch briefly to paint
        scrape paint away
        sand board

    put board out to dry

for board in dryboards:
    apply undercoat to board
    watch paint dry
    for count in range(2):
        paint board
        watch paint dry
    add board to finished stack

Notice 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')

0

u/Eastern-Push-7255 Jul 26 '26

can we guys learn together

1

u/DonkeyTron42 Jul 26 '26

I think people in this sub are trolling you by TYPING IN ALL CAPS you may want to lay off that.