Question

In: Computer Science

3. Using the following list in Python givenlist = [[1,2,3],[“a”,”b”,”c”],True,10] do the following: * each answer...

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?

Solutions

Expert Solution

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 :


Related Solutions

Calculus #3: 1. a) Let A = (2,4,6),B = (1,2,3) and C = (5,5,5). Find point...
Calculus #3: 1. a) Let A = (2,4,6),B = (1,2,3) and C = (5,5,5). Find point D so that ABCD is a parallelogram. b). Two points X and Y are colinear if they lie on the same line. Are the points A = (3,6,−1), B = (2,0,3) and C = (−1, 3, −4) colinear? Justify your answer.
Answer the questions below using the following information on stocks A, B, and C. A B...
Answer the questions below using the following information on stocks A, B, and C. A B C Expected Return 13% 13% 10% Standard Deviation 12% 10% 10% Beta 1.6 2 0.5                          Assume the risk-free rate of return is 4% and the expected market return is 10% Assuming an investor who will invest all of his money into one security, which stock will the investor choose? Assuming an investor with a well-diversified portfolio, which stock would the investor want to...
write a python program to do the following: ask a student for 3 coefficients a b...
write a python program to do the following: ask a student for 3 coefficients a b c using the high_school_quizz to display the solutions of a quadratic problem. after, ask the student if he would like another quadratic equation solved If the student answers anything but yes, the program terminates by displaying a good bye message if the student answers yes (any form of yes should be acceptable) , the student is asked for the coefficient again and the resulting...
Question 2 [10] TRUE/FALSE QUESTIONS Consider the following list of statements. Each statement is either true...
Question 2 [10] TRUE/FALSE QUESTIONS Consider the following list of statements. Each statement is either true or false. You must read each statement carefully and then select the option that you believe is correct as your answer. Write down only the question number and next to the number either “True” or “False”. 2.1. The level or rate of unemployment is a stock concept, that is, it is measured at a particular date. 2.2. The consumer price index (CPI) is an...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
Decide whether each of the following claims is: a) analytically true, (b) analytically false, c) not...
Decide whether each of the following claims is: a) analytically true, (b) analytically false, c) not analytic, or (d) impossible to classify (conceptually incoherent). 2) The helium atom is wearing a funny hat. 4) My dog is not a canine. 6) All three-sided figures are triangles. 8) Helping the helpless is praiseworthy. 10) She’s awake, because I see a light in the window. 12) Pornography degrades women. 14) All persons feel pain.
For each of the scales shown below (A, B, and C), answer the following questions: What...
For each of the scales shown below (A, B, and C), answer the following questions: What type of data is being collected? What level of scale measurement is being used? What is the most appropriate measure of central tendency? What is the most appropriate measure of dispersion? What weakness, if any, exists with the scale? A. How do you pay for your travel expenses? Cash , Company , charge , Check , Personal charge , Credit card , Other B....
Using the information given and the equation C = $200B + b(YD), please answer the following...
Using the information given and the equation C = $200B + b(YD), please answer the following questions. Calculate the value of Consumption (C) using the equation C = $200B + b(YD) (3 points) What is the value of MPC? (2 points) What is the value of MPS? (2 points) What is the value of multiplier M? (2 points) What happens to the value of multiplier M when MPC increases? (2 points) What happens to the value of multiplier M when...
Answer True or False to each of the following. Write the answer in the column on...
Answer True or False to each of the following. Write the answer in the column on the right. The width of the confidence interval is the margin of error. If a test is run and p=0.04, then we can reject H0 at ?=0.01. If a 95% CI for the difference in two independent means is (-4.5 to 2.1), then the point estimate is -2.1. The difference in means between two groups can be statistically significant, but not clinically significant.   The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT