0% found this document useful (0 votes)
3 views

Python Programes , 2310990785

This document is a practical file for the course 'Problem Solving using Python Programming' at Chitkara University, submitted by a student named Vivek. It includes a detailed index of various Python programming exercises and solutions, covering topics such as area calculations, number checks, string manipulations, and data structures. The document serves as a comprehensive guide for practical implementations of Python programming concepts.

Uploaded by

jaatmafia121106
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Programes , 2310990785

This document is a practical file for the course 'Problem Solving using Python Programming' at Chitkara University, submitted by a student named Vivek. It includes a detailed index of various Python programming exercises and solutions, covering topics such as area calculations, number checks, string manipulations, and data structures. The document serves as a comprehensive guide for practical implementations of Python programming concepts.

Uploaded by

jaatmafia121106
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

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: Neha Sharma Name: Vivek
Designation: Roll no:- 2310991163

Problem Solving using Python Programming (23CS001) 1 Page number:-


Problem Solving using Python Programming (23CS001)

Chitkara University,Punjab Sem, Batch


INDEX
Sr.n Practical Name Teacher Sign
o
1. a) Write a Python Program to Calculate the Area of a 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 0
c.) Write a Python Program to Check 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 program to print all odd numbers in a range.
4. a.) Write a Python Program to Print Pascal Triangle
Hint: Enter number of rows: 4
1
1 1
1 2 1
1 3 3 1
b.) WAP to Draw the following Pattern for n number:
11111
2222
333
44
5
5. 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”
6. 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.

Problem Solving using Python Programming (23CS001) 2 Page number:-


Problem Solving using Python Programming (23CS001)

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

8. a) Write a program that reads a string from keyboard and display:


* The number of uppercase letters in the string.
* The number of lowercase letters in the string.
* The number of digits in the string.
* The number of white space 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!
True
Check if Olive present in said tuple of tuples!
False
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. a) Write a Program in Python to Find the Differences Between Two
Lists Using Sets.
11. a) Write a Python program Remove duplicate values across Dictionary
Values.
Input : test_dict = {‘Manjeet’: [1], ‘Akash’: [1, 8, 9]}
Output : {‘Manjeet’: [], ‘Akash’: [8, 9]}
Input : test_dict = {‘Manjeet’: [1, 1, 1], ‘Akash’: [1, 1, 1]}
Output : {‘Manjeet’: [], ‘Akash’: []}
b) Write a Python program to Count the frequencies in a list using
dictionary in Python.
Input : [1, 1, 1, 5, 5, 3, 1, 3, 3, 1,4, 4, 4, 2, 2, 2, 2]
Output :
1:5
2:4

Problem Solving using Python Programming (23CS001) 3 Page number:-


Problem Solving using Python Programming (23CS001)

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.

16. Create an interactive application using Python's Tkinter library for


graphics programming.

Problem Solving using Python Programming (23CS001) 4 Page number:-


Problem Solving using Python Programming (23CS001)

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

Length of base :-> 5

Output:

B)
Code :-

a=int(input())
b=int(input())
t=a
a=b
b=t
print(a)
print(b)

Problem Solving using Python Programming (23CS001) 5 Page number:-


Problem Solving using Python Programming (23CS001)

Sample Input:
3
4

Output:

C)
Code :-

celsius=float(input("enter temperature in celcius: "))


fahrenheit=(celsius*(9/5))+32
print (" Converted temperature ",fahrenheit,"f")

Sample Input:
100

Output:

Problem Solving using Python Programming (23CS001) 6 Page number:-


Problem Solving using Python Programming (23CS001)

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)

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 program to print all odd numbers in a range.

Solution:
A)
Code:-

num = int(input("Enter a number: "))


a, b = 0, 1
while a < num:
a, b = b, a + b

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:-

Problem Solving using Python Programming (23CS001) 10 Page number:-


Problem Solving using Python Programming (23CS001)

n = int(input("Enter the value of n: "))


cube_sum = 0
for i in range(1, n + 1):
cube_sum += i ** 3
print(f"The cube sum of the first {n} natural numbers is: {cube_sum}")

Sample Input:-
1) 5
2) 3
3) 10

Output will be :-
1)

2)

3)

C)
Code:-

start = int(input("Enter the start of the range: "))


end = int(input("Enter the end of the range: "))
if start % 2 == 0:
start += 1
print("Odd numbers in the range:")
for num in range(start, end + 1, 2):
print(num)

Sample Input:-
1) Enter the start of the range: 5
2) Enter the end of the range: 10

Problem Solving using Python Programming (23CS001) 11 Page number:-


Problem Solving using Python Programming (23CS001)

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)

(Hint:- Enter number of rows: 4


1
1 1
1 2 1
1 3 3 1 )

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 :-

Problem Solving using Python Programming (23CS001) 13 Page number:-


Problem Solving using Python Programming (23CS001)

B)
Code:-

n=int(input("Integer Inputed:- "))


for r in range (1,n+1):
for c in range (n-r+1):
print(r,end="")
print()

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:-

li=[ int(x) for x in input().split()]


n=len(li)
li1=[]
for i in range(1, n+1):
li1.append(li[-i])
print(li1)

Sample Input:-
123789456

Output will be :-

B)
Code:-

li=[ int(x) for x in input().split()]


li.sort()
print("maximum number", li[-1])

Sample Input:-
Problem Solving using Python Programming (23CS001) 16 Page number:-
Problem Solving using Python Programming (23CS001)

77 85 95 42 15 758 654 349 4558

Output will be :-

Problem Solving using Python Programming (23CS001) 17 Page number:-


Problem Solving using Python Programming (23CS001)

Problem Solving using Python Programming (23CS001) 18 Page number:-


Problem Solving using Python Programming (23CS001)

Problem Solving using Python Programming (23CS001) 19 Page number:-


Problem Solving using Python Programming (23CS001)

Problem Solving using Python Programming (23CS001) 20 Page number:-


Problem Solving using Python Programming (23CS001)

Problem Solving using Python Programming (23CS001) 21 Page number:-


Problem Solving using Python Programming (23CS001)

Problem Solving using Python Programming (23CS001) 22 Page number:-


Problem Solving using Python Programming (23CS001)

Problem Solving using Python Programming (23CS001) 23 Page number:-

You might also like