1. Write a python program to construct the following patterns using nested for loop
*
**
***
****
*****
*****
****
***
**
*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
a. Sol:
rows = int(input("Enter the number of rows: "))
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print(" ")
for i in range(rows + 1, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print(" ")
Output:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
b. Sol
r=int(input("Enter no.of rows:"))
for num in range(r+1):
for i in range(num):
print(num, end=" ") # print number
# line after each row to display pattern correctly
print(" ")
2. Output:
Enter no.of rows:5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
c. Sol
r=int(input("Enter the no.of rows"))
for row in range(1, r+1):
for column in range(1, row + 1):
print(column, end=' ')
print("")
Output:
Enter the no.of rows5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Write a python program to print Multiplication table using format function inside print
Sol:
n=int(input("Enter a number:"))
for i in range(1,11):
print("{}*{}={}".format(n,i,n*i))
Output:
Enter a number:5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
Write a Python Program to Check how many times a given number can be divided by 3
3. before it is less than or equal to 10.
Sol:
n = int(input())
count = 0
while n>=10:
if n%3==0:
count+=1
n//=3
print(count)
Output:
81
2
Calculate the sum of the first 20 items of 1 + 2 + 4 + 8 + 16 +… using while True infinite
loop with break statement.
Sol:
s=0
i=0
while i<=20:
s=s+2**i
i=i+1
print(s)
Output:
2097151
Transform the existing while loops with calculations 0-100, and add continue statements to
calculate only the odd sums.
Sol:
s=0
n=1
while n<=100:
if(n%2!=0):
s=s+n
n=n+1
else:
n=n+1
continue
print("The odd sum is:",s)
Output:
The odd sum is: 2500