What is a List?
The simplest data structure in Python and is used to store a list of values. Lists are collections of items (strings, integers, or even other lists).
Each item in the list has an assigned index value.Lists are enclosed in [ ].
Each item in a list is separated by a comma. Unlike strings, lists are mutable, which means they can be changed.
List Creation:
Lists hold a sequence of values (just like strings can hold a sequence of characters).
Lists are very easy to create, these are some of the ways to make lists
>>>list1 = ['one',2,3.4,'nishant']
>>>print(list1)
>>>print(list1[3])['one', 2, 3.4, 'nishant']
nishant
What is Tuples?
A tuple is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers.
>>>#empty tuple
>>>t = ()>>>#tuple having integers
>>>t = (1, 2, 3)
>>>print(t)
(1, 2, 3)>>>#tuple with mixed datatypes
>>>t = (1, 'raju', 28, 'abc')
>>>print(t)
>>>print(t[1])
(1, 'raju', 28, 'abc')
raju
The important difference is that tuples are immutable.That we can’t change the elements of tuple once it is assigned whereas in the list, elements can be changed.
>>>t = (1, 'raju', 28, 'abc')
>>>print(t)
>>>print(t[3])
>>>t[3] = 'def'(1, 'raju', 28, 'abc')
abc
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-254e32ad7a3c> in <module>()
2 print(t)
3 print(t[3])
----> 4 t[3] = 'def'TypeError: 'tuple' object does not support item assignment
What is Sets?
- Sets are unordered.
- Set elements are unique. Duplicate elements are not allowed.
- A set itself may be modified, but the elements contained in the set must be of an immutable type.
- Sets can be used to perform mathematical set operations like union, intersection, symmetric difference etc.
>>>#set of integers
>>>s = {1, 2, 3}
>>>print(s)
{1, 2, 3}>>>#print type of s
>>>print(type(s))
<class 'set'>>>> x = set(['foo', 'bar', 'baz', 'foo', 'qux'])
>>> x
{'qux', 'foo', 'bar', 'baz'}
What is Dictionary?
Use {} curly brackets to construct the dictionary, and [] square brackets to index it. Separate the key and value with colons: and with commas, between each pair. Keys must be quoted As with lists we can print out the dictionary by printing the reference to it. A dictionary maps a set of objects (keys) to another set of objects (values). A Python dictionary is a mapping of unique keys to values. Dictionaries are mutable, which means they can be changed. The values that the keys point to can be any Python value. Dictionaries are unordered, so the order that the keys are added doesn’t necessarily reflect what order they may be reported back.
Dictionaries and lists share the following characteristics:
- Both are mutable.
- Both are dynamic. They can grow and shrink as needed.
- Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.
Dictionaries differ from lists primarily in how elements are accessed:
- List elements are accessed by their position in the list, via indexing.
- Dictionary elements are accessed via keys.
>>>dictval = {"Nishant":1,"Akash":2,"Ravi":3,"Hari":4}
>>>print(dictval)
{'Nishant': 1, 'Akash': 2, 'Ravi': 3, 'Hari': 4}>>>print(dictval["Akash"])
2
Comments
Post a Comment