filmov
tv
Python Fundamentals - Python Tuples

Показать описание
Python Fundamentals - Python Tuples
Python Tuples, What is Tuple?, Operations on Tuples, Access Tuple Items, Range of Indexes, Change Tuple Values, Loop Through a Tuple, Check if Item Exists, Find Tuple Length, Add Items, Remove Items, and Join Two Tuples.
What is Tuple?
A tuple is a collection which is ordered and unchangeable. In Python, tuples are written with round brackets.
A Data Structure of Python or a Compound data type of Python language
Example
Create a Tuple:
fruits = ("apple", "banana", "cherry")
print (fruits)
Operations on Tuples
1) Access Tuple Items
We can access tuple items by referring to the index number, inside square brackets:
Example
Print the second item in the tuple:
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits [1])
Negative Indexing
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the tuple:
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits[-1])
2) Range of Indexes
We can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new tuple with the specified items.
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits [2:5])
It will print: ('cherry', 'orange', 'kiwi')
Note: the item in position 5 is Not included
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the tuple:
Example
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits [-4:-1])
It will print: ('orange', 'kiwi', 'melon')
This example returns the items from index -4 (included) to index -1 (excluded)
3) Change Tuple Values
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable, but we can convert the tuple into a list, change the list, and convert the list back into a tuple.
Example
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
4) Loop Through a Tuple
We can loop through the tuple items by using a for loop.
Example
Iterate through the items and print the values:
fruits= ("apple", "banana", "cherry")
for x in thistuple:
print(x)
5) Check if Item Exists
To determine if a specified item is present in a tuple,
Example
Check if "apple" is present in the tuple:
fruits = ("apple", "banana", "cherry")
if "apple" in fruits:
print("Yes, 'apple' is in the fruits tuple")
6) Find Tuple Length
To determine how many items a tuple has, use the len() keyword
Example
fruits = ("apple", "banana", "cherry")
print(len(fruits))
7) Add Items
Once a tuple is created, we cannot add items to it. Tuples are unchangeable.
fruits = ("apple", "banana", "cherry")
fruits[3] = "orange" # This will raise an error
print(fruits)
8) Remove Items
Note: We cannot remove items in a tuple.
Tuples are unchangeable, so we cannot remove items from it, but we can delete the tuple completely:
Example
The del keyword can delete the tuple completely:
fruits = ("apple", "banana", "cherry")
del fruits
print(fruits ) #this will raise an error because the tuple no longer exists
9) Join Two Tuples
To join two or more tuples you can use the + operator:
Example
Join two tuples:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
Python Tuples, What is Tuple?, Operations on Tuples, Access Tuple Items, Range of Indexes, Change Tuple Values, Loop Through a Tuple, Check if Item Exists, Find Tuple Length, Add Items, Remove Items, and Join Two Tuples.
What is Tuple?
A tuple is a collection which is ordered and unchangeable. In Python, tuples are written with round brackets.
A Data Structure of Python or a Compound data type of Python language
Example
Create a Tuple:
fruits = ("apple", "banana", "cherry")
print (fruits)
Operations on Tuples
1) Access Tuple Items
We can access tuple items by referring to the index number, inside square brackets:
Example
Print the second item in the tuple:
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits [1])
Negative Indexing
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the tuple:
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits[-1])
2) Range of Indexes
We can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new tuple with the specified items.
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits [2:5])
It will print: ('cherry', 'orange', 'kiwi')
Note: the item in position 5 is Not included
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the tuple:
Example
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits [-4:-1])
It will print: ('orange', 'kiwi', 'melon')
This example returns the items from index -4 (included) to index -1 (excluded)
3) Change Tuple Values
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable, but we can convert the tuple into a list, change the list, and convert the list back into a tuple.
Example
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
4) Loop Through a Tuple
We can loop through the tuple items by using a for loop.
Example
Iterate through the items and print the values:
fruits= ("apple", "banana", "cherry")
for x in thistuple:
print(x)
5) Check if Item Exists
To determine if a specified item is present in a tuple,
Example
Check if "apple" is present in the tuple:
fruits = ("apple", "banana", "cherry")
if "apple" in fruits:
print("Yes, 'apple' is in the fruits tuple")
6) Find Tuple Length
To determine how many items a tuple has, use the len() keyword
Example
fruits = ("apple", "banana", "cherry")
print(len(fruits))
7) Add Items
Once a tuple is created, we cannot add items to it. Tuples are unchangeable.
fruits = ("apple", "banana", "cherry")
fruits[3] = "orange" # This will raise an error
print(fruits)
8) Remove Items
Note: We cannot remove items in a tuple.
Tuples are unchangeable, so we cannot remove items from it, but we can delete the tuple completely:
Example
The del keyword can delete the tuple completely:
fruits = ("apple", "banana", "cherry")
del fruits
print(fruits ) #this will raise an error because the tuple no longer exists
9) Join Two Tuples
To join two or more tuples you can use the + operator:
Example
Join two tuples:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)