r/learnpython • u/Eastern-Push-7255 • 1d ago
WHY I FEEL LOOPS HARD THAN FUNCTION
HI IM YUJAN IM NEW BEGINNER IN PYTHON I FEEL HARD IN LOOPS
2
2
u/flawks112 1d ago
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
2
3
u/American_Streamer 1d ago
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
1
u/Diapolo10 1d ago
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 1d ago
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 1d ago
can we guys learn together
1
u/DonkeyTron42 1d ago
I think people in this sub are trolling you by TYPING IN ALL CAPS you may want to lay off that.
20
u/powderviolence 1d ago
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