parcticalpython[11]
parcticalpython[11]
Practical File
Of
Problem Solving using
Python Programming
24CSE0101
Submitted
of
BACHELEOR OF ENGINEERING
in
CHITKARA UNIVERSITY
December, 2024
List of Practicals
Sr. Practical Name Page Teacher
No Number Signature
1. a) Write a Python Program to Calculate the Area of a 5,6
Triangle
b) Write a Python Program to Swap Two Variables
c) Write a Python Program to Convert Celsius to 7,8
Fahrenheit
2. a.) Write a Python Program to Check if a Number is Odd or 9,10
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 11,12
3. a.) Write a Python program to check if a given number is 13,14
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 15,16
range.
4. a.) Write a Python Program to Print Pascal Triangle 17,18
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 19,20
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 19,20
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 21,22
using built-in function
max (). Your program should ask the user to input values in
list from keyboard.
Problem Solving using Python Programming (23CS001) Page 2
Problem Solving using Python Programming (23CS001)
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 23,24
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 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 25,26
String.
base = 10
height = 5
result = 0.5*base*height
print("Area of the triangle:", result)
Output 1: a)
a=5
b = 10
a, b = b, a
print(“Swapped variables:”, a,b)
Output 1: b)
celsius = 30
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
Output 1: c)
10 is even
11 is odd
Output 2: b)
digits = str(number)
num_digits = len(digits)
if is_armstrong(153):
else:
Output 2: c)
Problem Solving using Python Programming (23CS001) Page 7
Problem Solving using Python Programming (23CS001)
# Example usage
num = 5
result = is_fibonacci(num)
print("Is Fibonacci number?", result)
# Example usage
n=3
result = cube_sum(n)
print("Cube sum of first", n, "natural numbers:", result)
Output 3: a)
Output 3: b)
# Example usage
start = 1
end = 10
Output 3: c)
# Example usage
n = int(input(“Enter number of rows:”))
result = pascal_triangle(n)
for row in result:
print(row)
# Example usage
n = int(input('Enter n: '))
draw_pattern(n)
Output 4: a)
Output 4: b)
# Example usage
user_input = input("Enter numbers separated by spaces: ")
input_list = list(map(int, user_input.split()))
reversed_list = reverse_list(input_list)
print("Reversed list:", reversed_list)
Output 5:
Output 6: a)
# Example usage
user_input = input("Enter numbers separated by spaces: ")
input_list = list(map(int, user_input.split()))
largest_number = find_largest(input_list)
print("Largest number:", largest_number)
def sum_of_rows(matrix):
return [sum(row) for row in matrix]
# Example usage
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
row_sums = sum_of_rows(matrix)
print("Sum of each row:", row_sums)
Output 6: b)
Output 7:
# Example usage
input_string = input("Enter a string: ")
result = count_character_types(input_string)
print("Uppercase letters:", result[0])
print("Lowercase letters:", result[1])
print("Digits:", result[2])
print("Whitespace characters:", result[3])
# Example usage
string1 = "hello"
string2 = "world"
result = common_characters(string1, string2)
print("Common characters:", result)
Output 8: a)
Output 8: b)
# Example usage
input_string = "Hello World"
result = count_vowels(input_string)
print("Number of vowels:", result)
Output 8: c)
# Example usage
tuple_of_tuples = (('Red', 'White', 'Blue'), ('Green', 'Pink', 'Purple'), ('Orange', 'Yellow',
'Lime'))
result = check_in_tuples(tuple_of_tuples, "White")
print("Is the element present?", result)
# Example usage
list_of_tuples = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
result = remove_empty_tuples(list_of_tuples)
print("List after removing empty tuples:", result)
Output 9: a)
Output 9: b)
# Example usage
sample_dict = {‘Manjeet’: [1, 1, 1], ‘Akash’: [1, 1, 1]}
result = remove_duplicates(sample_dict)
print("Dictionary after removing duplicates:", result)
Output 10:
Output 11: a)
# Example usage
input_list = [1, 1, 1, 5, 5, 3, 1, 3, 3, 1,4, 4, 4, 2, 2, 2, 2]
result = count_frequencies(input_list)
print("Frequencies:", result)
# Example usage
# Assuming 'input.txt' contains the text
file_path = 'input.txt'
result = capitalize_file_words(file_path)
print("Capitalized content:", result)
Output 11: b)
# Example usage
# Assuming 'input.txt' contains multiple lines
file_path = 'input.txt'
print_file_reverse(file_path)
# Example usage
num1 = 10
num2 = 0
result = divide_numbers(num1, num2)
print("Result:", result)
Output 13:
def display_elements(self):
return self.elements
# Example usage
manager = ListManager()
manager.append_element(1)
manager.append_element(2)
manager.delete_element(1)
print("Current list:", manager.display_elements())
Output 14:
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * (self.radius ** 2)
def perimeter(self):
return 2 * math.pi * self.radius
# Example usage
circle = Circle(5)
print("Area of the circle:", circle.area())
print("Perimeter of the circle:", circle.perimeter())
Output 15:
class CounterApp:
def __init__(self, root):
self.root = root
self.root.title("Simple Counter App")
# Increase button
self.increase_button = tk.Button(root, text="Increase", command=self.increase, font=("Arial", 14))
self.increase_button.pack(side=tk.LEFT, padx=20)
# Decrease button
self.decrease_button = tk.Button(root, text="Decrease", command=self.decrease, font=("Arial", 14))
self.decrease_button.pack(side=tk.LEFT, padx=20)
# Reset button
self.reset_button = tk.Button(root, text="Reset", command=self.reset, font=("Arial", 14))
self.reset_button.pack(side=tk.LEFT, padx=20)
def increase(self):
self.counter += 1
self.update_counter()
def decrease(self):
self.counter -= 1
self.update_counter()
def reset(self):
self.counter = 0
self.update_counter()
def update_counter(self):
self.counter_label.config(text=str(self.counter))
Output 16: