Professional Documents
Culture Documents
Practical File
Of
Problem Solving using Python
Programming
23CS001
Submitted
of
BACHELEOR OF ENGINEERING
in
CHITKARA UNIVERSITY
December, 2023
INDEX
Sr. Practical Name Teacher
No. Sign
1 (a)Write a python program to calculate area of triangle.
(b) Write a python program to swap two variables.
(c) Write a python program to convert Celsius to Fahrenheit.
2 (a)Write a python program to check if a number is odd or even
(b)Write a python program to check if a number is positive, negative or zero.
(c) Write a python program to check if a number is an Armstrong number.
5 Write a python program with a function that accepts a string from keyboard
and create a new string after converting character of each word capitalized.
For instance, if the sentence is “stop and smell the roses” the output shall be
“Stop And Smell The Roses”.
6 (a)Write a program that accepts list from the user. Your program should
reverse the content of list and display it .Do not use reverse () method.
(b)Find and display the largest number of a list without using built-in
function max ().Your program should ask the user to input values in list from
keyboard.
7 Find the sum of each row of matrix of size m x n. For example ,for the
following matrix output will be like this:
2 11 7 12
5 2 9 15
Problem Solving using Python Programming (23CS001)
8 3 10 42\
Sum of row 1=32
Sum of row 2=31
Sum of row 3=63
8 (a)Write a program that reads a string from keyboard and display:
The number of uppercase letters in string
The number of lowercase letters in string
The number of digits in the string
The number of whitespace characters in the string
(b)Python program to find Common characters in two strings.
(c)Python program to count the number of vowels in a string.
9 a) Write a Python program to check if a specified element presents in a tuple
of tuples.
Original list:
((‘Red’ ,’White’ , ‘Blue’),(‘Green’, ’Pink’ , ‘Purple’), (‘Orange’, ‘Yellow’,
‘Lime’))
Check if White present in said tuple of tuples
Check if Olive present in said tuple of tuples
b) Write a Python program to remove an empty tuple(s) from a list of tuples.
Sample data: [(), (), (“,), (‘a’, ‘b’), (‘a’, ‘b’. ‘c’), (‘d’)]
Expected output:[(“,),(‘a’, ‘b’),(‘a’, ‘b’, ‘c’), ‘d’]
10 Write a Program in Python to Find the Differences Between Two Lists using
sets.
Problem Solving using Python Programming (23CS001)
Program 1(A):Write
Write a Python Program to Calculate the Area of a Triangle
Solution:
Output:
a = int(input("Number 1:"))
b =int(input("Number 2:"))
def swap(a,b):
i=a
b=i
print("b is now",b)
def swap1(a,b):
i=b
a=i
print("A is now",a)
print("Orignal value of A :",a, "of b:",b)
swap(a,b)
swap1(a,b)
GARVISH
Problem Solving using Python Programming (23CS001)
Output:
Problem Solving using Python Programming (23CS001)
Solution:
func(a)
Output:
GARVISH
Problem So
olving using Python Programming (23CS001)
Solution:
Output:
Solution:
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Solution:
num=int(input(“Enter a number:”)
Sum=0
nl=len(str(num))
temp=num
while temp>0:
digit=temp%10
sum+=digit**nl
temp //=10
if num==sum:
print(num, “is an Armstrong number”)
else:
print(num, “is not an Armstrong number”)
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Program 3(a): Write a python program to check if a given number is Fibonacci number.
Solution:
Output:
Problem Solving using Python Programming (23CS001)
Program 3(b):Write a python program to print cube sum of first n natural numbers.
Solution:
def sumOfCubes(n):
if n <0:
return
sum=0
foriinrange(n+1):
sum+=pow(i,3)
returnsum
n =int(input('Enter n : '))
sum=sumOfCubes(n)
print(f'Sum : {sum}')
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Solution:
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Solution:
Output:
Program 4(b):(b)Write
(b)Write a python program to draw the following pattern for n numbers:
11111
2222
333
44
5
Solution:
GARVISH
Problem Solving using Python Programming (23CS001)
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Write a python program with a function that accepts a string from keyboard and
Program 5:Write
create a new string after converting character of each word capitalized. For instance, if the
sentence is “stop and smell the roses” the output shall be “Stop And Smell The Rose
Roses”.
Solution:
x=input("Enter a sentence")
z=x.title()
print(z)
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Program 6(a):Write a program that accepts list from the user. Your program should reverse the
content of list and display it .Do not use reverse () method.
Solution:
l1=[]
n=int(input("Enter number of elements:"))
for i in range(0,n):
ele=int(input("Enter element:"))
l1.append(ele)
print(l1)
def reverse(num):
if len(num)==1:
return num
return reverse(num[1:])+num[0:1]
print (reverse(l1))
Output:
Program 6(b):Find and display the largest number of a list without using built
built--in function
max().Your program should ask the user to input values in list from keyboard.
GARVISH
Problem Solving using Python Programming (23CS001)
Solution:
l1=[]
n=int(input("Enter number of elements:"))
for i in range(0,n):
ele=int(input("Enter element:"))
l1.append(ele)
print(l1)
def myMax(list1):
max = list1[0]
for x in list1:
if x > max:
max = x
return max
print(myMax(l1))
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Find the sum of each row of matrix of size m x n. For example , for the following
Program 7:Find
matrix output will be like this:
2 11 7 12
5 2 9 15
8 3 10 42\
Sum of row 1=32
Sum of row 2=31
Sum of row 3=63
Solution:
a=[
[2, 11, 7, 12],
[5, 2, 9, 15],
[8, 3, 10, 42]
]
rows = len(a)
cols = len(a[0])
for i in range(0, rows):
sumRow = 0
for j in range(0, cols):
sumRow = sumRow + a[i][j]
print("Sum of " + str(i+1) +" row:: " + str(sumRow))
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Program 8: (a)Write a program that reads a string from keyboard and display:
The number of uppercase letters in string
The number of lowercase letters in string
The number of digits in the string
The number of whitespace characters in the string
Solution:
string=input("Enter string:")
count1=0
count2=0
for i in string:
if(i.islower()):
count1=count1+1
elif(i.isupper()):
count2=count2+1
#Number of lowercase letters
alpha=0
for i in string:
if (i.isalpha()):
alpha+=1
print("Number of Digits:", len(string)-alpha)
alpha)
GARVISH
Problem Solving using Python Programming (23CS001)
Output:
Problem Solving using Python Programming (23CS001)
Solution:
return count
if __name__ == "__main__" :
s1 = "weirdgeeks"
s2 = "higeeks"
n1 = len(s1)
n2 = len(s2)
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Solution:
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Solution:
colors = (
('Red', 'White', 'Blue'),
('Green', 'Pink', 'Purple'),
('Orange', 'Yellow', 'Lime'),
)
print("Original list:")
print(colors)
c1 = 'White'
print("\nCheck
nCheck if",c1,"presenet in said tuple of tuples!")
print(check_in_tuples(colors, c1))
c1 = 'White'
nCheck if",c1,"presenet in said tuple of tuples!")
print("\nCheck
print(check_in_tuples(colors, c1))
c1 = 'Olive'
print("\nCheck
nCheck if",c1,"presenet in said tuple of tuples!")
print(check_in_tuples(colors, c1))
Output:
GARVISH
Problem Solving using Python Programming (23CS001)
Sample data: [(), (), (“,), (‘a’, ‘b’), (‘a’, ‘b’. ‘c’), (‘d’)]
Expected output:[(“,),(‘a’, ‘b’),(‘a’, ‘b’, ‘c’), ‘d’]
Solution:
def Remove(tuples):
tuples = [t for t in tuples if t]
return tuples
tuples = [(),(),("",),('a','b'),('a','b','c'),('d')]
print(Remove(tuples))
Output:
]
GARVISH
Problem Solving using Python Programming (23CS001)
Solution:
list1 = [1, 3, 5, 7, 9]
list2=[1, 2, 4, 6, 7, 8]
Output:
GARVISH