Introduction
Setup
Operators
variable
String
Number
Dtype & Comment
Lists
Tuples
Sets
Dictionaries
if else statement
For Loop
While Loop
iterator & iterable
Pre-Built functions
Function
Decorators
generators
Types of errors
Exceptions Handling
OOP
Regular expression
Create module
OS module
Date module
Random module
Math
Statistics module
Exercise 1
Exercise 2
Exercise 3
Learn Data Structure & Algorithm
Learn Numpy
Learn Pandas
Learn Matplotlib
Learn Seaborn
Learn Statistics
Learn Math
Learn MATLAB
Learn Machine learning
Learn Github
Learn OpenCV
Learn Deep Learning
Learn MySQL
Learn MongoDB
Learn Web scraping
Learn Excel
Learn Power BI
Learn Tableau
Learn Docker
Learn Hadoop
For loop is used for iterating over a sequence. Here sequence means list, tuple, dictionary, string, or a set. By using for loop a set of statements can be executed.
In for loop, you will write "for" and then you will take a variable to store the value(like i in the example
1). You can use any word or letter as a variable but can't use a number.
Then write "in" and then that variable name from where you want to take a value and in the end use a colon(:).
" in" is used to take values from that variable(from where you want to take the values) and put it in that
variable which you wrote after for( in the example i).
In for loop, value comes one by one. If you have 5 values then the first time i variable will have the first
value then the second time i will have the second value. This way the process goes on. For each value, all the
code written inside the loop will run completely. Suppose you have three values and inside for loop you have
three math equations. So each time the loop runs, each time the three equations will be executed. When the
loop runs first time, it will work with the three equations and will give an output. When the loop runs second
times with the second value it will execute those three equations again. This will happen each time the loop
runs. So we can say that if the loop runs three times then the equation or code written inside the loop will
also completely run 3 times. The code we write inside the loop execute line by line. It means, first line then
the second line then the third line and this way the process goes on.
In example 1, we have a string in the z variable. We took the string value characters one by one in a for loop and then print it. The first time R goes to i and then it prints i value means R. Then second time a goes in i and print i means a. This way this process happens.
Example:2
In example 2, you took z variable values in i, one by one and print i(means z variable value which is present
in i). So when the loop is running, the first value of z will go in i and will print it and then second and
then third and so on. z have first value a then b then c, d. So that's why you got the result
a
b
c
d
While using the for loop, if we want to stop the loop at a particular point then use the break statement. Give a condition in for loop and if the condition get matched then the loop will break.
Example:1In the example, the num variable has a list. List have numbers 1-4. You want to print num variable value by using for loop. So you are taking value one by one from num in x. But you want that, if x has value 3 then the loop will stop. So when x gets values 1, 2 it will print the values but when x got value 3 it prints 3 and then the break function runs and the loop is stopped. So you got outputs 1, 2, and 3.
Example:2
While using the for loop if we want that the loop will do its work but when it gets a particular value, it
will ignore that value but the loop will continue. It means, if there is any work after that point it will do
it. In this type of case, we will use continue.
For example, if we want to print numbers 1-5 but don't want to print 3 then we will give the condition if
variable==3 then continue. It means don't print it(3). So we will get the output 1,2,4,5. Look one thing,
there are two numbers 4 and 5 after 3. It means that continue doesn't break the loop it just ignores the value
or work that is given in the condition.
In the range function, we give a range to get numbers. For example, if we want to get numbers 0-10 then we will write 11 in the range function and it will generate numbers 0-10. The range function starts generating numbers from 0(default). So if you write 10 it means 10 numbers. So range function will give numbers from 0-9. So if we want 10 then we have to write 11 in the bracket.
Example:1You can also pass the staring point as like ending point in the range function.
Example:2In example 2 we gave starting number 2 and ending number 6. It means the range function will generate numbers from 2-5. So we get results 2,3,4,5
You can also give an increment sequence in the end.
Example:3In example 3 you gave a starting number 2 and ending number 12 and the increment of number is 3. Because the increment number is 3, it means the difference between these two numbers is 3. It means the range function will generate numbers from 2-12 and the number increment is 3 . So you got results 2,5,8,11.
for loop cannot be empty, but if we have for loop with no content, then put the pass statement to avoid getting an error.
Example:
We can use for loop inside a for loop and it's called nested for loop.
Working process is, at first main for loop or outer loop will run 1 time then the inner loop will run
completely. Then the main loop or outer loop will run again and then the inside or inner loop will run
completely again. This process will continue until the main loop ends.
Here the main loop is x and the inner loop is y and in num and content variable both have 3 values. So this loop will run three times. So at first, the main loop or outer will run once and the inner loop will three times. After the inner loop is completed its maximum run then again the main loop or outer will run once then again inner loop will run three times. Then again main loop and then inner loop. This way nested loop works.
Example:2By else statement you can run a block of code one time when the condition become false or not true.
Example:8