LIST
list are ordered sequences ,which can hold variety of object types , it’s also mutable . They are represented by [],and ‘,’ is used to separate objects .It supports indexing and slicing .
my_list=['kaka',100,6.42]
1.len(my_list) = 3
2.my_list[0] = 'kaka'
3.my_list[1:] = [100,6.42]
4.my_list.append('element') = adds the element at the end of the list
5.my_list.pop() = removes the last element of the list by default . .pop('index of element') is used to remove an element form the list
SORTING IN LIST
There are two sort function's in python ,one is sort() and the other one is sorted().list.sort()
modifies the list in-place (and returnsNone
to avoid confusion). Usually it’s less convenient thansorted()
- but if you don’t need the original list, it’s slightly more efficient.
so
new_list=['a','e','b','c']
my_sortlist=new_list.sort()
it's gonna return NONE type
To index a nested list just add another set of bracket'[]'.
for example :
[1,[2,3],4] ,to access 3 from the list use my_list[1][1]
DICTIONARIES
They are unordered mappings for storing object, they use the concept of key-value pairing. The key value allows the user to get objects without knowing the index of the objects. They use {} and ‘:‘ .Syntax = {“key1″:”value1″,”key2:”value2”}
When to choose a dictionary and when to choose a list??
- Dictionary: when you want to quickly grab info without knowings it’s index value. You can’t sort in dictionary.
Functions in keys
- keys() = list’s all the keys available in the dictionary.
- values() = list’s all the values available in the dictionary.
- items() = list’s all the tuples available in the dictionary.
Dictionaries do not retain order , if you want order as well check orderddict.
TUPLES
Quite similar to list accept the fact that they are immutable.We use “()”. The 2 main functions are
- count() = count the number of time an element occurs
- index() = send the index of the first occurrence of the element
SETS
They are unordered collection of unique elements.i.e only one representation of an object.
my_set= set(). O/P is denoted by {}.
BOOLEANS
The value is either True(1) or false(0). Important with control flow and logic. Capitalize the first character of True and False.
To define an object with any value we can use None operator , for example – a=None