Python Programes , 2310990785
Python Programes , 2310990785
Practical File
Of
Problem Solving using
Python Programming
23CS001
Submitted
of
BACHELEOR OF ENGINEERING
in
CHITKARA UNIVERSITY
December, 2023
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:
Sum of row 1 = 32
Sum of row 2 = 31
Sum of row 3 = 63
3:3
4:3
5:2
Explanation : Here 1 occurs 5 times, 2 occurs 4 times and so
on...
12. a) Write a Python Program to Capitalize First Letter of Each Word in a
File.
b.) Write a Python Program to Print the Contents of File in Reverse
Order.
13. Write-a-program:-*to catch an exception and handle it using try and
except code blocks.
14. Write a Python Program to Append, Delete and Display Elements of a
List using Classes.
15. Write a Python Program to Find the Area and Perimeter of the Circle
using Class.
Program 1:
A)
Title of program: Write a Python Program to Calculate the Area of a Triangle.
B)
Title of program: Write a Python Program to Swap Two Variables.
C)
Title of program: Write a Python Program to Convert Celsius to Fahrenheit.
Solution:
A)
Code :-
a=float(input())
b=float(input())
area = (1/2)*a*b
cm_squared = "cm" + "²"
print("Area of triangle is :",area , cm_squared)
Sample Input:
length of height:-> 5
Output:
B)
Code :-
a=int(input())
b=int(input())
t=a
a=b
b=t
print(a)
print(b)
Sample Input:
3
4
Output:
C)
Code :-
Sample Input:
100
Output:
Program 2:
A)
Title of program: Write a Python Program to Check if a Number is odd or Even.
B)
Title of program: Write a Python Program to Check if a Number is Positive, Negative or 0.
C)
Title of program: Write a Python Program to Check Armstrong Number.
Solution:
A)
Code:-
n=int(input())
if(n%2==0):
print("The number is Even")
else:
print("The number is Odd")
Sample Input:
1) 3
2) 2004504
Output will be
1)
2)
B)
Code:-
Problem Solving using Python Programming (23CS001) 7 Page number:-
Problem Solving using Python Programming (23CS001)
n=int(input())
if(n>0):
print("Positive")
elif (n<0):
print("Negative")
else:
print("Zero")
Sample Input:
1) 5
2) -5
3) 0
Output will be :-
1)
2)
3)
C)
Problem Solving using Python Programming (23CS001) 8 Page number:-
Problem Solving using Python Programming (23CS001)
Code:-
n=input()
def armstrong(n):
list1=list(n)
m=len(list1)
list2=[]
sum=0
for i in range(m):
a=int(list1[i])**m
sum+=a
list2=list(str(sum))
if list1 == list2:
return "true"
else:
return "false"
print(armstrong(n))
Sample Input:-
1) 125
2) 153
Output will be :-
1)
2)
Program 3:
A)
Problem Solving using Python Programming (23CS001) 9 Page number:-
Problem Solving using Python Programming (23CS001)
Solution:
A)
Code:-
if a == num:
print(f"{num} is a Fibonacci number.")
else:
print(f"{num} is not a Fibonacci number.")
Sample Input:-
1)34
2)153
Output will be :-
1)
2)
B)
Code:-
Sample Input:-
1) 5
2) 3
3) 10
Output will be :-
1)
2)
3)
C)
Code:-
Sample Input:-
1) Enter the start of the range: 5
2) Enter the end of the range: 10
Output will be :-
Program 4:
A)
Write a Python Program to Print Pascal Triangle.
Problem Solving using Python Programming (23CS001) 12 Page number:-
Problem Solving using Python Programming (23CS001)
B)
Write a Python Program to draw the following pattern for n number:
11111
2222
333
44
5
Solution:
A)
Code:-
def factorial(n):
result=1
for i in range(1,n+1):
result=result*i
return result
n=int(input())
for i in range(n):
for s in range(n-i):
print( end=" ")
for j in range(i):
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")
print("1",end=" ")
print()
Sample Input:-
1) 5
Output will be :-
B)
Code:-
Sample Input:-
1) 5
2) 8
Output will be :-
1)
2)
Program 5:
Problem Solving using Python Programming (23CS001) 14 Page number:-
Problem Solving using Python Programming (23CS001)
Write a 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 should be {“Stop And Smell The
Roses”}
Solution:
Code:-
def capitalize_words(sentence):
words = sentence.split()
capitalized_words = [word.capitalize() for word in words]
return " ".join(capitalized_words)
input_sentence = input("Enter a sentence: ")
capitalized_sentence = capitalize_words(input_sentence)
print("Capitalized Sentence: ", capitalized_sentence)
Sample Input:-
“stop and smell the roses.”
Output will be :-
Program 6:
Problem Solving using Python Programming (23CS001) 15 Page number:-
Problem Solving using Python Programming (23CS001)
A)
Write a program that accepts a list from 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.
Solution:
A)
Code:-
Sample Input:-
123789456
Output will be :-
B)
Code:-
Sample Input:-
Problem Solving using Python Programming (23CS001) 16 Page number:-
Problem Solving using Python Programming (23CS001)
Output will be :-