- What exactly is []?
Ans: List object will be created within this square bracket []
- In a list of values stored in a variable called spam, how would you assign the value 'hello' as the third value? (Assume [2, 4, 6, 8, 10] are in spam.)
Ans: spam[2] = ”hello”
Let's pretend the spam includes the list ['a', 'b', 'c', 'd'] for the next three queries. 3. What is the value of spam[int(int('3' * 2) / 11)]?
Ans: d
- What is the value of spam[-1]?
Ans:d
- What is the value of spam[:2]?
Ans: [‘a’,’b’]
Let's pretend bacon has the list [3.14, 'cat,' 11, 'cat,' True] for the next three questions. 6. What is the value of bacon.index('cat')?
Ans: 1
- How does bacon.append(99) change the look of the list value in bacon?
Ans: [3.14, 'cat', 11, 'cat', True, 99]
- How does bacon.remove('cat') change the look of the list in bacon?
Ans: [3.14, 11, 'cat', True, 99]
- What are the list concatenation and list replication operators?
Ans: The operator for list concatenation is +, while the operator for replication is *. (This is the same as for strings.)
- What is difference between the list methods append() and insert()?
Ans: The only difference between append() and insert() is that insert function allows us to add a specific element at a specified index of the list unlike append() where we can add the element only at end of the list.
- What are the two methods for removing items from a list?
Ans: There are three ways in which you can Remove elements from List: Using the remove() method. Using the list object's pop() method. Using the del operator.
- Describe how list values and string values are identical.
Ans: The values that make up a list are called its elements. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can have any type and for any one list, the items can be of different types.
- What's the difference between tuples and lists?
Ans: 1 Lists are mutable Tuples are immutable 2 Implication of iterations is Time-consuming The implication of iterations is comparatively Faster 3 The list is better for performing operations, such as insertion and deletion. Tuple data type is appropriate for accessing the elements 4 Lists consume more memory Tuple consume less memory as compared to the list 5 Lists have several built-in methods Tuple does not have many built-in methods. 6 The unexpected changes and errors are more likely to occur In tuple, it is hard to take place.
- How do you type a tuple value that only contains the integer 42?
Ans: (42,) (The trailing comma is mandatory)
- How do you get a list value's tuple form? How do you get a tuple value's list form?
Ans: List to tuple: tuple(list name) Tuple to list: Using list comprehension along with zip() function
- Variables that "contain" list values are not necessarily lists themselves. Instead, what do they contain?
Ans: It may contain int, str, tuple, float and etc.
- How do you distinguish between copy.copy() and copy.deepcopy()?
Ans: copy.copy() is shallow copy of the original. We can modify the elements in the copy. But that’s not possible in the deep copy.