Problem Solving using Python Programming (23CS001)
Practical File
Of
Problem Solving using Python
Programming
23CS001
Submitted
in partial fulfillment for the award of the degree
of
BACHELEOR OF ENGINEERING
in
COMPUTER SCIENCE & ENGINEERING
CHITKARA UNIVERSITY
CHANDIGARH-PATIALA NATIONAL HIGHWAY
RAJPURA (PATIALA) PUNJAB-140401 (INDIA)
December, 2023
Submitted To: Submitted By:
Faculty name: Dr. Chanderprabha Name:Garvish
Designation: Mentor Roll No:2310990257
Chitkara University, Punjab Sem-I, Batch-2023G4
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
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.
3 (a)Write a python program to check if a given number is Fibonacci number.
(b)Write a python program to print cube sum of first n natural numbers.
(c)Write a python to print all odd numbers in a range.
4 (a)Write a python program to print Pascal triangle.
1
1 1
1 2 1
1 3 3 1
(b)Write a python program to draw the following pattern for n numbers:
11111
2222
333
44
5
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
[Type here]
GARVISH 2310990257
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.
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 1(A):Write
Write a Python Program to Calculate the Area of a Triangle
Solution:
b=int (input ("Enter length of base :"))
h=int (input ("Enter height of triangle :"))
Area= (1/2)*b*h
print (“Area of triangle:” Area)
Output:
Program 1(B): Write a Python Program to Swap Two Variables
Solution:
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)
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 1(c):Write a Python Program to Convert Celsius to Fahrenheit
Solution:
a=int(input("Enter temp in celcius:"))
:"))
def func(a):
c=a*(5/9)-32
print("Farenheit:",c)
func(a)
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 2(a): Write a python program to check if a number is odd or even
Solution:
a=int(input("Enter a number to be checked:"))
if a%2==0:
print("Number is even")
else:
print("Number is odd")
Output:
Program 2(b):Write a python program to check if a number is positive, negative or zero.
Solution:
num=float(input(“Enter the number to be checked”))
if num>0:
print(“Number is positive”)
elif num==0:
print(“Number is zero”)
else:
print(“Number is negative”)
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 2(c):Write a python program to check if a number is an Armstrong number.
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:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 3(a): Write a python program to check if a given number is Fibonacci number.
Solution:
n=int(input(“Enter the number you want to check”))
f3=0
f1=1
f2=1
if (n==0 or n==1):
print(“Number is Fibonacci number”)
else:
while f3<n:
f3=f1+f2
f2=f1
f1=f3
if f3==n:
print(“Given number is a fibonacci number”)
else:
print(“No it’s not a Fibonacci number”)
Output:
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:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 3(c):Write a python to print all odd numbers in a range.
Solution:
ll=int(input("Enter lower limit "))
ul=int(input("Enter upper limit "))
print("odd numbers in the range are")
for i in range(ll,ul):
if i%2!=0:
print(i,end=" ")
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 4(a):(a)Write
(a)Write a python program to print Pascal triangle.
1
21
12 1
1 3 3 1
Solution:
num = int(input("enter the number of rows"))
for i in range(1, num+1):
for j in range(0, num-i+1):
print(' ', end='')
C=1
for j in range(1, i+1):
print(' ', C, sep='', end='')
C = C * (i - j) // j
print()
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:
Num=int(input("enter the number of rows":))
for i in range(1, Num):
for j in range(i, Num):
print(i, end="")
print()
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 5:Write
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 Rose
Roses”.
Solution:
x=input("Enter a sentence")
z=x.title()
print(z)
Output:
[Type here]
GARVISH 2310990257
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.
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))
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 7:Find
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
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:
[Type here]
GARVISH 2310990257
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
print("The number of lowercase characters :",count1)
print(count1)
#Number of uppercase letters
print("The number of uppercase characters :",count2)
#Number of digits in the string
alpha=0
for i in string:
if (i.isalpha()):
alpha+=1
print("Number of Digits:", len(string)-alpha)
alpha)
#Number of whitespace characters in a string
count=0
for a in string:
if (a.isspace()) == True:
count+=1
print("The number of white space characters in a string:",count)
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 8(b):Python program to find Common characters in two strings.
Solution:
def countPairs(s1, n1, s2, n2) :
freq1 = [0] * 26
freq2 = [0] * 26
count = 0
for i in range(n1) :
freq1[ord(s1[i]) - ord('a')] += 1
for i in range(n2) :
freq2[ord(s2[i]) - ord('a')] += 1
for i in range(26) :
count += min(freq1[i], freq2[i])
return count
if __name__ == "__main__" :
s1 = "weirdgeeks"
s2 = "higeeks"
n1 = len(s1)
n2 = len(s2)
print("Number of Common alphabets in the string are",countPairs(s1, n1, s2, n2))
Output:
Program 8(c):Python program to count the number of vowels in a string.
Solution:
String = input('Enter the string :')
count = 0
String = String.lower()
for i in String:
if i == 'a' or i == 'e' or i == 'i'
i' or i == 'o' or i == 'u':
count+=1
if count == 0:
print('No vowels found')
else:
print('Total vowels are :' + str(count))
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Output:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 9(a):Write
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’))
Lime’))
Check if White present in said tuple of tuples
Check if Olive present in said tuple of tuples
Solution:
def check_in_tuples(colors, c):
result = any(c in tu for tu in colors)
return result
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'
print("\nCheck
nCheck if",c1,"presenet in said tuple of tuples!")
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:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 9(b):Write
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’]
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:
[Type here]
GARVISH 2310990257
Problem Solving using Python Programming (23CS001)
Program 10:Write a Program in Python to Find the Differences
Differences Between Two Lists using ssets.
Solution:
list1 = [1, 3, 5, 7, 9]
list2=[1, 2, 4, 6, 7, 8]
diff_list1_list2 = list(set(list1) - set(list2))
diff_list2_list1 = list(set(list2) - set(list1))
total_diff = diff_list1_list2 + diff_list2_list1
list2_list1
print("The elements they differ by are:",total_diff)
Output:
[Type here]
GARVISH 2310990257