Learning Python - 3rd Day (Morning - 2)
Watched this tutorial: Click here
I learnt about tuple & set.
Simply specifying Tuple & Set is same as List in python. The difference is I cannot change the value in Tuple.
Lets say there are 3 types of list in python
1. List
2. Tuple
3. Set
Example:
- Input: myList = [1, 2, 3, 4]
Here I can change the values of each element or add and remove them. - Input: myTuple = (1, 2, 3, 4)
Here I cannot change the values of each element or add and remove them. - Input: mySet= {1, 2, 3, 4}
Here I can add or remove values but no index can be specified
tuple
Tuples are immutable ordered and indexed collections of objects. Tuples of two or more items are formed by comma-separated lists of expressions. A tuple of one item (a ‘singleton’) can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions). An empty tuple can be formed by an empty pair of parentheses.
Constructors
- tuple
- Returns a tuple built from iterable.
- literal syntax
- Initializes a new instance of the tuple type.
Methods
Functions
- len
- Returns an int type specifying number of elements in the collection.
- min
- Returns the smallest item from a collection.
- max
- Returns the largest item in an iterable or the largest of two or more arguments.
- cmp
- Compares two objects and returns an integer according to the outcome.
- sum
- Returns a total of the items contained in the iterable object.
- sorted
- Returns a sorted list from the iterable.
- reversed
- Returns a reverse iterator over a sequence.
- all
- Returns a Boolean value that indicates whether the collection contains only values that evaluate to True.
- any
- Returns a Boolean value that indicates whether the collection contains any values that evaluate to True.
- enumerate
- Returns an enumerate object.
- zip
- Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
Operators
- [] (index operator)
- Gives access to a sequence’s element.
- [::] (slicing)
- Gives access to a specified range of sequence’s elements.
- + (concatenation)
- Returns a concatenation of two sequences.
- * (multiple concatenation)
- Returns a sequence self-concatenated specified amount of times.
set
Sets are mutable unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.
Sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.
Sets are implemented using dictionaries. They cannot contain mutable elements such as lists or dictionaries. However, they can contain immutable collections.
Constructors
- set()
- Returns a set type initialized from iterable.
- {} set comprehension
- Returns a set based on existing iterables.
- literal syntax
- Initializes a new instance of the set type.
Methods
Deleting
Information
- issuperset
- Returns a Boolean stating whether the set contains the specified set or iterable.
- issubset
- Returns a Boolean stating whether the set is contained in the specified set or iterable.
- isdisjoint
- Returns a Boolean stating whether the set contents do not overlap with the specified set or iterable.
Set Operations
- difference
- Returns a new set with elements in the set that are not in the specified iterables.
- intersection
- Returns a new set with elements common to the set and the specified iterables.
- symmetric_difference
- Returns a new set with elements in either the set or the specified iterable but not both.
- union
- Returns a new set with elements from the set and the specified iterables.
Set Operations Assignment
- difference_update
- Updates the set, removing elements found in the specified iterables.
- intersection_update
- Updates the set, keeping only elements found in it and the specified iterables.
- symmetric_difference_update
- Updates the set, keeping only elements found in either set or the specified iterable, but not in both.
Copying
- copy
- Returns a copy of the set.
Set Operators
Adding Elements
- |= (update)
- Adds elements from another set.
Relational Operators
- == (is equal)
- Returns a Boolean stating whether the set has the same elements as the other set.
- != (is not equal)
- Returns a Boolean stating whether the set has different elements as the other set.
- <= (issubset)
- Returns a Boolean stating whether the set is contained in the other set.
- < (issubset proper)
- Returns a Boolean stating whether the set is contained in the specified set and that the sets are not equal.
- >= (issuperset)
- Returns a Boolean stating whether the set contains the other set.
- > (issuperset proper)
- Returns a Boolean stating whether the set contains the other set and that the sets are not equal.
Set Operations
- - (difference)
- Returns a new set with elements in the set that are not in the other set.
- & (intersection)
- Returns a new set with elements common to the set and the other set.
- ^ (symmetric_difference)
- Returns a new set with elements in either the set or the other set but not both.
- | (union)
- Returns a new set with elements from the set and the other set.
Set Operations Assignment
- -= (difference_update)
- Updates the set, removing elements found in the other set.
- &= (intersection_update)
- Updates the set, keeping only elements found in it and the other set.
- ^= (symmetric_difference_update)
- Updates the set, keeping only elements found in either set or the other set, but not in both.
Functions
- len
- Returns an int type specifying number of elements in the collection.
- min
- Returns the smallest item from a collection.
- max
- Returns the largest item in an iterable or the largest of two or more arguments.
- sum
- Returns a total of the items contained in the iterable object.
- sorted
- Returns a sorted list from the iterable.
- reversed
- Returns a reverse iterator over a sequence.
- all
- Returns a Boolean value that indicates whether the collection contains only values that evaluate to True.
- any
- Returns a Boolean value that indicates whether the collection contains any values that evaluate to True.
- enumerate
- Returns an enumerate object.
- zip
- Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
Comments
Post a Comment