In: Computer Science
1. Find and print the length of the list listTest.
2. We can test if an item exists in a list or not, using the
keyword in
It will give you a boolean response if a certain element is
present. Find whether 'p' or 'P' is present in
listTest, and print the
results.
Print results first for 'p' and then for 'P' as shown in example
below.
For example:
Test | Result |
---|---|
listTest=['c','i','s','c','1','0','6','P','y','t','h','o','n'] |
False True |
3. We can delete one or more items from a list using the keyword
del. It can even delete the list entirely.
From the list listTest, delete the 4th
element.
4. Using the del keyword and/or the slicing operator :, delete the 3rd to 6th elements from the list listTest.
5. We can access a range of items in a list by using the slicing
(colon) operator.
There is a predefined list listTest. Use the
slicing operator to print the third to fifth
elements from listTest as a list.
Note: this does not mean the values at indices 3, 4, and 5!
6. We can access a range of items in a list by using the slicing
operator (colon).
7. There is a predefined list listTest. Use the
slicing operator to print the elements (as a list) from beginning
through the sixth from listTestWe can access a
range of items in a list by using the slicing operator
(colon).
Given a predefined list listTest, use slicing to
print the elements from the fourth through the end from
listTest
listTest=["C","i","s","c","1","0","6","P","y","t","o","n"]#defining
a list
# 1.legth of list
print("1.Legth of list = ",len(listTest))#len operator is used to
find length of list
# 2.using keyword in
print("2.")
if 'p' in listTest:
print("True")
else:
print("False")
if 'P' in listTest:
print("True")
else:
print("False")
# 3.deleting 4th element
del(listTest[3]) #list index is starts from 0.so here 3 means 4th
element
print("3.After deleting 4th element:\n",listTest)
# 4.deleting 3 to 6 elements
del(listTest[2:6])#[2:6] means 3rd,4th,5th,6th elements
print("4.After deleting 3 to 6 elements:\n",listTest)
# 5.prints 3 to 5 elements from listTest as list
list1=listTest[2:5] #[2:5] means 3rd,4th,5th elements
print("5.prints 3 to 5 elements:\n",list1)
# 6.accessing rage of items in listTest
print("6.accessing 1 to 4 elements:\n",listTest[:4])#[:4] means
1st,2nd,3rd,4th elements
# 7.
print("7.")
print("Beginning through the sixth:\n",listTest[:6]) # [:6] means
1st element to 6th element
print("fourth through the end:\n",listTest[3:]) # [3:] means 4th
element to last element