Final Python Project File
Final Python Project File
Practical File of
Python Programming
Using Artificial Intelligence
24CAI1101
Submitted
BACHELEOR OF ENGINEERING
in
CHITKARA UNIVERSITY
December, 2024
0|Page
Python Programming for Artificial Intelligence (24CAI1101)
1|Page
Python Programming for Artificial Intelligence (24CAI1101)
10 Program in Python to Find the Differences Between Two Lists Using Sets. 23
11.a
Python program Remove duplicate values across Dictionary Values 24
11.b
Python program to Count the frequencies in a list using dictionary in 25
Python.
13 WAP to catch an exception and handle it using try and except code blocks
28
2|Page
Python Programming for Artificial Intelligence (24CAI1101)
32
15 Python Program to Find the Area and Perimeter of the Circle using Class.
3|Page
Python Programming for Artificial Intelligence (24CAI1101)
Output:
First side of triangle 7
Second side of triangle 5
Third side of triangle 6
Area of triangle is 14.696938456699069
Result:
Output:
enter variable a 4
enter variable b 6
a swapped is 6
4|Page
Python Programming for Artificial Intelligence (24CAI1101)
b swapped is 4
Result:
Output:
enter temperature in Celsius 37 temperature in
Solution:
a=int(input("enter a number")) #takes number input from
user if a%2==0: #checks if number is even print("the
number is even") else :
print("the number is odd")
Output:
enter a number 3
the number is odd
Result:
6|Page
Python Programming for Artificial Intelligence (24CAI1101)
Result:
Output:
Enter a number: 34
34 is not an Armstrong number Result:
7|Page
Python Programming for Artificial Intelligence (24CAI1101)
Objective: Write a Python program to print cube sum of first n natural numbers.
Solution:
a=int(input("Enter a Number")) #takes input from
user b=1 su=0
for i in range(0,a+1): #iterates over each natural number till it reaches inputed
number b=i**3 #assigns the value of cubes to b su+=b #finds the value of sum
and assigns it to su print(su)
Output:
Enter a Number 5
225
Result:
the ending point of range")) #takes ending point input for range
9|Page
Python Programming for Artificial Intelligence (24CAI1101)
for i in range(rows):
# Create a new row with fixed size
row = [0] * (i + 1) row[0] = 1 # First
element is always 1 row[i] = 1 # Last
element is always 1 if i > 1: for j
in range(1, i):
10 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
11 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Solution:
a=input("enter a string")
#takes string input from user
b=a.title()
12 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
c=a[::-1]
#reverses the list and assigns it value to a print(c)
Output:
enter elements of list123457
['7', '5', '4', '3', '2', '1']
Result:
Program 6.b: ……………Largest In List …..………..
13 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Objective: 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=list(map(int,input("enter numbers separated by space").split()))
#takes input from user separated by space
largest=a[0]
#assigns the largest to any value in list
for i in a:
#iterates over list
if i>largest:
#checks if number is larger than largest
largest=i
#assigns the largest number to largest
print(largest)
Output:
enter numbers separated by space23 45 67 56 1 90 56 89 90
Result:
Output:
= RESTART: C:\Users\Lenovo\Desktop\python_file\question7.py
Enter the number of rows (m): 3
Enter the number of columns (n): 4 Enter
the elements of the matrix:
Enter element at position (1, 1): 2
Enter element at position (1, 2): 11
Enter element at position (1, 3): 7
Enter element at position (1, 4): 12
Enter element at position (2, 1): 5
Enter element at position (2, 2): 2
Enter element at position (2, 3): 9
Enter element at position (2, 4): 15
Enter element at position (3, 1): 8
Enter element at position (3, 2): 3
Enter element at position (3, 3): 10
Enter element at position (3, 4): 42
Sum of row 1 = 32
Sum of row 2 = 31
15 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Sum of row 3 = 63
Result:
lo="qwertyuiopasdfghjklzxcvbnm"
#All the small letters are stored in lo
nu="1234567890"
#All the numbers are stored in lu
sp=" "
#All the spaces are stored in sp
for i in a:
if i in up:
#checks how many capital letters in string
upper+=1
#counts how many capital letters in string
elif i in lo:
#checks how many small letters in string
lower+=1
#counts how many small letters in string
elif i in nu:
#checks how many numbers letters in string
space+=1
#counts how many spaces in string
17 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
number of spaces 5
Result:
18 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Solution:
# Original tuple of tuples
tuples = (('Red', 'White', 'Blue'), ('Green', 'Pink', 'Purple'), ('Orange', 'Yellow', 'Lime'))
20 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Solution:
# Input lists list1 =
map(int,input().split())
list2=map(int,input().split())
# Convert lists to
sets set1 = set(list1)
set2 = set(list2)
Solution:
# Input dictionary from the user n =
int(input("Enter the number of keys: "))
test_dict = {}
# Process each key and its list of values for key in test_dict:
new_values = [] # Temporary list to store non-duplicate values
for value in test_dict[key]:
# Add the value only if it hasn't been seen before
if value not in seen_values: new_values +=
22 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
input_list = [1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]
# Count frequencies
for element in input_list: if element in frequency_dict: frequency_dict[element]
+= 1 # Increment count if element exists in the dictionary else:
frequency_dict[element] = 1 # Initialize count to 1 if element is not in the dictionary
try:
24 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
# Open the file in write mode and save the updated content with
open(file_name, 'w') as file: file.write(capitalized_content)
print(f"First letter of each word in '{file_name}' has been capitalized.")
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except Exception as e: print(f"An error
occurred: {e}")
Output:
Enter the file name: testpad.txt
First letter of each word in 'testpad.txt' has been capitalized.
Result:
25 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Objective: Write a Python Program to Print the Contents of File in Reverse Order.
Solution:
# Input the file name
file_name = input("Enter the file name: ")
try:
# Open the file in read mode
with open(file_name, 'r') as file:
# Read all lines of the file lines
= file.readlines()
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.") except
Exception as e:
print(f"An error occurred: {e}")
Output:
Enter the file name: testpad.txt
Contents of the file in reverse order with each line reversed:
.margorP ehT kcehC oT tnemetatS tseT A sI sihT
Result:
26 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Objective:WAP to catch an exception and handle it using try and except code blocks.
Solution:
try:
# Ask user for input and convert it to an integer
num1 = int(input("Enter the first number: ")) num2
= int(input("Enter the second number: ")) #
Perform division result = num1 / num2
print(f"The result of {num1} divided by {num2} is: {result}")
except ZeroDivisionError:
# This block will run if the user tries to divide by zero
print("Error: You cannot divide by zero!")
except ValueError:
# This block will run if the user does not enter a valid integer
print("Error: Invalid input. Please enter valid integers.")
except Exception as e:
# This block will catch any other exception
print(f"An unexpected error occurred: {e}")
Output:
= RESTART: C:\Users\Lenovo\Desktop\python_file\question13.py
Enter the first number: 8
Enter the second number: 6
The result of 8 divided by 6 is: 1.3333333333333333
27 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
28 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
29 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
Solution:
class Circle:
# Constructor to initialize radius
def __init__(self, radius):
self.radius = radius
30 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
def click_button(value):
if value == "=":
try:
result = eval(entry.get()) entry.delete(0,
tk.END) entry.insert(tk.END, str(result))
except Exception as e:
messagebox.showerror("Error", "Invalid Input") elif
value == "C": entry.delete(0, tk.END) else:
entry.insert(tk.END, value) app
= tk.Tk()
app.title("Interactive Calculator")
app.geometry("300x400") app.resizable(False,
False)
# Button buttons
=[
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"C", "0", "=", "+"
]
row_val = 1 col_val
=0
)
button_widget.grid(row=row_val, column=col_val, sticky="nsew")
col_val += 1 if col_val > 3: col_val = 0 row_val += 1
Output:
/usr/local/bin/python3 "/Users/yuvrajsharma/Desktop/python final file. /programme4a.py"
yuvrajsharma@Yuvrajs-MacBook-Pro python final file. % /usr/local/bin/python3 "/Users/yuvrajsharma/
Desktop/python final file. /programme4a.py"
32 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
33 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
34 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
35 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
36 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
37 | P a g e
Python Programming for Artificial Intelligence (24CAI1101)
38 | P a g e