PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 2 - PYTHON LOOPING STRUCTURES(For Loops) | WEEK 3

in Steem4Nigeria2 years ago (edited)

python day2 week3 banner.PNG


Good day everyone!
Hope you're doing great!! Welcome back to my Python programming class. Today is the second day of the Python Programming class week 3. If you have followed my week 1 & 2 till the end, you must have acquired some basic Python programming skills that can help you start your programming career. However, if you did not follow my week 1 & 2 classes, please visit my blog now @anyiglobal to see all my week 1 & 2 classes.

I brought out this idea of teaching python on steemit because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on steemit communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.

This is my first time of posting in this community, and I believe this community will treat me right!

Note: The English language used in this class is literally understandable by a layman. So let's begin...


PYTHON LOOPING STRUCTURES

According to English dictionary;

Loop is a programmed sequence of instructions that is repeated until a particular condition is satisfied.

In Python programming language, loop structures are used to iterate certain range of numbers or sequence until a certain condition is met. Looping structures are used in application programs as at when required to execute a set of statements iteratively for a predetermined number of times until a certain condition is met. To achieve this, we make use of "For loops" and "While loops". We will discuss For loops in this class with examples.


PYTHON FOR LOOPS

For loops is a looping structure used for iterating over a sequence of lists, tuples, sets, dictionaries, strings, e.t.c. which executes the statements for the number of times equal to the number of elements in the sequence or equal to the number of values generated by the range() function. Once the condition specified in the statement is met, the program breaks out of the loop. Don't worry we will see the examples to this for loop sooner in this class below.

"For Loop" Syntax:
for iterater_variable in sequence or range:
  statement(s)

iterater_variable could be variables like x, i, y, z, e.t.c. However, the "in" operator functions as an operator that extracts one element at a time from the sequence or a number generated by the range() function and then assigns to the iterater variables.

THE RANGE FUNCTION:
We can use the range() function to iterate sequence of numbers.

For Example,
Let's loop through eight range of numbers starting from 0-8:

for i in range(0, 8):
  print(i)

Output
0
1
2
3
4
5
6
7

From the example we have above, we can see how we iterated through a range of 8 numbers (number 8 not inclusive) using the For loops. If we watch closely to the code, you will observe the last number 8 is not printed to the screen. This is because, Python syntax does not include the last number in the range of numbers. As soon as the condition specified in the range function is met, the loop breaks out i.e. the iteration stops and print the extracted elements to the screen.

Also, do well to observe the colon ":" at the end of the for loop; it shows that the statements is a block statement. The colon is very important, please always be on the lookout when writing for loops. The "i" in the for loop statement is iterater_variable discussed above.

python for loops.PNG
for loops in range function


INCREMENTING THE RANGE() FUNCTIONS:
In the range function, we can increment the sequence by specifying a number as the increment value in the third parameter.

For Example;
Let's increment a sequence in range 0-20 by adding a third parameter "2" as the increment value.

for i in range(0, 20, 2)
  print(i)

Output
2
4
6
8
10
12
14
16
18

python for loops increment.PNG

In the screenshot above, we increment the range() function by specifying 2 as the third parameter. Please make sure to make more research on Python for loops to have a better understanding of it.

NB: Copy the code in the example above and try it out in your text editor. Use this material to learn more about Python for loops.


LOOPING THROUGH A STRING:
We can as well loop through a string of characters to extract all the characters in the string. String contains sequence of characters which are iterable objects.

For Example;
Let's loop through the letters in the word "Steemit".

for x in "Steemit":
  print(x)

Output
S
t
e
e
m
i
t

In the example above, we loop through the sequence of string characters in the word "Steemit" to extract all the characters in the string.

python looping through a string.PNG
Looping through a string

From the screenshot above, we see how we used the for loops to extract all the characters in the word Steemit. Please make sure you practice along with me so that you will master what I teach.

NB: Copy the code and try it out in your text editor. Use this material to learn more about Python for loops.


