Basic Python Programs
1.Check Armstrong Number
num=int(input("Enter the number:"))
num_str = str(num)
num_digits = len(num_str)
total = 0
for digit in num_str:
power = int(digit) ** num_digits
total = total + power
if total == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
OUTPUT
2.Find area of a circle
r=int(input("Enter the radius of circle:"))
area=3.14*r*r
print("Area of circle:",area)
OUTPUT
3. Print all Prime numbers in an Interval
lower=int(input("Enter the lower range:"))
upper=int(input("Enter the upper range"))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower,upper+1):
if num>1:
for i in range(2,num):
if num%i==0:
break
else:
print(num)
OUTPUT
4. Check whether a number is Prime or not
num=int(input("Enter the number:"))
if num>1:
for i in range(2,num):
if n%i==0:
print(num," is not prime number")
break
else:
print(num,"is a prime number")
break
else:
print(num,"is not a prime number")
OUTPUT
5.N-th Fibonacci number
n1=0
n2=1
n=int(input("Enter the number:"))
print("Fibonacci series:",n1,n2,end=' ')
for i in range(2,n):
n3=n1+n2
n1=n2
n2=n3
print(n3,end=' ')
print('')
OUTPUT
Enter the number: 6
Fibonacci series: 0 1 1 2 3 5
6. Print ASCII Value of a character
char = input("Enter the character:")
ascii_value = ord(char)
print("The ASCII value of",char,"is",ascii_value)
OUTPUT
Enter the character: A
The ASCII value of A is 65
Array Programs
7.Find sum of array
array=[10,15,20,25,30]
total=sum(array)
print("Sum of array:",total)
OUTPUT
Sum of array: 100
8.Find largest element in an array
array=[10,45,20,25,30]
largest=max(array)
print("Largest element in the array:",largest)
OUTPUT
Largest element in the array: 45
9.Program for array rotation
array=[10,15,20,25,30]
n=int(input("Enter no. of positions to rotate"))
rotated=array[n:]+array[:n]
print("Array after left rotation by", n, "positions:", rotated)
OUTPUT
Enter no. of positions to rotate 2
Array after left rotation by 2 positions: [20, 25, 30, 10, 15]
List Programs
10.Swap two elements in a list
array=[10,15,20,25,40,23]
array[0],array[3]=array[3],array[0]
print("Array after swapping is:",array)
OUTPUT
Array after swapping is: [25, 15, 20, 10, 40, 23]
11.Ways to find length of list
array=[10,15,20,25,40,23]
n=len(array)
print("Length of the given array is",n)
OUTPUT
Length of the given array is 6
12.Ways to check if element exists in list
n=int(input("Enter the number:"))
array=[10,15,20,25,40,23]
if n in array:
print("The number exists in the list.")
else:
print("The number does not exist in the list.")
OUTPUT
Enter the number: 3
The number does not exist in the list.
13.Different ways to clear a list
14.Reversing a List
15.Find sum of elements in list
16.Find smallest number in a list
Matrix Programs
17.Adding and Subtracting Matrices
A = [[1, 2],
[3, 4]]
B = [[5, 6],
[7, 8]]
add = [[0, 0],
[0, 0]]
sub = [[0, 0],
[0, 0]]
for i in range(len(A)):
for j in range(len(A[0])):
add[i][j] = A[i][j] + B[i][j]
sub[i][j] = A[i][j] - B[i][j]
print("Matrix Addition:")
for row in add:
print(row)
print("\nMatrix Subtraction:")
for row in sub:
print(row)
OUTPUT
String Programs
19.Check whether the string is Symmetrical or Palindrome
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not a Palindrome")
mid = len(s) // 2
if len(s) % 2 == 0 and s[:mid] == s[mid:]:
print("Symmetrical")
else:
print("Not Symmetrical")
OUTPUT
20.Reverse words in a given String
s = input("Enter a sentence: ")
words = s.split()
reversed_words = ' '.join(reversed(words))
print("Reversed:", reversed_words)
OUTPUT
22.Check if a Substring is Present in a Given String