Skip to main content

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
So, what do we need them?
Because of speed.

Set is faster than Tuple and Tuple is faster than list in terms of reading the values.

More information: 

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

Information

index
Returns the index of the first occurrence of the specified tuple item.
count
Returns the number of times the specified item appears in the tuple.

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

Adding Elements

add
Adds a specified element to the set.
update
Adds specified elements to the set.

Deleting

discard
Removes an element from the set.
remove
Removes an element from the set (raises KeyError if not found).
pop
Removes and returns an arbitrary element from the set.
clear
Removes all elements from the set.

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

Popular posts from this blog

Apple iPad Air (2022) Specifications and Review in 2023

17-year-old suspect in fatal stabbing at Taylor Swift-themed dance event ID’d

Exercise vs Diet