Home >>Python Tutorial >For Loops Python

For Loops Python

The Python For Loops

This is the Python for loop module of the entire tutorial. And as we have mentioned before in this Python tutorial that if you wish to iterate over a sequence then the for loop is used.

The sequence can be a list, a dictionary, a tuple, a string, or a set. Just like many other programming languages that are object-oriented similarly this loop works more like an iterator than the way the for keyword functions in many other languages.

It is important for you to remember that with the help of the for loop you can execute a number of different statements where there will be a single statement for every single item present on the list like a tuple, a set, a dictionary, and many others.

For example, if you wish to print each fruit in a list of fruits then  

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print ( x )

  It is important for you to remember that you do not need to set an indexing variable before a for loop. This is a rather important Python for loop step that you need to remember.  

To Loop Through a String - For Loops

Strings contain a different sequence of characters and because of this particular feature strings are iterable objects. For example, if you wish to loop through the different letters that are present in the word banana then

for x in “banana” :
print ( x )

 

The Break Statement - For Loops

If you wish to stop any particular loop before it is even done lopping then you can choose to use the break statement. If you wish to exit the loop when x is “banana” then the Python for loop example for this is mentioned below  

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print ( x )
if x == “banana” :
break

  Another example is mentioned below or a case where you would want to exit a loop when x is “banana” but this particular time you would want the break to occur before the print then  

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana” :
break
print ( x )

 

The Continue Statement - For Loops

If you wish to stop the iteration of the loop that is currently going on and instead start with the next one already then to do that you would be required to use the continue statement. If you do not want to print banana then the Python for loop example for this is mentioned below  

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana” :
continue
print ( x )

 

The Range Function - For Loops

If you wish to loop through a set of different codes a specified number of times then it is suggested that you should use the range () function. It is also important for you to remember that the range () function helps in returning a sequence of number, it starts from 0 by default, and there is also an increment of 1 that happens by default. This sequence also ends at a specified number.

For example

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

It is important for you to remember that here the range (6) is not a sequence of 0 to 6 but instead it is from 0 to 5. This is one of the most important things that you should be aware of in this loop for Python module. While using this particular feature if you wish to start the entire sequence from a particular value then you can do that by adding a type of parameter like range (2, 6). This will mean that the value will be from 2 to 6.

For example, if you wish to use the start parameter then  

For x in the Range - For Loops (2, 6):
print ( x )

  It is also important for you to remember that you can also specify the increment number in this programming language while using the for loop function. This can be achieved by adding a third parameter. If you want the sequence to have an increment of 3 instead of 1 then the python for loop example for this is mentioned below  

for x in range (2, 30, 3):
print (x)

 

The Else In For Loop - For Loops

As a developer who is using this particular programming language, you should be aware of the fact that if you see an else keyword in the for loop then that just specifies a block of code that needs to be executed whenever the loop is finished. If you wish to print all the number from the 0 to 5 and you also wish to display a message when the loop is ended then the Python for loop example for this is mentioned below  

for x in range (6) :
print ( x )
else:
print (“Finally Finished!”)

 

The Nested Loops - For Loops

A nested loop can basically be defined as a type of Python for loop that is present within another loop itself. You should also remember that this ‘inner loop’ will only be executed a single time only whenever you execute an ‘outer loop’.

For example, if you wish to print one adjective for one single fruit then

adj = [“red”, “big”, “tasty”]
fruits = [“apple”, “banana”, “cherry”]
for x in adj:
for y in fruits:
print ( x, y )

 

The Recursions

Python is a programming language that also accepts a number of different function recursion. This basically means that in this programming language a defined function will be able to call itself. It is important for you to know the recursion is a quite common mathematical and programming concept.

This function basically means that it can call itself. This presents the developers or the users with the benefit of easily looping through a data to reach to a specific result. This is one of the most important concepts that you must be aware of in this Python for loop tutorial.

However, it is important for every developer to be quite careful while using recursion as it becomes quite easy to slip into a kind of function that never actually terminates or a kind of function that just uses a lot of processor or memory power.

But if you do end up writing recursion correctly then this Python for loop step can be a very efficient and an elegant approach to the entire concept of programming. To understand this concept better you should look at the below-mentioned example.  

def tri_recursion ( k ) :
if ( k>0 ) :
result = k+tri_recursion ( k-1 )
print (result)
else :
result = 0
return result

print (“ \n\nRecursion Example Results”)
tri_recursion ( 6 )

  In this above-mentioned python for loop example, the tri_recursion is a type of function that we have used and defines it in such a way that it calls itself (“recurse”). We have also used a variable and that variable is k.

This variable gets a decrements of -1 whenever we recurse. The recursion eventually ends in the example whenever the condition is zero or not greater than zero. If you are a new developer then it is important for you to remember that it is okay to be a little confused about this but you should continue practicing as much as you can so that you are able to learn more about all these features in a better way. With this, we finish the Python for loop part of our Python tutorial.


No Sidebar ads