15. Python Program to Access Index of a List Using for Loop.
Programs:
my_list = [21, 44, 35, 11]
for index in range(len(my_list)):
value = my_list[index]
print(index, value)
Output:
0 21
1 44
2 35
3 11
16. Python Program to Reverse a Number.
Program:
num = 1234
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: " + str(reversed_num))
Output:
4321
17. Python Program to Convert Celsius To Fahrenheit.
Program:
# Python Program to convert temperature in celsius to fahrenheit
# change this value for a different result
celsius = 37.5
# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'
%(celsius,fahrenheit))
Output:
37.5 degree Celsius is equal to 99.5 degree Fahrenheit
18. Python Program to Generate a Random Number.
Program:
import random
my_list = [1, 'a', 32, 'c', 'd', 31]
print([Link](my_list))
output:
31
19. Python Program to Check if a Number is Odd or Even.
Program:
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Output:
Enter number: 5
5 is odd
20. Python Program to Print the Fibonacci sequence.
Program:
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
output:
How many terms? 7
Fibonacci sequence:
0
1
1
2
3
5
8
21. Python Program to Add Two Matrices.
Program:
# Program to add two matrices using nested loop
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]
22. Python Program to Illustrate Different Set Operations.
Program:
# Program to perform different set operations like in mathematics
# define three sets
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# 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)
# set symmetric difference
print("Symmetric difference of E and N is",E ^ N)
Output:
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {8, 0, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}
23. Python Program to Create Pyramid Patterns.
Program:
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print()
Output:
* *
* * *
* * * *
* * * * *
Program:
rows = int(input("Enter number of rows: "))
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