Python Programming

Python Programming

For-loop in Python

In this Python Programming blog, We are going to learn for-loop in Python.

What are loops?

Loops are basically use for running a same line of code multiple times. Suppose, we want to print any number(i.e, 1) multiple times(i.e, 100 times) then we don’t need to write a print statement 100 times, instead of we can use loop. We can put our single line print statement in a loop which consist a argument 100 in our case. The argument decides “How many times our code would be execute”. There are multiple types of loops in Python like for-loop, while-loop etc. Don’t worry I would tell you about every single loop in this Python Programming Series.

For-loop in Python Programming

For-loop is a loop which is widely use in multiple programming languages, Python is one of those. The syntax of using for-loop is very simple in Python Programming Language. For-loop line is always ended with a “:” in python. The for-loop body part code is written with some gap in the beginning of every line of the loop body.

for i in range(100):
    print(1)

Here, 100 is an argument which is given to for-loop. So, for-loop would run 100 times. It will print 1 on every execution. So, It will print 1 100 times.

Range Function : It is a function which took an integer as an argument. It ranges from 0 to n-1. In our case, n is 100 so, It will run from 0 to 99.

Nested For-loop in Python Programming

When a for-loop body contains another for-loop then that is called nested for-loop.

for i in range(100):
    for j in range(10):
        print("Hello")

Here, “Hello” would print 1000 times. As first loop will run 100 times and each time inner loop will run 10 times.

Time Complexity

For-loop : O(n)

2 Nested for-loop: O(n^2)

Here, n is the number of times, our loop will execute.

If you want to know about the basic structure of Python then you can go through my other blog in which I discussed about basics and structure of Python.click here

Recent Job Posts

[ Important ]

  • All Company names, logos, and brands are the Intellectual Property of their respective owners. All company, product, and service names used on this website are for identification purposes only.
  • We are not associated with any company/agency/agent whose jobs are posted on mechomotive.com, We are just an information provider for job openings. Read our Disclaimer Policy and Term of Service for more information

For more job offers, CLICK HERE