Python Basics – Part 2 cont…
4.Tuples
A tuple is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed. A tuple is created by placing all the items (elements) inside a parentheses ()
- We generally use tuple for different datatypes and list for homogeneous similar datatypes.
- Since tuple are immutable, means value can not be changed.
- Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
- If you have data that doesn’t change, implementing it as tuple will guarantee that it remains write-protected.
Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses)
CODE:
OUTPUT:
5 .Dictionary
Python dictionary is an unordered set of key:value pairs , with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}.Dictionary have no concept of order among elements.
Keys are unique within a dictionary while values may not be.The values Continue reading