Python Programming Tutorial (Part 3) – LOOPING (For Loop).

in #steemiteducation7 years ago

For Loop:

Probably the most powerful thing about computers is that they can repeat things over and over
very quickly. There are several ways to repeat things in Python, the most common of which is the for loop.
monitor-1307227__480.jpg

Example 1:

The following program will print Hello ten times:
IMG_20180406_164219_087.jpg
The structure of a for loop is as follows:
for variable name in range( number of times to repeat ):
statements to be repeated
The syntax is important here. The word for must be in lowercase, the first line must end with a
colon, and the statements to be repeated must be indented. Indentation is used to tell Python which statements will be repeated.

Example 2:

The program below asks the user for a number and prints its square, then asks for another number and prints its square, etc. It does this three times and then prints that the loop is done.
IMG_20180406_164142_003.jpg

IMG_20180406_163953_065.jpg
Since the second and third lines are indented, Python knows that these are the statements to be
repeated. The fourth line is not indented, so it is not part of the loop and only gets executed once, after the loop has completed.

Looking at the above example, we see where the term for loop comes from: we can picture the
execution of the code as starting at the for statement, proceeding to the second and third lines,
then looping back up to the for statement.

Example 3:

The program below will print A, then B, then it will alternate C’s and D’s five times
and then finish with the letter E once.
IMG_20180406_165938_098.jpg

The first two print statements get executed once, printing an A followed by a B. Next, the C’s and
D’s alternate five times. Note that we don’t get five C’s followed by five D’s. The way the loop works is we print a C, then a D, then loop back to the start of the loop and print a C and another D,
etc. Once the program is done looping with the C’s and D’s, it prints one E.

Example 4:

If we wanted the above program to print five C’s followed by five D’s, instead of alternating C’s and D’s, we could do the following:
IMG_20180406_170006_311.jpg

Exercise:

My Advice:

Please ensure you try all the codes in this article for a better understanding. And also attempt all the exercises as well. If you are confused or stuck anywhere fell free to comment below and I or any other programmer in the house will give you a hint.

Thanks for reading patiently! I hope you enjoyed it? Please share your class experience with us. And know that your ideas to make this class a success is also appreciated, just feel free 🙂

My most recent programming articles.

  1. Python programming tutorial (Part 2) - Printing Function (optional Arguments).
  2. Solutions to my exercises on Python Programming (Part 1).
  3. Python programming tutorial 1–(Installing Python, getting user Inputs and running basic programs)
  4. My new Steemit project and how I intend to run it.
  5. Top 3 reasons why everyone should learn programming.

If you miss any of my tutorial don't worry as you can always visit my page to see all my articles, or click on the #edywill tag to see all my recent programming articles.

To get more programing tutorials, tips and technological updates you won't want to miss out, subscribe to my Steemit page by following @edywill.

Sort:  

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly & Featured Posts are voted every 2.4hrs
Join the Curation Team Here | Vote Resteemable for Witness

Coin Marketplace

STEEM 0.26
TRX 0.20
JST 0.037
BTC 94837.57
ETH 3452.01
USDT 1.00
SBD 3.93