Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
Python programming language has been one step ahead of other programming languages from the start. Loops in Python has a similar advantage when it comes to Python programming. In this article, we will learn about Python For Loop and how we can use it in a program. The following concepts are covered in this article:
Check out Edureka’s Python Course, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
With immense applications and easier implementations of Python with data science, there has been a significant increase in the number of jobs created for data science every year. Enroll for Edureka’s Python For Data Science and get hands-on experience with real-time industry projects along with 24×7 support, which will set you on the path of becoming a successful Data Scientist,
Let’s go ahead and start this article with a basic introduction to for loop in python.
A for loop is used to iterate over sequences like a list, tuple, set, etc or. And not only just the sequences but any iterable object can also be traversed using a for loop.
Let us understand the for loop with the help of a flowchart shown below.
The execution will start and look for the first item in the sequence or iterable object. It will check whether it has reached the end of the sequence or not. After executing the statements in the block, it will look for the next item in the sequence and the process will continue until the execution has reached the last item in the sequence.
Let us understand the for loop syntax with an example:
x = (1,2,3,4,5) for i in x: print(i)
Output: 1
2
3
4
5
In the above example, the execution started from the first item in the tuple x, and it went on until the execution reached 5. It is a very simple example of how we can use a for loop in python. Let us also take a look at how range function can be used with for loop.
In python, range is a Built-in function that returns a sequence. A range function has three parameters which are starting parameter, ending parameter and a step parameter. Ending parameter does not include the number declared, let us understand this with an example.
a = list(range(0,10,2)) print(a)
Output: [0,2,4,6,8]
In the above example, the sequence starts from 0 and ends at 9 because the ending parameter is 10 and the step is 2, therefore the while execution it jumps 2 steps after each item.
Now let us take a look at an example using python for loop.
def pattern(n): k = 2 * n - 2 for i in range(0,n): for j in range(0,k): print(end=" ") k = k - 1 for j in range(0, i+1): print("*", end=" ") print("r") pattern(15)
Output:
In the above example, we were able to make a python pyramid pattern program using a range function. We used the range function to get the exact number of white spaces and asterisk values so that we will get the above pattern.
Let us take a look at how we can use a break statement in a python for loop.
Break in python is a control flow statement that is used to exit the execution as soon as the break is encountered. Let us understand how we can use a break statement in a for loop using an example.
Let’s say we have a list with strings as items, so we will exit the loop using the break statement as soon as the desired string is encountered.
company = ['E','D','U','R','E','K','A'] for x in company: if x == "R": break print(x)
Output: E
D
U
In the above example, as soon as the loop encounters the string “R” it enters the if statement block where the break statement exits the loop. Similarly, we can use the break statement according to the problem statements.
Now, let us take a look at how we can use python for loop in lists.
A list in python is a sequence like any other data type, so it is quite evident on how we can make use of a list. Let me show you an example where a for loop is used in a list.
color = ["blue", "white"] vehicle = ['car', 'bike', 'truck'] color_comb = [(x,y) for x in color for y in vehicle] print(color_comb)
Output: [('blue', 'car'), ('blue', 'bike'), ('blue', 'truck'), ('white', 'car'), ('white', 'bike'), ('white', 'truck')]
Let us also take a look how we can use continue statement in a for loop in python.
Let us understand this the same example we used in the break statement, instead of break, we will use the continue statement. It is also a control statement but the only difference is that it will only skip the current iteration and execute the rest of the iterations anyway.
company = ['E', 'D', 'U', 'R', 'E', 'K', 'A'] for x in company: if x == "R": continue print(x)
Output: E
D
U
E
K
A
In the above example, the continue statement was encountered when the string value was “R”, so the execution skipped that particular iteration and moved to the next item in the list.
Let us now look at a few other examples for a better understanding of how we can use for loop in Python.
Here is a simple for loop program to print the product of any five numbers taken from the user
res = 1 for i in range(0,5): n = int(input("enter a number")) res *= n print(res)
Output:
Here is another simple program to calculate area of squares whose sides are given in a list.
side = [5,4,7,8,9,3,8,2,6,4] area = [x*x for x in side] print(area)
Output: [25, 16, 49, 64, 81, 9, 64, 4, 36, 16]
Now that we are done with the for loop concepts, here are a few tutorials that will help you learn the programming language in a structured way.
This brings us to the end of this article where we have learned how we can use For Loop In Python. I hope you are clear with all that has been shared with you in this tutorial.
We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Python developer. The course is designed to give you a head start into Python programming and train you for both core and advanced Python concepts along with various Python frameworks like Django.
If you come across any questions, feel free to ask all your questions. Put them in the comments section of “Python for Loop” and our team will be glad to answer.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 25th January,2025 25th January SAT&SUN (Weekend Batch) | View Details |
edureka.co