If some one ask you to print numbers from 0 to 5, you can print,
print(0) print(1) print(2) print(3) print(4) print(5)
its easy but what if some one ask you to do it 100 times or 1000 times or more. Then printing the line one by one is not a good way to solve this problem. You need some solution which can perform one task repeatedly as long as its required.
Python has very famous solution for this problem called Loops. Loops can help you to perform one task as long as you want, it can be for 5 times or for 500 times or more. Loops are very efficient in this.
There are two different types of loop available in Python.
1. While Loop
2. For Loop
While loop is useful when you want to perform one task as long as condition is true. Once the condition is false loop will be stopped. What does it mean, lets discuss in detail.
As you know that if statement works with condition. If student_marks are less than 85 then it will print student marks on your screen.
student_marks = 80 if (student_marks < 85): print( "Student marks", student_marks)
While loop also works in same fashion but with repetition. You can do some minor changes in previous code. Replace if with while, and your while loop is ready for work.
student_marks = 80 while (student_marks < 85): print( "Student marks", student_marks)
While loop will check if student_marks are less than 85, it will go inside the loop and execute the code block, on next iteration it will go back at the start of while loop and see if student_marks are still less than 85 if yes, it will go inside the loop again and it will execute the code. While loop will keep printing as long as the condition is true. In this case condition will remain true because student_marks variable is not changing. If you want to stop this loop at some point, start incrementing student_marks with 1 on every iteration, so the loop will stop when student_marks will be 85.
student_marks = 80 while (student_marks < 85): print( "Student marks", student_marks) student_marks = student_marks + 1 ## you should see this output Student marks 80 Student marks 81 Student marks 82 Student marks 83 Student marks 84
You can break while loop anytime with break statement. See following example:
while (student_marks < 85): print( "Student marks", student_marks) if(student_marks == 82): break student_marks = student_marks + 1
Keep printing Student marks but break this loop when student_marks is equal to 82.
While loop called while loop because it keeps working while the condition is true. Some times you want to execute a loop for some specific range or number. While loop does not provide this functionality but you can do that with for loop with provided list or range.
for variable in list: code block to execute
Check the following example:
for i in 'PYTHON': print ('Characters :', i)
For loop required keyword for, a variable i with in to store the value and list or range “PYTHON” to iterate on. For loop takes one item each time from list, executes the code block, goes back to the list, takes next item from the list and executes the code block. This process continuos till the end of the list, following is the output of your loop:
## OUTPUT Characters : P Characters : Y Characters : T Characters : H Characters : O Characters : N
For Loop with range
Check another example of for loop, as you know that for loop works with range also. See this example, you can provide a variable number and range (how many times a for loop should execute).
for variable in range: code block to execute
If you want to check what is the total count of first 5 digits, create a variable total and assign a value 0, provide a range to for loop and add total in code block, you can also print total and number on each iteration.
total = 0 for number in range(6): total = total + number print("number", number,"total", total) print("Total", total)
and the output is
## OUTPUT number 0 total 0 number 1 total 1 number 2 total 3 number 3 total 6 number 4 total 10 number 5 total 15
One thing which you have noticed is, range function with one argument (range(6)) starts from 0 not from 1, so if you want to get a count of first five numbers, provide range(6) because first number is 0 and loop will execute 6 times (0,1,2,3,4,5).
range function provides different options to work with range, you can provide the starting point of your choice, you can pass two arguments to range(1,6) function, range will start from 1 and will finish on 6 but 6 is not included. You can get the same result with following example:
for number in range(1,6): total = total + number print("number", number,"total", total) print("Total", total) # Try following range functions range(0,20,2) ## range from 0 to 20 with interval of 2 range(20,-1,-2) ## reverse iteration. range of 20 digits start from 20 to 0 with interval of 2
For Loop with list
For loop works with list as well. Define a list of numbers or strings and pass it to for loop, check this example:
mylist = [4, 5, 9, 10, 15, 16] for i in mylist: print ("Value of my list" , i)
and output is:
Value of my list 4 Value of my list 5 Value of my list 9 Value of my list 10 Value of my list 15 Value of my list 16
Another example with list of strings.
days_of_week = ['Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri'] for day in days_of_week: print("Day in a week" , day)
Day in week Sat Day in week Sun Day in week Mon Day in week Tue Day in week Wed Day in week Thr Day in week Fri
In all previous examples one thing is clear that for loop basically iterates on list, it can be list of integers, float, string or list of characters (which is string). Check the following example: Create a function printAnyList
def printAnyList(mylist): for value in mylist: print(value)
and pass different types of lists to the function.
language = "PYTHON" print("Language") printAnyList(language) daysofweek = ('Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat', 'Sun') print("Days of week") printAnyList(daysofweek) mylist = [2,4,6,8,10] print("My List") printAnyList(mylist) tuple = (1,2,3,4,5,6) print("My tuple") printAnyList(tuple)