Python Built-in DataTypes - Day - 11
- There are 4 built-in data types which are used to store the collections of data, and those are Lists, Tuple, Dictionary and Set.
Lists :
used to store multiple values in single variable, starting from index 0.
items are ordered, changeable and allows duplicate values.
represented in square brackets.
list items are indexed, they can be accessed by referring to the index number.
it supports negative indexing, and it means it starts from end of the list.
| Method | Description | | --- | --- | | append() | Adds an element at the end of the list | | clear() | Removes all the elements from the list | | copy() | Returns a copy of the list | | count() | Returns the number of elements with the specified value | | extend() | Add the elements of a list, to the end of the current list | | index() | Returns the index of the first element with the specified value | | insert() | Adds an element at the specified position | | pop() | Removes the element at the specified position | | remove() | Removes the item with the specified value | | reverse() | Reverses the order of the list | | sort() | Sorts the list |
Tuple:
items are ordered ,unchangeable and allows duplicate members.
represented in round brackets.
Its mandatory if you want to create a Tuple with one item, just use comma after the item.
Items in the tuple can be of any data type.
these are defined as objects with data type 'tuple'.
it also supports negative indexing which starts from end of tuple.
As the items are unchangeable, no items cannot be changed but if that needs to be done, then convert the tuple to list and then change it back to tuple again.
| Method | Description | | --- | --- | | count() | Returns the number of times a specified value occurs in a tuple | | index() | Searches the tuple for a specified value and returns the position of where it was found |
Set:
the items are unordered, unchangeable and unindexed.
represented using curly brackets.
as the items are unordered, there should not be any duplicates in the Set. If any duplicates, it just ignores. (Values 'True and 1' and 'false and 0' are considered to be same values and treated as duplicates)
accessing items is not possible by using index, instead use for loop or by using 'in' keyword.
| Method | Shortcut | Description | | --- | --- | --- | | add() | | Adds an element to the set | | clear() | | Removes all the elements from the set | | copy() | | Returns a copy of the set | | difference() |
-
| Returns a set containing the difference between two or more sets | | difference_update() |-=
| Removes the items in this set that are also included in another, specified set | | discard() | | Remove the specified item | | intersection() |&
| Returns a set, that is the intersection of two other sets | | intersection_update() |&=
| Removes the items in this set that are not present in other, specified set(s) | | isdisjoint() | | Returns whether two sets have a intersection or not | | issubset() |<=
| Returns whether another set contains this set or not | | |<
| Returns whether all items in this set is present in other, specified set(s) | | issuperset() |>=
| Returns whether this set contains another set or not | | |>
| Returns whether all items in other, specified set(s) is present in this set | | pop() | | Removes an element from the set | | remove() | | Removes the specified element | | symmetric_difference() |^
| Returns a set with the symmetric differences of two sets | | symmetric_difference_update() |^=
| Inserts the symmetric differences from this set and another | | union() ||
| Return a set containing the union of sets | | update() ||=
| Update the set with the union of this set and others |Dictionaries:
used to store values in key: value pair.
items are ordered, changeable and do not allow duplicates.
represented using curly brackets and have keys and values.
cannot have 2 items with same key, if any then duplicate values will overwrite existing values.
| Method | Description | | --- | --- | | clear() | Removes all the elements from the dictionary | | copy() | Returns a copy of the dictionary | | fromkeys() | Returns a dictionary with the specified keys and value | | get() | Returns the value of the specified key | | items() | Returns a list containing a tuple for each key value pair | | keys() | Returns a list containing the dictionary's keys | | pop() | Removes the element with the specified key | | popitem() | Removes the last inserted key-value pair | | setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value | | update() | Updates the dictionary with the specified key-value pairs | | values() | Returns a list of all the values in the dictionary |
-