Tuple:-
- Tuple is immutable. User cannot change elements of a tuple once it is assigned.
- The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
- Tuples are defined by specifying items separated by commas within a pair of parentheses. Methods that add items or remove items are not available with tuple.
- However, there are certain advantages of implementing a tuple over a list. Below listed are some of the main advantages:-
- We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
- Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.
- Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
- If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
Tuple Operations:-
1)index()
2)count()
3)len()
4)max()
5)min()
6)tuple()
7)sum()
8)sorted()
9)reverse()
10)any()
11)all()
Adding Tuples:- Tuple can be added by using the concatenation operator(+) to join two tuple.
Replicating tuple:- Replicating means repeating. It can be performed by using '*' operator by a specific number of time.
Tuple slicing:- A subpart of a tuple can be retrieved on the basis of index.
Count():- returns the number of occurrences of an element in a tuple. It searches the given element in a tuple and returns how many times the element has occurred in it.
Syntax:-
tuple.count(element)
Index():- searches an element in a tuple and returns its index. If the same element is present more than once, the first/smallest position is returned.
Syntax:-
tuple.index(element)
max():- Returns the maximum value from the tuple.
Syntax:-
max(tuple)
min():- Returns the minimum value from the tuple.
Syntax:-
min(tuple)
Len():- returns the length of a tuple
Syntax:-
len(tuple)
Sum():- adds the items of an iterable and returns the sum.
Syntax:-
sum(iterable, start)
The sum() function adds start and items of the given iterable from left to right. Start is optional. The default value of start is 0.
all():- returns True when all elements in the given iterable are true.
If not, it returns False.
Syntax:-
all(iterable)
iterable - any iterable (list, tuple, dictionary, etc.) which contains the elements
The all() method returns:
True - If all elements in an iterable are true
False - If any element in an iterable is false
In case of dictionaries, if all keys (not values) are true or the dictionary is empty, all() returns True. Else, it returns false for all other cases.
Some Python Methods:-
any():- returns True if any element of an iterable is true. If not, this method returns False.
Syntax:-
any(iterable)
iterable - any iterable (list, tuple, dictionary, etc.) which contains the elements
The any() method returns:
True if at least one element of an iterable is true
False if all elements are false or if an iterable is empty
In case of dictionaries, if all keys (not values) are false, any() returns False. If at least one key is true, any() returns True.
enumerate():- method adds counter to an iterable and returns it (the enumerate object).
Syntax:-
enumerate(iterable, start=0)
iterable - a sequence, an iterator, or objects that supports iteration
start (optional) - enumerate() starts counting from this number. If start is omitted, 0 is taken as start.
User can convert enumerate objects to list and tuple using list() and tuple() method respectively.
NEXT POST ABOUT DICTIONARY In Python...…..
Check Out Our Python ALL Post In Just One Click....
Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)




0 Comments