Python Programs 15-24
Python Programs 15-24
Programs:
Output:
0 21
1 44
2 35
3 11
Program:
num = 1234
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
Output:
4321
Program:
# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'
%(celsius,fahrenheit))
Output:
Program:
import random
output:
31
Program:
Output:
Enter number: 5
5 is odd
Program:
output:
Program:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output:
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
Program:
# set union
print("Union of E and N is",E | N)
# set intersection
print("Intersection of E and N is",E & N)
# set difference
print("Difference of E and N is",E - N)
Output:
Program:
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print()
Output:
* *
* * *
* * * *
* * * * *
Program:
for i in range(rows):
for j in range(i+1):
print(j+1, end=" ")
print()
Output:
1 2
1 2 3
1 2 3 4
1 2 3 4 5