# Programming and Problem Solving through Python

#### Sequence data types in python Question & Answer

Question :
How many types of sequences are supported in Python?

Three Types of Sequences are supported in python:

(i) String

(ii) List

(iii) Tuple

Question :
What are nested Lists?

A list can have an element in it, which itself is a list. Such a list is called nested list. e.g.

L = [1,2,3,4,[5,6,7],8]

Question :
Discuss the utility and significance of Lists.

The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that items in a list need not be of the same type. List is majorly used with dictionaries when there is large number of data.

Question :
What is the purpose of the del operator and pop method? Try deleting a slice.

del operator is used to remove an individual item, or to remove all items identified by a slice. It is to be used as per syntax given below –

>>>del List[index]

>>>del List[start:stop]

pop method is used to remove single element, not list slices. The pop() method removes an individual item and returns it. Its syntax is –

>>>a=List.pop() #this will remove last item and deleted item will be assigned to a.

>>>a=List[10] # this will remove the ite at index 10 and deleted item will be assigned to a.

Question :
What are list slices?

List slices, like string slices are the sub part of a list extracted out. Indexes can be used to create list slices as per following format:

seq = L[start:stop]

Question :
What do you understand by true copy of a list? How is it different from shallow copy?

A shallow copy means constructing a new collection object and then populating it with references to the child objects found in the original. In essence, a shallow copy is only one level deep. The copying process does not recurse and therefore won‘t create copies of the child objects themselves.

True Copy means you can create a copy of a list using New_list=My_list. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment.

Question :
How are Tuples different from Lists when both are sequences?

Tuples are similar to lists in many ways like indexing, slicing, and accessing individual values but they are different in the sense that –

Tuples are immutable while lists are mutable.

List can grow or shrink while tuples cannot.

Question :
Discuss the utility and significance of Tuples.

Tuples are great for holding static data that you don't plan on modifying and changing a lot. Tuples are four to seven times faster than lists. This means for every list item manipulated, four to seven tuples could be manipulated within that time frame. This has huge advantages for scientific work, as this allows for speedy data reading.

Question :
How can you say that a tuple is an ordered list of objects?

A tuple is an ordered list of objects. This is evidenced by the fact that the objects can be accessed through the use of an ordinal index amd for a given index, same element is returned everytime.

Question :
How are dictionaries different from Lists?

The dictionary is similar to lists in the sense that it is also a collection of data-items but it is different from lists in the sense that lists are sequential collections(ordered) and dictionaries are non-sequential collections(unordered).

Elements in lists or tuples can be accessed by using indexes. But in dictionaries the values can be obtained using keys. By changing the sequence of key we can shuffle the order of elements of dictionary while this thing is not possible in list.

Question :
When are dictionaries more useful than lists?

Dictionaries can be much more useful than lists when we wanted to store all our friends cell-phone numbers. We could create a list of pairs,(name of friend, phone number), but once this list becomes long enough searching this list for a specific phone number will get-time consuming. Better would be if we could index the list by our friend‘s name. This is precisely what a dictionary does.

Question :
Discuss the utility and significance of Dictionaries.