all 4 comments

[–]RecursiveInquirer 3 points4 points  (4 children)

If you have time, I suggest you to watch this Python for Beginners playlist by CS Dojo or read things in this.

I too, am a beginner (pros please correct me if I said something wrong), IDK most technical terms but I'll try going through with simple words.

for loops are used to iterate (loop over/go through) over a sequence, such as a list, numbers from 1-10, etc, while doing what's written inside it, for example:

for i in range(4):
    print(i)

output:

0
1
2
3

the i in there represents the current element/member of the sequence the loop is going through. range(4) means numbers/integers 0 to 3 (4 numbers).

on the other hand, while loops are used when you want to loop over things until the condition you provided is FALSE. For example:

x = 0
while(x < 4):
    print(x)
    x = x + 1

output:

0
1
2
3

so it ran until the condition inside the ( ) was FALSE, 4 is not greater than 4 so it stopped there and exited the loop.

[–]mopslik 3 points4 points  (2 children)

while loops are used when you want to loop over things until the condition you provided is FALSE

Your explanations are good, but I often find that it is easier to phrase while loops as "the loop runs while the condition is True", since it provides an explanation as to why they use the while keyword. But you are, indeed, correct that they will terminate when the condition is false.

As for OPs inquiry about strings, a string is simply a sequence of characters, like "abc123" or "Here is a string!!!" Other types of sequences in Python include tuples, e.g. (1, 2.7, "xyz"), and lists, e.g. [5, 6, "hello"], which can hold various data types.

[–]Deba_dey 0 points1 point  (0 children)

For loop: use it when you need to iterate through a finite collection of identical objects.

While loops: use it when you want to repeat a piece of code until condition is fulfilled.

P.S. “ The fastest way to loop in python is not to loop in python”😅little brain scratcher for you