ARTIFICIAL
INTELLIGENCE
(PRACTICAL FILE)
SUBJECT CODE: 417
(Language: Python)
Submitted By: Tanuj Khanduja
Roll No.: 34
Class & Section: 9 E
(Session: 2023-2024)
____________________________________
Salwan Public School
Sector-15(II), Gurugram-122001
TABLE OF CONTENT
S.NO
PROGRAM T. Sign
.
Write a program to ask the user to enter his first name and last name and print it
1.
on the screen.
Write a program to ask the user to enter a number and check if the number is odd
2.
or even.
Write a program to ask the user to enter three numbers and check which one is
3.
the greatest among the three.
Write a program to ask the user to enter three numbers and print the smallest
4.
number out of them.
Write a program to ask the user to enter his/her age and check whether he/she is
5.
eligible to drive or not.
6. Write a program to find the sum of n natural numbers.
Write a program to ask the user to enter his/her marks of 5 subjects and calculate
7.
the percentage and print it on the screen.
Write a program to ask the user to enter the temperature in Fahrenheit and
8.
convert it to Celsius.
Write a python program to remove an empty element from the list :
9.
num=[“Hello” , 34 ,45, “ “ , 40]
Write a python program to append an element 22 to the list:
10.
num=[18,19,20,21]
Write a program to append data of the second list to the first list.
11.
List1=[23,24,25,26] List2=[27,28,29,30]
12. Write a program to count the number of items stored in a list.
13. Write a program to reverse a list in python.
Write a program to square each element of the list:
14. For example:
Given x = [2, 3, 4, 5, 6]
Expected output Result : 4 9 16 25 36
Write a program to ask the user to sort the given list:
15.
List1=[40,50,33,23,12]
Program-1: Write a program to ask the user to enter his first name and last name and print it on the
screen.
Python Code:
# Get user input for first and last names
First_name = input(“Enter your first name: “)
Last_name = input(“Enter your last name: “)
# Print the full name
Print(“Your full name is:”, first_name, last_name)
```
Output
You can run this program in a Python environment, and it will prompt you to enter your first and last
names, then print the full name on the screen.
Program-2: Write a program to ask the user to enter a number and check if the number is odd or even.
Python Code
```python
# Get user input
User_input = int(input(“Enter a number: “))
# Check if the number is odd or even
If user_input % 2 == 0:
Print(f”{user_input} is an even number.”)
Else:
Print(f”{user_input} is an odd number.”)
```
Output :
Copy and paste this code into a Python environment, and it will prompt you to enter a number and then
tell you whether it’s odd or even.
Program – 3 Write a program to ask the user to enter three numbers and check which one is the
greatest among the three.
Input:
```python
# Get input from the user
Num1 = float(input(“Enter the first number: “))
Num2 = float(input(“Enter the second number: “))
Num3 = float(input(“Enter the third number: “))
# Compare the numbers
If num1 > num2 and num1 > num3:
Print(f”{num1} is the greatest.”)
Elif num2 > num1 and num2 > num3:
Print(f”{num2} is the greatest.”)
Else:
Print(f”{num3} is the greatest.”)
```
Output:
This program prompts the user to enter three numbers and then determines and prints the greatest
among them.
Program – 4 Write a program to ask the user to enter three numbers and print the smallest number out
of them.
Input:
```python
# Get three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Find the smallest number
smallest_number = min(num1, num2, num3)
# Print the result
print("The smallest number is:", smallest_number)
```
Output:
Copy and paste this code into a Python environment to run it.
Program – 5 Write a program to ask the user to enter his/her age and check whether he/she is eligible
to drive or not.
Input:
```python
# Get user input for age
Age = int(input(“Enter your age: “))
# Check eligibility to drive
If age >= 18:
Print(“You are eligible to drive.”)
Else:
Print(“Sorry, you are not eligible to drive yet.”)
```
Output:
Copy and paste this code into a Python environment to run it.
Program – 6 Write a program to find the sum of n natural numbers.
Input:
```python
def sum_of_natural_numbers(n):
sum_value = (n * (n + 1)) // 2
return sum_value
# Example usage:
n = int(input("Enter a positive integer (n): "))
result = sum_of_natural_numbers(n)
print(f"The sum of the first {n} natural numbers is: {result}")
```
Output:
This program uses the formula for the sum of the first n natural numbers, which is `sum = n * (n + 1) /
2`
Program – 7 Write a program to ask the user to enter his/her marks of 5 subjects and calculate the
percentage and print it on the screen.
Input :
```python
# Get marks for 5 subjects from the user
Subject1 = float(input(“Enter marks for subject 1: “))
Subject2 = float(input(“Enter marks for subject 2: “))
Subject3 = float(input(“Enter marks for subject 3: “))
Subject4 = float(input(“Enter marks for subject 4: “))
Subject5 = float(input(“Enter marks for subject 5: “))
# Calculate total marks and percentage
Total_marks = subject1 + subject2 + subject3 + subject4 + subject5
Percentage = (total_marks / 500) * 100
# Print the calculated percentage
Print(f”Your percentage is: {percentage}%”)
```
Output:
Copy and paste this code into a Python environment, run it, and enter the marks for each subject when
prompted. The program will then calculate the total marks and percentage and display it on the screen.
Program – 8 Write a program to ask the user to enter the temperature in Fahrenheit and convert it to
Celsius.
Input:
```python
# Get temperature in Fahrenheit from the user
Fahrenheit = float(input(“Enter temperature in Fahrenheit: “))
# Convert Fahrenheit to Celsius
Celsius = (fahrenheit – 32) * 5/9
# Display the result
Print(f”{fahrenheit} Fahrenheit is equal to {celsius:.2f} Celsius.”)
```
Output:
Copy and paste this code into a Python environment, and it will prompt you to enter the temperature in
Fahrenheit and then display the equivalent temperature in Celsius.
Program – 9 Write a python program to remove an empty element from the list :
num=[“Hello” , 34 ,45, “ “ , 40]
Input:
```python
Num = [“Hello”, 34, 45, “ “, 40]
# Use list comprehension to filter out empty elements
Num = [element for element in num if element.strip()]
Print(num)
```
Output:
This program uses list comprehension and the `strip()` method to remove elements that are empty or
contain only whitespaces.
Program – 10 Write a python program to append an element 22 to the list:
num=[18,19,20,21]
Input:
```python
Num = [18, 19, 20, 21]
Num.append(22)
Print(num)
This will output:
[18, 19, 20, 21, 22]
Program – 11 Write a program to append data of the second list to the first list.
List1=[23,24,25,26] List2=[27,28,29,30]
Input:
```python
List1 = [23, 24, 25, 26]
List2 = [27, 28, 29, 30]
List1.extend(List2)
Print(“Combined List:”, List1)
```
Output:
This program uses the `extend` method to append the elements of `List2` to the end of `List1`. The
final combined list is then printed.
Program – 12 Write a program to count the number of items stored in a list.
Input:
```python
My_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Item_count = len(my_list)
Print(“Number of items in the list:”, item_count)
```
Output:
This program uses the `len` function to get the length (number of items) in the list and then prints the
result.
Program – 13 Write a program to reverse a list in python.
Input :
```python
Original_list = [1, 2, 3, 4, 5]
Reversed_list = list(reversed(original_list))
Print(“Original List:”, original_list)
Print(“Reversed List:”, reversed_list)
```
Output:
This code will write the original list but reversed
Program – 14 Write a program to square each element of the list:
For example:
Given x = [2, 3, 4, 5, 6]
Expected output Result : 4 9 16 25 36
Input:
```python
X = [2, 3, 4, 5, 6]
Squared_list = [num ** 2 for num in x]
Print(“Original List:”, x)
Print(“Squared List:”, squared_list)
```
Output:
This program uses a list comprehension to create a new list where each element is squared. The
original list and the squared list are then printed.
Program – 15 Write a program to ask the user to sort the given list:
List1=[40,50,33,23,12]
Input:
```python
List1 = [40, 50, 33, 23, 12]
User_choice = input(“Do you want to sort the list? (yes/no): “).lower()
If user_choice == ‘yes’:
List1.sort()
Print(“Sorted List:”, List1)
Else:
Print(“List remains unsorted:”, List1)
```
Output:
This program prompts the user to input whether they want to sort the list. If the input is ‘yes’, the list is
sorted using the `sort` method, and the sorted list is printed. If the input is 'no' or anything else, the
original list is printed as is.