Python File
Python File
(24CSE0101)
Practical File
Of
Problem Solving using
Python Programming
24CSE0101
Submitted
degree 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-7
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 8-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
3. a.) Write a Python program to check if a given number is 11-13
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 14-16
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 16
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 17-18
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 (24CSE0101) Page 2
Problem Solving using Python Programming
(24CSE0101)
7. Find the sum of each row of matrix of size m x n. For 19
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 20-22
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
String.
# Example usage
base = 10
height = 5
result = area_of_triangle(base, height)
print("Area of the triangle:", result)
Output 1: a)
# Example usage
x = 15
y = 20
x, y = swap_variables(x, y)
print("Swapped variables:", x, y)
Output 1: b)
#Example usage
temp_celsius=37
temp_fahrenheit=celsius_to_fahrenheit(temp_
celsius)
print("Temperature in Fahrenheit:",
temp_fahrenheit)
Output 1: c)
def check_odd_even(number):
return "Even" if number % 2 == 0 else "Odd"
# Example usage
num = 5
result = check_odd_even(num)
print("The number is:", result)
Output 2: a)
# Example usage
number = -8
result = check_number(number)
print("The number is:", result)
Output 2: b)
order = len(str(num))
# Example usage
number = 153
result = is_armstrong(number)
Output 2: c)
# Example usage
num = 55
result = is_fibonacci(num)
print("Is Fibonacci number?", result)
Output 3: a)
# Example usage
n=3
result = cube_sum(n)
print("Cube sum of first", n, "natural numbers:", result)
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)
Output 4: a)
# Example usage
n = int(input('Enter n: '))
draw_pattern(n)
Output 4: b)
Output 5:
# 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 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)
Output 6: b)
# Example
usage matrix
=[
[11, 22, 33],
[41, 54, 67],
[75, 38, 19]
]
row_sums =
sum_of_rows(matrix)
print("Sum of each row:",
row_sums)
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])
Output 8: a)
# Example usage
string1 = "hello"
string2 = "world"
result = common_characters(string1, string2)
print("Common characters:", result)
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)
Output 9: a)
# 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: b)
Output 10:
# Example usage
sample_dict = {‘Abdul’: [1, 1, 1], ‘Akash’: [2, 2, 2]}
result = remove_duplicates(sample_dict)
print("Dictionary after removing duplicates:", result)
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)
Output 11: b)
# Example usage
# Assuming 'input.txt' contains the text
file_path = 'input.txt'
result = capitalize_file_words(file_path)
print("Capitalized content:", result)
# 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:
# 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: