In: Computer Science
3. Using the following list in Python givenlist = [[1,2,3],[“a”,”b”,”c”],True,10] do the following:
* each answer is one point, question is 5 points a.
Print the following two slices of the givenlist [“a”,”b”,”c”] and [10] b.
Print just element [“a”] c. Print every other element (use step 2) d.
Print the type for elements [True] and [10] e. Print element at position 1 of the given list. What type is this element?
Given :
We have a nested list , givenlist=[[1,2,3],["a","b","c"],True,10]
It has list, char, bool , int data types .
Solution (Python Code) :
a)
#!/usr/bin/env python
#initialize list
givenlist = [[1,2,3],["a","b","c"],True,10]
'''
Task a) To print ["a","b","c"] and 10
["a","b","c"]: it is at index 1 of givenlist
10 : It is at index 3 of givenlist
'''
#printing ["a","b","c"] of givenlist
print(givenlist[1])
#printing 10 of givenlist
print(givenlist[3])
b)
#!/usr/bin/env python
#initialize list
givenlist = [[1,2,3],["a","b","c"],True,10]
'''
Task b) To print just element "a"
"a": It is an element of a sublist ["a","b","c"]
In this sublist , it's index is 0
Index of this sublist is 1
So, Index of this element is [1][0]
'''
#Printing element ["a"]
print(givenlist[1][0])
c)
#!/usr/bin/env python
#initialize list
givenlist = [[1,2,3],["a","b","c"],True,10]
'''
Task c) To print every element of givenlist
Since, givenlist is an example of nested list.
So, traverse through each element , Algorithm will be :
Step 1: Start
Step 2: For each element in givenlist, if it's type is list go to
step 3 else step 4
Step 3: Print each element of the sublist
Step 4: Print element of the list , 'givelist'
Step 5: End
'''
#Iterate over each element of givenlist
for i in range(len(givenlist)):
#check if element's type is list , sublist found
if(type(givenlist[i])==type(givenlist)):
#Iterate over each element of the
sublist and print
for j in
range(len(givenlist[i])):
print(givenlist[i][j])
#If it's type is not list
else:
#simply print this element
print(givenlist[i])
d)
#!/usr/bin/env python
#initialize list
givenlist = [[1,2,3],["a","b","c"],True,10]
'''
Task d) To print type of element [True] and [10]
We will use function type(): it returns type of the element
ex: type(1) : will return int as 1 is of int data type
Here, element [True] is at index 2 of givenlist
element [10] is at index 3 of givenlist
'''
#printing type of [True]
print(type(givenlist[2]))
#printing type of [10]
print(type(givenlist[3]))
e)
#!/usr/bin/env python
#initialize list
givenlist = [[1,2,3],["a","b","c"],True,10]
'''
Task e) To print type of element at index 1 of givenlist
Also, print it's type
Here, ["a","b","c"] is at position or index 1 of givenlist
And it's of 'list' data type
'''
#printing element at index 1
print(givenlist[1])
#printing type of element at index 1
print(type(givenlist[1]))
Complete code :
#!/usr/bin/env python
#initialize list
givenlist = [[1,2,3],["a","b","c"],True,10]
'''
Task a) To print ["a","b","c"] and 10
["a","b","c"]: it is at index 1 of givenlist
10 : It is at index 3 of givenlist
'''
print("----Part A---")
#printing ["a","b","c"] of givenlist
print(givenlist[1])
#printing 10 of givenlist
print(givenlist[3])
'''
Task b) To print just element "a"
"a": It is an element of a sublist ["a","b","c"]
In this sublist , it's index is 0
Index of this sublist is 1
So, Index of this element is [1][0]
'''
print("----Part B---")
#Printing element ["a"]
print(givenlist[1][0])
'''
Task c) To print every element of givenlist
Since, givenlist is an example of nested list.
So, traverse through each element , Algorithm will be :
Step 1: Start
Step 2: For each element in givenlist, if it's type is list go to
step 3 else step 4
Step 3: Print each element of the sublist
Step 4: Print element of the list , 'givelist'
Step 5: End
'''
print("----Part C---")
#Iterate over each element of givenlist
for i in range(len(givenlist)):
#check if element's type is list , sublist found
if(type(givenlist[i])==type(givenlist)):
#Iterate over each element of the
sublist and print
for j in
range(len(givenlist[i])):
print(givenlist[i][j])
#If it's type is not list
else:
#simply print this element
print(givenlist[i])
'''
Task d) To print type of element [True] and [10]
We will use function type(): it returns type of the element
ex: type(1) : will return int as 1 is of int data type
Here, element [True] is at index 2 of givenlist
element [10] is at index 3 of givenlist
'''
print("----Part D---")
#printing type of [True]
print(type(givenlist[2]))
#printing type of [10]
print(type(givenlist[3]))
'''
Task e) To print type of element at index 1 of givenlist
Also, print it's type
Here, ["a","b","c"] is at position or index 1 of givenlist
And it's of 'list' data type
'''
print("----Part E---")
#printing element at index 1
print(givenlist[1])
#printing type of element at index 1
print(type(givenlist[1]))
Console Output :