TUPLES



Python - Tuples


A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.


CREATE A TUPLE :

>>> tuple=("apple","ball","cat","dog")
>>> print(tuple)
('apple', 'ball', 'cat', 'dog')


Access Tuple Items :

We can access tuple items by referring to the index number, inside square brackets.


>>>tuple=("apple',"ball","cat","dog")
>>>print(tuple[1])
ball

Negative Indexing :

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.


>>>tuple=("apple","banana","cat","dog")
>>>print(tuple[-2])
cat

Range of Indexes :

You 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.


>>> tuple=("apple","ball","cat","dog","egg","fish")
>>> print(tuple[2:4])
('cat', 'dog')

Note: The search will start at index 2 (included) and end at index 5 (not included).


Range of Negative Indexes :

Specify negative indexes if you want to start the search from the end of the tuple.This returns the items from index -4 (included) to index -1 (excluded).


>>> tuple=("apple","banana","catch","bounce")
>>> print(tuple[-3])
banana


Change Tuple Values :

Tuples,being unchnaged and immutable values cant be chnaged, but there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.


x = ("apple", "banana", "cherry")
>>>y = list(x)
>>>y[1] = "kiwi"
>>>x = tuple(y)

>>>print(x)
('apple','kiwi','cherry')


Loop Through a Tuple :

We can loop through the tuple items by using a for loop.


tuple=("apple","banana","cat","dog")
for i in tuple:
    print(i)

apple
banana
cat
dog

Check if Item Exists :

To determine if a specified item is present in a tuple use the in keyword:


tuple=("apple","banana","cat","dog")
if "apple"  in tuple:
    print("yes,'apple' is in tuple")
    
yes,'apple' is in tuple


Tuple Length :

To determine how many items a tuple has, use the len() method.


tuple=("app","camera","google","chrome","uc browser")
print(len(tuple))
5

Add Items :

Once a tuple is created, you cannot add items to it. Tuples are unchangeable.


thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
print(thistuple)

Traceback (most recent call last):
  File "demo_tuple_add.py", line 2, in <module>
    thistuple[3] = "orange" # This will raise an error
TypeError: 'tuple' object does not support item assignment

Create Tuple With One Item :

To create a tuple with only one item, you have to add a comma after the item, unless Python will not recognize the variable as a tuple.



tuple=("apple",)
print(type(tuple))

#not a tuple
tuple=("apple")
print(type(tuple))

<class 'tuple'>
<class 'str'>

Remove Items :

Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely using the del keyword.


tuple = ("apple", "banana", "cherry")
del tuple
print(tuple) #this will raise an error because the tuple no longer exists


Join Two Tuples :

To join two or more tuples you can use the + operator.


tuple=("apple","ball","cat","dog")
tuple1=(1,2,3,4)
tuple3=tuple+tuple1
print(tuple3)
 
('apple', 'ball', 'cat', 'dog', 1, 2, 3, 4)


The tuple() Constructor :

It is also possible to use the tuple() constructor to make a tuple.


thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)

('apple', 'banana', 'cherry')

Tuple Methods


MethodDescription
count()Returns the number of times a specified value occurs in a tuple
index()Searches the tuple for a specified value and returns the position of where it was found

Tuple inbuilt functions


FunctionDescription
cmp(tuple1, tuple2)It compares two tuples and returns true if tuple1 is greater than tuple2 otherwise false.
len(tuple)It calculates the length of the tuple.
max(tuple)It returns the maximum element of the tuple.
min(tuple)It returns the minimum element of the tuple.
tuple(seq)It converts the specified sequence to the tuple


RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials