UNIT 3
UNIT 3
UNIT 3
n=[6,0,1,2,9,3,2,0,3]
print(n.index(9))
print(n.index(10))
Output :
4
Traceback (most recent call last):
File "<string>", line 3, in <module>
ValueError: 10 is not in list
n=[6,0,1,2,9]
n.append(10)
print(n)
n.append([11,12,13])
print(n)
Output :
[6, 0, 1, 2, 9, 10]
[6, 0, 1, 2, 9, 10, [11, 12, 13]]
n=[6,0,1,2,9,3,2,0,3]
print(n.count(3))
Output :
2
n=[6,0,1,2,9]
n.remove(1)
n=[6,0,1,2,9]
n.insert(2,50)
print(n)
Output :
[6, 0, 50, 1, 2, 9]
n=[6,0,1,2,9]
n.pop(1)
print(n)
n.pop()
print(n)
Output :
[6, 1, 2, 9]
[6, 1, 2]
print(n)
n.remove(10)
print(n)
Output :
[6, 0, 2, 9]
Traceback (most recent call last):
File "<string>", line 4, in <module>
ValueError: list.remove(x): x not in list
n=[6,0,1,2,9]
n.reverse()
print(n)
Output :
[9, 2, 1, 0, 6]
n=[6,0,1,2,9]
n.sort()
print(n)
Output :
[0, 1, 2, 6, 9]
n=[6,0,1,2,9]
n.extend([11,12,13])
print(n)
Output :
[6, 0, 1, 2, 9, 11, 12, 13]
x=[10,20,30,40]
y=x
print(id(x))
print(id(y))
Output:
139985896812096
139985896812096
n=[6,0,1,2,9]
n.clear()
print(n)
Output :
[ ]
n=[6,0,1,2,9]
a=n.copy()
print(n)
print(a)
Output :
[6, 0, 1, 2, 9]
[6, 0, 1, 2, 9]
Ex:
x=[10,20,30,40]
y=x[:]
y[1]=777
print(x)
print(y)
Output :
[10,20,30,40]
[10,777,30,40]
def Cloning(li1):
li_copy = list(li1)
return li_copy
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
Output :
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
def Cloning(li1):
li_copy =[]
li_copy = li1.copy()
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
Output :
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
def Cloning(li1):
li_copy =[]
for item in li1:
li_copy.append(item)
return li_copy
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
Output :
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
6. Write a function called is_anagram that takes two strings and returns True
if they are anagrams.
Program:
def is_anagram(s1,s2):
s1=sorted(s1)
s2=sorted(s2)
if(s1==s2):
return True
else:
return False
a=input("enter 1st string:")
b=input("enter 2nd string:")
print("Is Anagram:",is_anagram(a.lower(),b.lower()))
Output:
enter 1st string:Ajax
enter 2nd string:Jaxa
Is Anagram: True