[Tutor] Hello

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Thu Apr 20 03:50:25 EDT 2023


Bernard

Loops are used to execute a piece of code based upon specific conditions
(expressions) in the loop statement or within the code block itself. There
is three basic loop structures in any language:

The for loop -- this example is pythons way of a pre-defined number of
iterations over (executions) of a piece of code:

for i in range(1, 11):
    print(i) 

The above code will print 1 to 10. Then exit. Instead of doing:

I = 0
I += 1
Print (i)

I += 1
Print (i)
I += 1
Print (i)
... and so on.

The for loop in Python allows you to execute codes based upon a list and
other data types. For example:

A = [1,2,3,4,5]
For I in a:
    Print (i)

The above will print each element of A.

The while loop is the 2nd basic loop. This loop performs the expression test
before any of the code within the loop block. If true, then nothing is
executed in the loop block. For example:

A = 5
While a >4:
    A -= 1
    Print (a)

The expression "a > 4" is true because a is greater than 4. The count and
print statements are not executed. Change the a variable and the expression
to get different results.

The do ... while loop does the reverse to the while loop. The test of the
expression is at the end. A better question would be, what are common loops
used in python?

My limited experience is the while and for loops.

Sean 
  
-----Original Message-----
From: Tutor <tutor-bounces+mhysnm1964=gmail.com at python.org> On Behalf Of
ThreeBlindQuarks via Tutor
Sent: Thursday, April 20, 2023 2:14 AM
To: Bernard Kofi Agboado <luk4bernard at gmail.com>
Cc: tutor at python.org
Subject: Re: [Tutor] Hello


Bernard, that is an extremely broad question.

My first thought was that in America we eat our loops with fruit but that is
not very helpful.

Could you post a few lines of code that contain an example of loops and tell
us what you do not understand or how to modify it so it does not produce
error messages or wrong results?

Python has lots of constructs that I would call loops. All of them share a
concept of repeating the same code some number of times with some things
changing as you move along.

Are you looking at explicit loops where you see key words like "for" and
"while" with indented code below or some more subtle ones including
comprehensions or generators or the kind where the loop is done invisibly?

Again, send an example or two and say what about it does not make sense. But
the people here are not tutors in the sense of teaching very basic things
that a student should be getting by reading a textbook and listening to
lectures. We would appreciate a more focused question so we can provide more
focused help.



Sent with Proton Mail secure email.

------- Original Message -------
On Wednesday, April 19th, 2023 at 3:30 AM, Bernard Kofi Agboado
<luk4bernard at gmail.com> wrote:


> Good morning, please how best can I understand the loops.?
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list