Ca 2 Pyt

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Q1.

Create a Tuple containing atleast five strings


#Use Tuple unpacking to assign values to multiple variables.
#Concatenate two tuples and store the results in the new tuple
# #Use the index() method to find the index of a specific element
# #print out the modified tuple

myTuple1=(21, 32, 14, 56, 76, 54)


a, b, c, d,e = (1, 2, 3, 4, 5)
newTuple=(myTuple1+(a,b,c,d, e))
indexing= myTuple1.index (76)
print(indexing)
print("New Tuple is- ", newTuple)

OUTPUT

Q2. #create two sets containing atleast five elements each.


#Perform set intersection and store the results in a new set. #use set
comprehension to create a set of square of intergers.
#Check if one set is a subset of another set.
#Print out the results of each operation.
set1={"Pandu", "Abhishek", "abhi", "Himanshu", "Ayush", "Sree"}
set2={"Abhishek", "Pandu", "Harsh", "Ashhar", "Anurag", "Chirag patil"}
newset=set1.intersection(set2)
print (newset)
comprehension= {i**2 for i in range(1, 10)} #unordered
print(comprehension)
print(set1.issubset(set2))
Q3. #Implement a function to check if a number is prime,even/odd
#determine if a number is even, prime or odd
#Print out appropriate messades based on the determination.
num = int(input("Enter a number: "))
if num > 1:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
else:
is_prime = False
if num % 2 == 0:
is_even = True
else:
is_even = False
if is_prime:
print(num, "is a prime number.")
elif is_even:
print(num, "is an even number.")
else:
print(num, "is an odd number.")

Q.4

#on a string containing at least five words, Store the string in a variable
# #Use function to extract all the unique words from the string
# #Convert Each word to title Case.
#Cocatenate all the title case words into a single string, sepearate by
commas.
#print out the resulting string.
str="MY name is Abhishek, Abhishek is good at python basics"
print(str)
words= str.strip()
def unique (words):
words_list = [word. strip().lower() for word in words.split() if
word.isalnum()]
unique_words = set(words_list)
return unique_words
text = "MY name is Abhishek, Abhishek is good at python basics!"
unique_words = unique (text)
print("Unique words:", unique_words)
words = str. split()
title_cased_words = []
for word in words:
title_cased_words. append (word.title)
print(title_cased_words)
separator_string = ','
result = ''.join(title_cased_words)
print(result)

You might also like