Python For Loops
Loops are very useful programming principles for executing certain logic or a block of code multiple times for a collection of items like those found in string, list, tuple and dictionary data types.
One such looping technique is using a
for loop. A for loop is the most widely used looping technique for iterating over a list, dictionary, string or a tuple. It repeats a certain logic over and over again for different items unless there are no more items left or an exit-condition is met. An exit-condition is usually something that will stop the loop.
Let’s look at a small flow chart that explains how for loop works:
For loop in Python has following syntax:
for value in list: for loop logic for value
Let’s look at how to use for loop in code snippet below:
Python Range Function
Sometimes while using a loop, you are not necessarily looping through a predefined list or string. Instead, you may wish to loop through a predefined range of values or numbers.
range() function comes really handy when you want to loop for a certain range of numbers. Based on the parameters passed to the
range() function, it will automatically generate those values, which can be then used inside of a for loop.
range() function is defined as follows in two different variations:
range(stop) range(start, stop[, step]) ```
start defines the starting number from which range should start.
stop defines the number at which the range should stop. The range will stop at the value defined by
stop minus 1.
step defines how the numbers should step up or increment in the range.
Let’s take a look at how
range() function might come handy inside a for loop syntax:
Python Enumerate Function
When using loops, it is often handy for developers to remember the index or count of the item that has already been looped through.
enumerate() function in Python allows you to loop through an iterable while also providing the index or count of each item being looped through.
enumerate() function is of the following form:
``` enumerate(iterable, start=0) ```
Let’s take a look at the code snippet below to understand how to use
enumerate() function with a for loop:
Kick-Start Your Data Career
We offer a wide variety of programs and courses built on adaptive curriculum and led by leading industry experts.
- Work on projects in a collaborative setting
- Take advantage of our flexible plans and scholarships
- Get access to VIP events and workshops
The Python certificate course provides individuals with fundamental Python programming skills to effectively work with data.
Taught by data professionals working in the industry, the part-time Data Science course is built on a project-based learning model, which allows students to use data analysis, modeling, Python programming, and more to solve real analytical problems.
The Data Science bootcamp is an intensive course designed to launch students' careers in data.
The part-time Data Analytics course was designed to introduce students to the fundamentals of data analysis.