For loop is one of the most useful methods to reuse a code for repetitive execution.
For loop is one of the most useful methods to reuse a code for repetitive execution.
For loop is one of the most useful methods to reuse code for repetitive execution. This loop is very common in computer languages, but when it comes to Python, there are significant improvements in the for loop more than in other languages. For loops run a code in a loop until given conditions are met.
In the following code implementation, you can see the basic structure of the for loop. We are iterating through the list “num” from 1 to 10.
num=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in num:
print(i)
1
2
3
4
5
6
7
8
9
10
PythonIf you compare this loop with c++/c/ java languages, you will see one difference that makes these codes more complex and lengthy.
import java.io.*;
class forward_loop{
public static void main(String args[])
{
int list[]={1,2,3,4,5,6,7,8,9,10};
int count=0;
for (int i=0;i<10; i++){
count=count+1;
System.out.println(list[i]+" ");
}
}
}
using namespace std;
int main()
{
int list[5]={1,2,3,4,5,6,7,8,9,10}
for (int i=0;i<10;i++)
{
cout << list[i] << "\n";
}
return 0;
}
Observe the difference between these codes and Python code. Python code has only 3 lines and is fairly understandable. Java and C++ codes span over 10 lines and may look ambiguous to a newcomer.
The range is an independent function, but it is popularly used for loops. It creates an iterable number generator with given parameters.
Range function has 3 arguments ”range (start, stop, step)”. It can operate with 1 stop argument. By giving a stop argument, the range will iterate over 0 to stop.
range(0,10,2)
The range can operate with a minimum of 1 argument. Remember that range does not return a list. It returns numbers per cycle.
list(range(0,10,2))
[0,2,4,6,8]
Pythonnum=[1,2,3,4,5]
for i in range( 5 ):
print(num[i])
1
2
3
4
5
PythonIt can operate with 2 arguments (start, stop). By giving these arguments, the range will iterate from start to stop.
for i in range(1,5):
print(i)
1
2
3
4
PythonIt can operate with 3 arguments (start, stop, step). By giving these arguments, the range will iterate from start to stop and with skip numbers with each step.
for i in ranage(0,10,2):
print(i)
0
2
4
6
8
PythonLet’s vary the look of the output with simple in changes in print. Using the end parameter cancels out the new line that executes with every print statement.
num=[x for x in range(5)]
for i in num:
print(i,end=' ')
0 1 2 3 4
Pythonnum=[x for x in range(5)]
for i in num:
print(i,end='\t')
0 1 2 3 4
Pythonstring='the quick browm fox jumps over the lazy dog'
for i in string:
print(i,end='')
the quick browm fox jumps over the lazy dog
PythonWe can use continue to end and start a new iteration in the for loop rather than executing the remaining body of code in the loop.
In the following example, we don’t want to print 4. So we skip if 4 occurs in x by using the continue statement.
even=[x for x in range(10) if x%2==0]
print(even)
for x in even:
if x==4:
continue
print(x)
0
2
6
8
10
12
14
16
18
PythonWe mostly use a pass statement in place of a code for the continuation of execution. If a scope of a function has no statement body, we can use pass in that scope to avoid any errors.
for i in even:
pass
Observe in the following code there is no statement body of if, by using pass we are continuing to execute for loop.
for i in even:
if i>0:
pass
print(i)
0
2
4
6
8
PythonUsing a loop inside another loop, Scope creates a nested loop. By using nested loops, we can handle problems with complex and multiple recursive operating structures. It also gives the power to operate over matrix-like structures of lists that are Multi-Dimensional lists.
a = [[1,3,5,7],
[2,4,6,8],
[8,6,4,2],
[7,5,3,1]]
for i in a:
print(i)
[1, 3, 5, 7]
[2, 4, 6, 8]
[8, 6, 4, 2]
[7, 5, 3, 1]
PythonLet’s perform a multiplication operation on a diagonal row in a matrix.
for i in range(len(a)):
for j in range(len(a[i])):
if i==j:
a[i][j]*=20
print(a[i][j],end=' ')
print()
20 3 5 7
2 80 6 8
8 6 80 2
7 5 3 20
PythonThe else block in a for-loop executes only if the loop has completed all of its iterations. If the iterations are interrupted in the for-loop, the else block won’t be executed.
for i in range(1,4):
print(i)
else:
print("No Break")
1
2
3
No Break
Pythonfor i in range(1,4):
print(i)
break
else: # Not executed as there is a break
print("No Break")
1
PythonThis can be a super guide for you to start and excel in your data science career.
ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.
What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!
Start using NotebookLM today and embark on a smarter, more efficient learning journey!
This article will walk you through the different data types of numbers.
TF-IDF method belongs to domain of information retrieval,
Python has several types of operators. Mathematical, Assignment, Comparison, Logical, Identity, Membership, Bitwise operators.
If a software language is easy in terms of declaring variables, then consider that half of the time and efforts are saved. Python is one of the easiest and convenient languages for declaring variables.”
If a software language is easy in terms of declaring variables, then consider that half of the time and effort are saved. Python is one of the easiest and most convenient languages for declaring variables.
Chatbots are a necessity of the current IT era. Chatbots offer visitors round-the-clock customer service.
Improve your analytical skills by practicing the following tasks
Improve your analytical skills by practicing the following tasks
Leave a Reply
You must be logged in to post a comment.