Example HTML page

# 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 in : #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

```for x in [1,2,3,4,5,6,7,8,9,10]:
print(x)

```

Output

 1 2 3 4 5 6 7 8 9 10

The following code gives the same Output

```for x in range(0,11):
print(x)

```

Output

 0 1 2 3 4 5 6 7 8 9 10

Program 2: Sum of first 100 numbers

```>>> sum=0
>>> for x in range(101):
sum=sum+x

>>> print(sum)
5050

```

Do you want the sum up to your choice of number? Then read it at runtime

```sum=0
number=int(input("enter the number"))
for x in range(number):
sum=sum+x
print(sum)

```

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.

```sum=0
for x in range(50,61):
sum=sum+x
print(sum)

```

Output

 605

Similarly do the product as shown below

Program 4: Product of the numbers from 1 to 10.

```product=1
for x in range(1,10):
product=product*x
print(product)

```

Output

 362880

Program 5: product of the numbers with in a specific range.

```product=1
for x in range(50,61):
product=product*x
print(product)

```

Output

 13679492361575040000

Program 6: Multiplication Table for a given Number.

```number =10
for x in range(0,11):
print(number,"*",x,"=",number*x)

```

If you want the multiplication for user defined number then read it from the user.

```number = int(input("enter the number for which you want multiplication table"))
for x in range(0,11):
print(number,"*",x,"=",number*x)

```

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.

```>>> number=10
>>> for x in range(11):
print(number*x)

```

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 in : for in : #body goes here

#Program 7

```>>> for i in range(3):
for j in range(4):
print("i=",i,",j=",j,sep=" ")

```

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

```>>> for i in range(3):
print("i=",i, end =": ")
for j in range(1,10):
print(j, end=" ")
print()

```

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

```>>> for i in range(2):
for j in range(11):
print(i,"*",j,"=",i*j)
print()

```

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.

```>>> for x in range(1,11):
for y in range(1,11):
print(x*y, end= "\t")
print()

```

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’

```>>> items=['chicken_starters','biryani','icecream']
>>> for x in items:
print("I ate",x)
else:
print("You ate all the items, go and pay the bill")

```

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.

```value=64
for i in(range(0,5)):
for j in(range(0,i+1)):
ch=chr(value+1)
print(ch,end=" ")
value=value+1
print()

```

Output

 A B C D E F G H I J K L M N O

Program 12: Pattern program of numbers.

```num=int(input("enter the number of rows you want to print:"))
for i in range(1,num+1):
for j in range(1,i+1):
print(i,end=" ")
print()

```

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)

```str=input("enter the string:")
for i in range(1,len(str)):
for j in range(1,i+1):
print(str[i],end=" ")
print()

```

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

```num=int(input("enter the number of rows you want to print:"))
for i in range(1,num+1):
for j in range(1,i+1):
print("*",end=" ")
print()

```

Output

 enter the number of rows you want to print: 5 * * * * * * * * * * * * * * *

Program 15: Pattern program of Reverse right angle triangle.

```num=int(input("enter the number of rows you want to print:"))
for i in range(1,num+1):
for j in range(1,num+2-i):
print("*",end=" ")
print()

```

Output

 enter the number of rows you want to print: 5 * * * * * * * * * * * * * * *

Program 16: Pattern program to print letter ‘E’

```for row in range(7):
for col in range(5):
if (row==0)or(row==3)or(row==6)or(col==0):
print("#",end="")
else:
print(end=" ")
print()

```

Output

 ##### #    #    ##### #    #    #####

Program 17: Pattern program to print letter ‘H’

```for row in range(7):
for col in range(5):
if (col==4)or(col==0)or(row==3):
print("\$",end="")
else:
print(end=" ")
print()

```

Output

 \$       \$ \$       \$ \$\$\$\$\$ \$       \$ \$       \$ \$       \$

Program 18: Pattern program to print letter ‘y’

```for row in range(7):
for col in range(5):
if (row==3)or(col<=0)and(row<=3)or(row==6)or(col==4):
print("*",end="")
else:
print(end=" ")
print()

```

Output

 *       * *       * *       * *****          *          * *****

Program 19: Pattern program to print letter ‘T’

```for row in range(7):
for col in range(5):
if (col==2)or(row==0):
print("\$",end="")
else:
print(end=" ")
print()

```

Output

 \$\$\$\$\$     \$      \$      \$      \$      \$      \$

Program 20: Pattern program to print number ‘6’

```for row in range(7):
for col in range(5):
if (row==0)or(row==3)or(row==6)or(col==0)or(col>3)and(row>3):
print("\$",end="")
else:
print(end=" ")
print()

```

Output

 \$\$\$\$\$ \$    \$    \$\$\$\$\$ \$       \$ \$       \$ \$\$\$\$\$

Program 21: Pattern program to letter ‘P’

```for row in range(7):
for col in range(5):
if (col==0)or(col>3)and(row<3)or(row==0)or(row==3):
print("*",end="")
else:
print(end=" ")
print()

```

Output

 ***** *       * *       * ***** *    *    *

Example HTML page