Skip to content

Latest commit

 

History

History
202 lines (182 loc) · 3.51 KB

43. Unit 4 - Lesson 4.md

File metadata and controls

202 lines (182 loc) · 3.51 KB

Basic List Operations

Q1. 1
Q2. Need Solution
Q3.

list1 = ["hi", "hello", "Lists"]
# print every element in list1 using index
print(list1[0])
print(list1[1])
print(list1[2])
list1[2] = "Python"
# print list1
print(list1)
# add "Code is Life" to list1
list1.append("Code is Life")
# print list1
print(list1)
list1.extend([45, 67, 89])
# print list1
print(list1)

Q4.

a=input("data: ")
b=input("element: ")
print(b in a)

Q5.

data=input("data: ")
list=data.split(",")
print("first, last elements:",list[0],list[-1])

Q6.

a = [9, 8, 7, 6, 5, 4]
print("a =",a)
print("a[0:3] =",a[0:3])
print("a[:4] =",a[:4])
print("a[:] =",a[:])
print("a[2:2] =",a[2:2])
print("a[0:6:2] =",a[0:6:2])
# print("a[::-1] =",a[::-1])
print("a[-3:] =",a[-3:])
print("a[:-3] =",a[:-3])

# write your code here

Q7.

data=input("data: ")
list=data.split(",")
l=len(list)
k=int(l)-1
if list[0]=="3" or list[k]=="3":
	print("True")
else:
	print("False")

Q8.

y=input("data1: ")
z=input("data2: ")
m=z.split(",")
n=int(input("num: "))
l=y.split(",")
print(l*n)
print(m*n)
l.extend(m)
print("extending list1 with list2:",l)

Q9.

a=input("data1: ")
b=input("data2: ")
print("is equal:",a==b)
print("is not equal:",a!=b)

Q10.

a=input("data: ")
l=a.split(",")
k=int(len(l)-1)
if l[0]==l[k]:
	print("equal")
else:
	print("not equal")

Q11. Need Solution
Q12.

d=input("data: ")
l=list( int(x)for x in d.split(","))
print(l)
k=int(len(l)-1)
if l[0]>l[k]:
	print("largest among first, last elements:",l[0])
else:
	print("largest among first, last elements:",l[k])

Q13. Need Solution
Q14.

# Demo for cloning using slicing
a = [1, 2, 3, 4, 5]
print("a =", a)
print("b = a[:]")
# Write the code to clone using slicing method
b=a[:]


print("b =", b)
print("a is b ? :", a is b)

# Demo for cloning using list function
a = [1, 2, 3, 4, 5]
print("b = list(a)")
# Write the code to clone elements in a to b using list

a[0]=100
print("b =", b)
print("a is b ? :", a is b)
print("a[0] = 100 ")
# Write the code to set a[0] to 100

print("a =", a)
print("b =", b)


# Demo for cloning using copy method
a = [1, 2, 3, 4, 5]
print("a =", a)
print("b = a.copy()")
# Write the code to clone a list of elements in a to b using the copy method

print("b =", b)
print("a is b ? :", a is b)

Q15.

d1=input("data1: ")
d2=input("data2: ")
l1=list(str(x) for x in d1.split(","))
l2=list(str(x) for x in d2.split(","))
k=int(len(l1)-1)
p=int(len(l2)-1)
if l1[0]==l2[0] or l1[k]==l2[p]:
	print("True")
else:
	print("False")

Q16.

#del
dlist = ['red', 'orange', 'blue', 'green', 'yellow', 'cyan']

print("dlist =",dlist)
print("del dlist[5]")
del dlist[5]

#delete element 5 
print("dlist =",dlist)
# print the result
print("del dlist[2:]")
del dlist[2:]
print("dlist =", dlist)
print("del dlist")
del dlist

#remove
remlist = ['red', 'orange', 'blue', 'green', 'yellow', 'cyan']
print("remlist =", remlist)
print("remlist.remove('green')")
# remove green from the list and print the list
remlist.remove('green')
print("remlist =",remlist)


#pop
plist = ['red', 'orange', 'blue', 'green', 'yellow', 'cyan']
print("plist =", plist)
print("elem = plist.pop(2)")
elem=plist.pop(2)

# remove element at index 2

print("element popped & removed :", elem)
print("plist =", plist)
print("elem = plist.pop()")
elem=plist.pop()

# remove last element

print("element popped & removed :", elem)
print("plist =", plist)
print("plist.clear()")
plist.clear()
print("plist =", plist)

Q17. Need Solution