THE BREAK STATEMENT
There are some scenario whereby we want the loop to break out when it reaches a specified item in a sequence. Therefore, to achieve that, we make use of the "break statement". With the break statement, we can iterate and stop the loop before it loops through all the items in the sequence.

For Example;
Let's loop through a list of Fruits and stop the loop when the loop encounters an item called "apple".

Fruits = ["banana", "cherry", "orange", "apple", "mango"]
for x in Fruits:
  if x == "apple":
    break
  print(x)

Output
banana
cherry
orange

python for loop break statement.PNG
The break statement

From the screenshot above, we implemented the break statement by using "break" keyword. We were able to stop the iteration and break out of the loop when the loop encountered the specified item, "apple", hence the output on the screen.


THE CONTINUE STATEMENT
This statement is used to skip a specified item in a sequence and continue our iteration from the next item. This is achieved using the "continue" keyword.

For Example;
Let's give instruction to our interpreter not to print the item "cherry" in the given list of fruits.

Fruits = ["banana", "cherry", "orange", "apple", "mango"]
for x in Fruits:
  if x == "cherry":
    continue
  print(x)

Output
banana
orange
apple
mango

python for loop continue statement.PNG
The continue statement

As we can see from the screenshot above, we used the "continue statement" to skip the specified item in the sequence i.e. cherry and print the rests.

NB: Copy the code and try it out in your text editor.


FOR LOOP WITH ELSE
The "else statement" in for loops just specifies a block of code that will execute after the for loop finished it's execution.

For Example;

for x in "Steemit":
  print(x)
else:
  print("Execution Finished Successfully")

Output
S
t
e
e
m
i
t
Execution Finished Successfully

python for loop with else.PNG
Python for loop with else

From the above code, the else statement in the else block executed as soon as the for loop finishes it's execution.

NB: Copy the code above and try it out in your text editor. Use this material to learn more about Python for loops.


Conclusion

In Python programming language, Looping structures are very necessary to save the programmer's time and space of writing many lines of codes. Using for loops in any program increases the efficiency of the program. If you can ask yourself this question; "what if there's no looping structures, how can I access many items in a sequence at a time and print all of them to the screen?". In answering this question, you will find the importance of looping structures in a program. It is an integral part of any programming language. In our next class, we will look at "Python While Loop Structures". Thanks for being part of this class, I appreciate your coming!!



This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed.

Please do well to follow this blog, and also don't forget to resteem this post so that it can reach larger number of steemians who wants to learn this skill.


Am glad you participated in this class! I believe you have learnt something new today.

I am @anyiglobal, a Computer Scientist, Software Engineer and a Blogger!



THANKS TO THE COMMUNITY ADMINS FOR THE SMOOTH RUNNING OF THIS COMMUNITY
Cc: @steem4nigeria @reminiscence01 @ngoenyi

Sort:  

Thank you for contributing to #LearnWithSteem theme. This post has been upvoted by @Reminiscence01 using @steemcurator09 account. We encourage you to keep publishing quality and original content in the Steemit ecosystem to earn support for your content.

Club Status: #Club5050

Sevengers Comment GIF.gif

Regards,
Team #Sevengers

After checking out one of your lectures, I was really impressed with what you have been sharing with us sir, and I decided to start following all your courses. Keep up with the good work.

Thanks so much bro for the acknowledgement! This is what I want to be hearing from my students to keep me motivated all the time! Please as you have started following my classes, make sure you don't miss any of them so that you will stay on track with us!! God bless you!!!

Regards,
Anyiglobal.

 2 years ago 

Congratulations, your post has been supported by @steem4nigeria. This is the official community account of Nigerians on Steemit. You can reach us here on our community account.

Manual Curator : @Reminiscence01

Subscribe and Join Steem4Nigeria Telegram
Discord Facebook Twitter

Thanks so @steemNigeria for the support! I am dedicated to giving Nigerians in this community the necessary skills they need to start their programming career.

Thanks SC01 for the support I appreciate!