Python for Hacking | Part 5 | concept of loops | while loops and for loops

Python for Hacking | Part 5 | concept of loops | while loops and for loops

Python for Hacking | Part 5 | concept of loops | while loops and for loops

5.1 Introduction


In this post, we are going to discuss the concept of looping in Python. We will discuss in detail the concept of while and for loops. We will learn what are while loops and for loops and how to use them.

So, let's dive deep in.

5.2 The while loop

There are many things which a computer can do better than a human. One of such things is to repeat the same task a lot of times and without doing any error. It's obvious that if a person had to do a task involving a huge number of repetition, his chances of doing a mistake is very high. So, we will assign this task to a computer or computer-like machine for better accuracy and speed.

This task of repeating things is called iteration. In Python, we have the concept of loops which we can use in order to complete a repetitive task in a much faster and accurate way. 



In python, we can achieve this task by using a while or a for loop. Now, let's discuss the while loop in detail. In a while loop, if the condition is false, the execution of the program will skip the while statements and the statements after it will be executed. If the condition remains true, all the statements under the while loop will be executed and it will execute the statements after it.

Look the example below of the execution of the while loop in a function:

def printing(n):
  while n >  0:
    print(n)
    n = n-1 

printing(9) 

In the above example, we have made a function which will print a number and all its predecessors until it is greater than '0'. When n will be equal to or less than 0, the program will stop the execution.

See the complete execution of the above program in the below image:




While statement comes very handy when we have to check a condition a huge number of times and the execution depends on its output.

5.3 The for loop

A for loop is used to create a loop based on a sequence. This sequence can be a list, a tuple, a dictionary, a set, a range, or a string (we will discuss a tuple, a list, a dictionary and other things in detail in coming posts).

If you have studied other programming languages and know about the for loop, you probably had known that in other languages, when you use the for loop, you have to specify an indexing variable, but in the Python, it is not required.


The syntax of a 'For loop'

Now, let's see how to use a for loop in Python:

We will see a very basic example of a for loop to make things clear to understand, 

number = ["one","two","three","four"]
for x in fruits:
   print(x)

We will get the following output on our screen when we will run this program:

one
two
three
four

Now, let's try to understand what we had done in this program. Firstly, we had made a list of values of type string which contains four strings.

Then, we had used the for loop to print the values of the list. 

Note: look carefully the syntax of the for loop, we have used two keywords: for and in. These are the default keywords in Python.

This statement will basically select all the values of the list: number and will print on the screen.

For loop in a string 

Now let's see how a for loop will be used in a string. 

x = 'strong'
print(x[2])

Output:

r

In the above program, we will get the output as 'r' because each character in a string will act as a value of a list, just as in the case of our previous example and the for loop statement will look for the desired value and will print it on the screen.

Now, let's try to understand what is the meaning of the statement x[2]?

Basically, in Python, the indexing of a number starts from a whole number that is 0. Means instead f 1, our counting will start from 0. 

In the word 'strong', 

s tends to 0, t tends to 1, r tends to 2, o tends to 3, n tends to 4 and g tends to 4.

So, when x[2] is executed the list x containing the word strong looks for the digit on the place 2 which is r, will be printed on the screen.

For loop usage with the range() function

We can also use the for loop with the range function. Range function returns a sequence of values and it terminates at a specified value.
Look at the below example,

for x in range(5):
  print(x)

Output:

0
1
2
3
4

In the above example, what has happened is that the range function has taken the value of x from the range(5) or from 0-5.

Remember, 5 has been excluded because counting has been started from 0.

You can also use else statements inside a for loop.

Similarly, we can nest multiple for loop statements inside each other and get our desired result. Nesting for loops means running multiple for statements inside a single statement. Remember, in this case, the outermost loop will be executed first.



Conclusion

So, basically in today's post, we have learned the concept of looping in Python in detail. We had studied about the for loops, while loops and their usage in real programs. 

Hope this tutorial is cleared to you. If you have any doubts, please ask them in the comment box below. 

Have a nice day!




Share:

Email Subscription

Enter your email address:

Delivered by FeedBurner

Wikipedia

Search results

Translate

Contact Form

Name

Email *

Message *

Featured Post

How hackers can hack your car?

How hackers can hack your car? For a considerable length of time, AUTOMAKERS and programmers have thought about a smart assault that ...

Search This Blog

Archive

Subscribe

Comment

Popular Posts

Recent Posts