For loops
The “for” loop in Python is used to iterate over a sequence (list,tuple,string) or other iterable objects. Iterating over a sequence is called traversal.
Syntax:
>>> for <variable> in <sequence>:
#body goes here |
Here, variable takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.
Working of ‘for’ loop
In for loop the condition is checked first. If the condition is false the body of the loop will not be executed and the control moves to next instruction which is immediately after the for loop. If it is true the body of the loop is executed.After the execution of the body the value of initialized variable is incremented and will check the condition.This process is repeated until the condition becomes false, i.e, till the element is not found in sequence.
Program 1: Printing the numbers from 1 to 10
Output
1
2 3 4 5 6 7 8 9 10 |
The following code gives the same Output
Output
0
1 2 3 4 5 6 7 8 9 10 |
Program 2: Sum of first 100 numbers
Do you want the sum up to your choice of number? Then read it at runtime
Output
enter the number 10
45 |
Do you want the sum between a specific range. For example the following shows the sum of the numbers between 50 and 60. (60 is included)
Program 3: Sum of the numbers with in a specific range.
Output
605 |
Similarly do the product as shown below
Program 4: Product of the numbers from 1 to 10.
Output
362880 |
Program 5: product of the numbers with in a specific range.
Output
13679492361575040000 |
Program 6: Multiplication Table for a given Number.
If you want the multiplication for user defined number then read it from the user.
Output
enter the number for which you want multiplication table10
10 * 0 = 0 10 * 1 = 10 10 * 2 = 20 10 * 3 = 30 10 * 4 = 40 10 * 5 = 50 10 * 6 = 60 10 * 7 = 70 10 * 8 = 80 10 * 9 = 90 10 * 10 = 100 |
The way of printing is choice of yours. Let’s look at the two snippets given in interactive shell.
Output
0
10 20 30 40 50 60 70 80 90 100 |
>>> for x in range(11):
print(number*x, end=” “)
0 10 20 30 40 50 60 70 80 90 100 |
Nested Loops:
>>> for <variable> in <sequence>:
for <variable> in <sequence>: #body goes here |
#Program 7
Output
i= 0 ,j= 0
i= 0 ,j= 1 i= 0 ,j= 2 i= 0 ,j= 3 i= 1 ,j= 0 i= 1 ,j= 1 i= 1 ,j= 2 i= 1 ,j= 3 i= 2 ,j= 0 i= 2 ,j= 1 i= 2 ,j= 2 i= 2 ,j= 3 |
# Program 8
Output
i= 0: 1 2 3 4 5 6 7 8 9
i= 1: 1 2 3 4 5 6 7 8 9 i= 2: 1 2 3 4 5 6 7 8 9 |
#Program 9: Multiplication from 0 to 1
Output
0 * 0 = 0
0 * 1 = 0 0 * 2 = 0 0 * 3 = 0 0 * 4 = 0 0 * 5 = 0 0 * 6 = 0 0 * 7 = 0 0 * 8 = 0 0 * 9 = 0 0 * 10 = 0
1 * 0 = 0 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10 |
Another Snippet for getting Multiplication Table from 1 to 10.
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 |
Program 10: for loop with ‘else’
Output
I ate chicken_starters
I ate biryani I ate icecream You ate all the items, go and pay the bill |
Program 11: Pattern program of alphabets.
Output
A
B C D E F G H I J K L M N O |
Program 12: Pattern program of numbers.
Output
enter the number of rows you want to print: 5
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 |
Program 13: Pattern program of string(user-defined)
Output
enter the string: sudhakar
s u u d d d h h h h a a a a a k k k k k k a a a a a a a r r r r r r r r |
Program 14: Pattern program of Right angle triangle
Output
enter the number of rows you want to print: 5
* * * * * * * * * * * * * * * |
Program 15: Pattern program of Reverse right angle triangle.
Output
enter the number of rows you want to print: 5
* * * * * * * * * * * * * * * |
Program 16: Pattern program to print letter ‘E’
Output
#####
# # ##### # # ##### |
Program 17: Pattern program to print letter ‘H’
Output
$ $
$ $ $$$$$ $ $ $ $ $ $ |
Program 18: Pattern program to print letter ‘y’
Output
* *
* * * * ***** * * ***** |
Program 19: Pattern program to print letter ‘T’
Output
$$$$$
$ $ $ $ $ $ |
Program 20: Pattern program to print number ‘6’
Output
$$$$$
$ $ $$$$$ $ $ $ $ $$$$$ |
Program 21: Pattern program to letter ‘P’
Output
*****
* * * * ***** * * * |