Hello Everyone, In this article, we will learn about For Loop in Python | Learn Concept With Example – Part 1  to make sure everyone can able to learn For Loop concept in better and easy way.

Before jumping to the coding section, let’s first know a little bit about Python Programming.

Introduction to Python

  • Python is a popular programming language created in 1991 by Guido Van Rossum.
  • In these days, Python used for scripting, web development, software development, big data, data science, IoT application and so on.
  • By using Python, we can able to do server side work as well as can able to handle big data and perform complex mathematics.
  • Python is one of the most used as well as the most in-demand in the IT industries.
  • The best part about Python, It is easy to learn and write fewer codes compared to other programming languages such as C, C++, JAVA etc.
  • For more information about python, you can visit the Python Office Website.

That is the short introduction about Python. Now let’s first know about what is the actual meaning of FOR LOOP.

What is For Loop?

  • For Loop in the programming language is used for iterating over a sequence, whereas the sequence can be anything such as list, tuple, string etc.
  • With the for loop, we can execute a set of meaningful statement one by one on each item in sequence and can able to execute similar operation by single operation.
  • In For Loop doesn’t require an indexing variable to set any beforehand data.

These aspects make for loop as an important part of any programming language.

In this article, we will cover few For Loop one by one and will see in details about each and every statements meaning as how is it useful and what actual meaning particular statement want to deliver.

The syntax of For Loop:-

for {variable} in {sequence}: 
       {statements} 
else: 
       {statements}

Here, you can able to see in for loop:

  • start with for keyword followed by variable information where we can assign variable.
  • in {sequence} indicates for variable will match in particular sequence data.
  • {statement} indicates for once variable will match with sequence, then statement run.
  • In the case for statement doesn’t meet the expected scenario, then else statement will execute.

It’s so easy and simple to understand, isn’t it?

Now, the time came to play with some actual For Loop example to understand in more depth.


Program 1:- For Loop with Number List

In this program, we are going to print all the number which comes in the list in sequence. So For Loop will read each and every number and print it.

for i in [1,2,3,4,5,6,7,8,9,10]:

    print(i)

Here i is a variable go through the list [1,2,3,4,5,6,7,8,9,10] and will check number store value in variable i and print it.

Quite simple and easy to understand. isn’t it? Now let’s switch to next For Loop program with text/string/word.


Program 2:- For loop with text/string/word

In this program, we are going to print all the text/string/word which comes in the list. So for loop will read each and every text/ string/word and print it.

 name = ["Nandan","Kundan","Chandan"]

for i in names:

    print(i) 

Here we first define a list of names into the variable name. and then run for loop with variable i to take one text/string/ word, read and print it.


Program 3: For loop with Number and String mix.

In this program, we are going to print all the number as well as a string from a mixed list. In this program, I will show you that, you can define a string with single and double quote both in the same list and you will get the correct output.

 data = [1,2,3,'Nandan',"Kundan",'Chandan']

for i in data:

    print(i)

Here you can see, in data variable, I provided numbers as well as string and for loop works perfectly with mixed data and reading each and every variable and printing the data in the same way.


Program 4: For Loop with Range data

In this program, we are going to print i variable whereas variable i will take data from range function.

Range function used to pass the data in defined sequence.

 for i in range(5):

    print(i)

Here, you can see, as we are using range(5) it means data will become as [0,1,2,3,4] because by default range start with 0 and rest-activity will be done as we discussed in above programmes.


Let’s see another example with Range function.

In this example, I am going to use range function with the start and end range number.

 for i in range(1,10):

    print(i)

Here, you can see, as we are using range(1,10) it means data will become as [1,2,3,4,5,6,7,8,9] because start position index is 1 which indicate 1 and end range index is 10 which indicate 9, so print(i) will give a detailed list.


Let’s see another example with range function with an increment number.

 for i in range(1,10,2):

    print(i)

Here, you can see, as we are using range(1,10,2) it means data will start with 1 and it will give data with increment data of 2, so it means data will become as [1,3,5,7,9], so print(i) will give data as 1,3,5,7,9.


Conclusion

I hope, you learn about For Loop in Python | Learn Concept With Example – Part 1. In case of any confusion, you can comment down and I will try my best to reply as soon as possible.

For other tutorials, related to Python and other big data technology, please visit my website 

Share.

About Author

Comments are closed.