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

AI Lab Final Programs

Uploaded by

abdulbasithvk242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

AI Lab Final Programs

Uploaded by

abdulbasithvk242
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

In [9]:

#1. Implement programs using tuple


#a. Concatenation

tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print("Concatenated tuple:", concatenated_tuple)

Concatenated tuple: (1, 2, 3, 4, 5, 6)

In [11]:

#1. Implement programs using tuple


#b.Unpacking

tuple = (1, 2, 3)
a, b, *rest = tuple
print("Unpacking:", "\na:", a, "\nb:", b, "\nrest:", rest)

Unpacking:
a: 1
b: 2
rest: [3]

In [12]: #2. String Slicing and Reversing


#a. Print letters starting from index 4, ending at index 11 with step 3

str1 = input("Enter the string: ")


print("Sliced portion of str1:", str1[4:12:3])

Enter the string: My name is sreenand


Sliced portion of str1: a

In [13]:
#2. String Slicing and Reversing
#b. Print the string “WELCOME BACK TO HOME” in reverse order

str1 = "WELCOME BACK TO HOME"


print("String in reverse:", str1[::-1])

String in reverse: EMOH OT KCAB EMOCLEW

In [15]:
#3. String Manipulations
#a. Count the occurrence of a character in a string

str1 = input("Enter a string: ")


char = input("Enter the character to count: ")
print(f"Count of '{char}':", str1.count(char))
Enter a string: Hello world
Enter the character to count: Hello
Count of 'Hello': 1

In [16]:
#3. String Manipulations
#b. Reverse the order of elements in a list

colors = ["Red", "Blue", "Green"]


colors.reverse()
print("Reversed list:", colors)

Reversed list: ['Green', 'Blue', 'Red']

In [17]: #4. Sort the list in ascending and descending order

colors = [50,60,20,10,70]
colors.sort()
print("Ascending order:", colors)
colors.sort(reverse=True)
print("Descending order:", colors)

Ascending order: [10, 20, 50, 60, 70]


Descending order: [70, 60, 50, 20, 10]

In [18]: #5. Count and print all vowels in a sentence

sentence = input("Enter a sentence: ")


vowels = "aeiouAEIOU"
vowel_count = {v: sentence.count(v) for v in vowels if v in sentence}
print("Vowel counts:", vowel_count)

Enter a sentence: The world is cooked


Vowel counts: {'e': 2, 'i': 1, 'o': 3}

In [19]:
#6. Dictionary and Palindrome Check
#a. Create a dictionary of student names and their corresponding grades

student_grades = {
"Alice": 85,
"Bob": 92,
"Charlie": 78,
"David": 90,
"Eve": 88
}
for student, grade in student_grades.items():
print(student, ":", grade)

Alice : 85
Bob : 92
Charlie : 78
David : 90
Eve : 88
In [23]:
#6. Dictionary and Palindrome Check
#b. Check whether a string is a palindrome or not

string = input("Enter a string: ")


if string == string[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

Enter a string: MALAYALAM


The string is a palindrome.

In [29]: #7. Python function to find the maximum of three numbers

def max_of_three(a, b, c):


return max(a, b, c)

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


b=int(input("Enter second number: "))
c=int(input("Enter thirs number: "))

print("Max of three :" , max_of_three(a, b, c))

Enter first number: 10


Enter second number: 2
Enter thirs number: -8
Max of three : 10

In [30]: #8. Calculate the factorial of a number

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

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


print("Factorial:", factorial(num))

Enter a number: 0
Factorial: 1

In [31]:
#9. Generate random values using a module
#a. Random alphabetical string

import random
import string
print("Random letter:", random.choice(string.ascii_letters))

Random letter: U
In [33]:
#9. Generate random values using a module
#b. Random value between two integers

import random
print(random.randint(0, 10))

In [34]:
#10. Modify list elements
#a. Assign a new value to a specific index

my_list = [10, 20, 30, 40, 50]


my_list[2] = 35
print("Modified list:", my_list)

Modified list: [10, 20, 35, 40, 50]

In [35]:
#10. Modify list elements
#b. Modify a range of elements using slicing

my_list = [10, 20, 30, 40, 50]


my_list[1:3] = [15, 25]
print("Modified list:", my_list)

Modified list: [10, 15, 25, 40, 50]

You might also like