ACCESSING NUMBER TUPLE IN PYTHON
INTRODUCTION
Number tuple will contain any number variable as a field, either Integer or Floating point number.
ORDER OF PYTHON TOUPLE
Order in Python list is very important. Because tuple fields are accessed in the order they appear in the tuple.
int_tuple = (400, 200, 600, 100, 150, 500)
print("\nFields in the tuple -> ", int_tuple)
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 100, 150, 500)
TOUPLE INDEX
Indexes are used to access the individual field in the tuple. Index in Python tuple starts with 0.
INTEGER TOUPLE
# Number tuple example 1
int_tuple = (400, 200, 600, 100, 150, 500)
print("\nFields in the tuple -> ", int_tuple)
print("\n1st field of tuple int_tuple ->", int_tuple[0])
print("2nd field of tuple int_tuple ->", int_tuple[1])
print("3rd field of tuple int_tuple ->", int_tuple[2])
print("4th field of tuple int_tuple ->", int_tuple[3])
print("5th field of tuple int_tuple ->", int_tuple[4])
print("6th field of tuple int_tuple ->", int_tuple[5])
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 100, 150, 500)
1st field of tuple int_tuple -> 400
2nd field of tuple int_tuple -> 200
3rd field of tuple int_tuple -> 600
4th field of tuple int_tuple -> 100
5th field of tuple int_tuple -> 150
6th field of tuple int_tuple -> 500
FLOATING POINT NUMBER TOUPLE
# Number tuple example 2
float_tuple = (4.2, 2.8, 6.5, 1.9, 15.2, 5.4)
print("\nFields in the tuple -> ", float_tuple)
print("\n1st field of tuple float_tuple ->", float_tuple[0])
print("2nd field of tuple float_tuple ->", float_tuple[1])
print("3rd field of tuple float_tuple ->", float_tuple[2])
print("4th field of tuple float_tuple ->", float_tuple[3])
print("5th field of tuple float_tuple ->", float_tuple[4])
print("6th field of tuple float_tuple ->", float_tuple[5])
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (4.2, 2.8, 6.5, 1.9, 15.2, 5.4)
1st field of tuple float_tuple -> 4.2
2nd field of tuple float_tuple -> 2.8
3rd field of tuple float_tuple -> 6.5
4th field of tuple float_tuple -> 1.9
5th field of tuple float_tuple -> 15.2
6th field of tuple float_tuple -> 5.4
ACCESSING TUPLE INDEX IN REVERSE ORDER
Python allows negative values as index which in turn helps us to access the tuple fields in reverse order.
When we are accessing the tuple fields from reverse order then index position 0 will not be used as we are using negative index.
# Number tuple example 3
int_tuple = (400, 200, 600, 100, 150, 500)
print("\nFields in the tuple -> ", int_tuple)
print("\n1st field of tuple int_tuple ->", int_tuple[-6])
print("2nd field of tuple int_tuple ->", int_tuple[-5])
print("3rd field of tuple int_tuple ->", int_tuple[-4])
print("4th field of tuple int_tuple ->", int_tuple[-3])
print("5th field of tuple int_tuple ->", int_tuple[-2])
print("6th field of tuple int_tuple ->", int_tuple[-1])
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 100, 150, 500)
1st field of tuple int_tuple -> 400
2nd field of tuple int_tuple -> 200
3rd field of tuple int_tuple -> 600
4th field of tuple int_tuple -> 100
5th field of tuple int_tuple -> 150
6th field of tuple int_tuple -> 500
In the above example, we can see that by using negative index values we have printed the tuple in normal order. In below example we will see that if we start with index value -1 then the entire tuple will be printed in reverse order.
# Number tuple example 4
int_tuple = (400, 200, 600, 100, 150, 500)
print("\nFields in the tuple -> ", int_tuple)
print("\n1st field of tuple int_tuple from the end ->", int_tuple[-1])
print("2nd field of tuple int_tuple from the end ->", int_tuple[-2])
print("3rd field of tuple int_tuple from the end ->", int_tuple[-3])
print("4th field of tuple int_tuple from the end ->", int_tuple[-4])
print("5th field of tuple int_tuple from the end ->", int_tuple[-5])
print("6th field of tuple int_tuple from the end ->", int_tuple[-6])
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 100, 150, 500)
1st field of tuple int_tuple from the end -> 500
2nd field of tuple int_tuple from the end -> 150
3rd field of tuple int_tuple from the end -> 100
4th field of tuple int_tuple from the end -> 600
5th field of tuple int_tuple from the end -> 200
6th field of tuple int_tuple from the end -> 400
If we try to access a field of tuple which does not exists, then Python will raise an error -> “tuple index out of range“
# Number tuple example 5
int_tuple = (400, 200, 600, 100, 150, 500)
print("\nFields in the tuple -> ", int_tuple)
print("\n1st field of tuple int_tuple ->", int_tuple[0])
print("2nd field of tuple int_tuple ->", int_tuple[1])
print("3rd field of tuple int_tuple ->", int_tuple[2])
print("4th field of tuple int_tuple ->", int_tuple[3])
print("5th field of tuple int_tuple ->", int_tuple[4])
print("6th field of tuple int_tuple ->", int_tuple[5])
print("7th field of tuple int_tuple ->", int_tuple[6])
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 100, 150, 500)
1st field of tuple int_tuple -> 400
2nd field of tuple int_tuple -> 200
3rd field of tuple int_tuple -> 600
4th field of tuple int_tuple -> 100
5th field of tuple int_tuple -> 150
6th field of tuple int_tuple -> 500
Traceback (most recent call last):
File "d:\PYTHON\PROGRAM\01_hello.py", line 16, in <module>
print("7th field of tuple int_tuple ->", int_tuple[6])
~~~~~~~~~^^^
IndexError: tuple index out of range
In the above example, we have tried to access the 7th field of the tuple which doesn’t exists. So python has raised the error.
COUNTING TOUPLE FIELDS
len() FUNCTION
len() function helps us to count the number fields present in the tuple including duplicates.
# Number tuple example 6
int_tuple = (400, 200, 600, 100, 150, 500, 600)
print("\nFields in the tuple -> ", int_tuple)
print("\nNumber of element present in the tuple int_tuple ->", len(int_tuple))
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 100, 150, 500, 600)
Number of element present in the tuple int_tuple -> 7
count() FUNCTION
count() function returns the number of occurrences of a field in a tuple. If any field is not present in the tuple then count() function will return 0.
# Number tuple example 7
int_tuple = (400, 200, 600, 10.5, 150, 500, 600)
print("\nFields in the tuple -> ", int_tuple)
print("\nNumber of occurrence of 600 in the tuple int_tuple ->", int_tuple.count(600))
print("Number of occurrence of 10.5 in the tuple int_tuple ->", int_tuple.count(10.5))
print("Number of occurrence of 700 in the tuple int_tuple ->", int_tuple.count(700))
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 10.5, 150, 500, 600)
Number of occurrence of 600 in the tuple int_tuple -> 2
Number of occurrence of 10.5 in the tuple int_tuple -> 1
Number of occurrence of 700 in the tuple int_tuple -> 0
set() FUNCTION
set() function removes the duplicate fields from the tuple.
# Number tuple example 8
int_tuple = (400, 200, 600, 10.5, 150, 500, 600, 500, 10.5)
print("\nFields in the tuple -> ", int_tuple)
print("\nFields in the tuple -> ", set(int_tuple))
ACCESSING NUMBER TUPLE IN PYTHON : Output
Fields in the tuple -> (400, 200, 600, 10.5, 150, 500, 600, 500, 10.5)
Fields in the tuple -> {200, 10.5, 400, 500, 150, 600}
In the above example, we can see that the original tuple contains 600, 500, 10.5 twice. But after using the set() function, the duplicates have been removed. The revised tuple contains only one field for 600, 500, 10.5. Also the output is enclosed with curly brackets which indicates the output is a set.
RELATED TOPICS:
- INTRODUCTION TO PYTHON TUPLE
- ACCESSING STRING TUPLE IN PYTHON
- ACCESSING MIXED TUPLE IN PYTHON
- ACCESSING NESTED TUPLE IN PYTHON
- PYTHON TOUPLE SLICING OPERATION
- STEP SIZE IN PYTHON TUPLE SLICING
- IMMUTABILITY OF TUPLE IN PYTHON
- ZIP FUNCTION IN PYTHON TUPLE
- PYTHON TUPLE SORTING OPERATIONS
- PYTHON TUPLE BUILT-IN FUNCTIONS
- REOVING TUPLE DUPLICATES USING SET
- PYTHON TUPLE SHALLOW COPY
- PYTHON TUPLE DEEP COPY
Pingback: STEP SIZE IN PYTHON TUPLE SLICING - Sayantan's Blog On Python Programming