Python Tuple Append Python Tuple Comprehension Python Tuple Functions Python Tuple Index Python Tuple Methods Python Tuple to List Python Tuple to String Python Tuple vs List

17 Practical Python Tuples Examples

Tuple is just like Listing in python language, both are sequential, index based mostly knowledge structure.

The primary difference between tuples and record is that tuples are immutable i.e. we can’t modify a tuple’s content material however Listing is mutable knowledge construction. Also, tuples makes use of parenthesis and record makes use of square brackets.

This article will talk about about following in Tuples:

  1. Create an Empty Tuple
  2. Create Tuple with Homogeneous Parts
  3. Create Tuple with Heterogeneous Parts
  4. Create Tuple with Single Aspect
  5. Modify Parts of Tuple
  6. Accessing Parts of Tuple – From the Entrance
  7. Accessing Parts of Tuple – From the Back
  8. Search Within a Tuple
  9. Add Parts to a Tuple
  10. Delete an Component from a Tuple
  11. Iterate Over a Tuple
  12. Concatenation of Tuples
  13. Determine Size of a Tuple
  14. Slice a Tuple
  15. Rely the Variety of Parts in a Tuple
  16. Determine the Index of an Component in a Tuple
  17. All Tuple Examples in one Example tuples.py Program

1. Create an Empty Tuple

Tuple could be created by merely writing parts of tuple, separated by comma “,” enclosed by parenthesis. Parenthesis is elective, but it’s good follow to enclose the weather with it.

The following creates an empty tuple.

>>> emptyTuple=();

>>> emptyTuple
()

You can too create an empty tuple with constructor as proven under

>>> empty=tuple();

>>> empty
()

Empty tuple could be helpful in few algorithmic codes where empty construction is required in some instances.

2. Create Tuple with Homogeneous Parts

The next tuple has all integer values for it’s parts.

>>> intTuple=(1,2,Three,5,6);

>>> intTuple
(1, 2, Three, 5, 6)

The following tuple has all string values for it’s parts.

>>> stringTuple=(“geek1″,”geek2″,”geek3”);

>>> stringTuple
(‘geek1’, ‘geek2’, ‘geek3’)

Three. Create Tuple with Heterogeneous Parts

The next tuple has a mixture of both integers and strings for it’s parts.

>>> mixTuple=(1,2,Three,Four,”geek1″,”geek2″,”geek3″);

>>> mixTuple
(1, 2, 3, 4, ‘geek1’, ‘geek2’, ‘geek3’)

Four. Create Tuple with Single Factor

You can too create a tuple simply with one factor. However, be sure to use a comma as proven under. Without comma, you are not creating a single aspect tuple.

>>> t=(1,);

>>> t
(1,)

>>> t1=(2);

>>> t1
2

Observe: Without additional comma, t1 might be integer variable as an alternative of tuple. So to create a tuple with single factor all the time use comma after the aspect.

5. Modify Parts of Tuple

Tuples are immutable, we can’t change the tuple as soon as it’s initialized, and that is why they’re greatest keys for dictionaries

>>> primes=(3,5,7,11,13,17)

>>> primes
(Three, 5, 7, 11, 13, 17)

>>> primes[0] 3

>>> primes[0]=19
Traceback (most up-to-date name last):
File ““, line 1, in
primes[0]=19
TypeError: ‘tuple’ object doesn’t help merchandise task
>>>
It’s going to throw an error each time code try to modify its parts.

6. Accessing Parts of Tuple – From the Front

Tuple is an indexed knowledge structure like array, in comparable style we will access the weather of tuple using [] operator, index begins from ‘0’. And it will throw an error if code attempt to entry tuple beyond its range

Component : Three 5 7 11 13 19
Index : 0 1 2 3 4 5

Accessing from entrance:

>>> primes=(Three,5,7,11,13,19)

>>> primes
(3, 5, 7, 11, 13, 19)

>>> primes[1] 5

>>> primes[6] Traceback (most recent name last):
File ““, line 1, in
primes[6] IndexError: tuple index out of vary

7. Accessing Parts of Tuple – From the Again

Accessing from end: Utilizing destructive indexing, ‘-1’ will level to last item and so on

Factor: 3 5 7 11 13 17
Index : -6 -5 -Four -3 -2 -1

>>> primes=(3,5,7,11,13,17);

>>> primes[-1] 17

>>> primes[-2] 13

eight. Search Inside a Tuple

An element could be searched in tuple using tuple’s membership check, ‘in’ key phrase

>>> primes=(3,5,7,11,13,17);

>>> primes
(Three, 5, 7, 11, 13, 17)

>>> print (3 in primes)
True

>>> print (41 in primes)
False

9. Add Parts to a Tuple

As already mentioned Tuples are immutable so we can’t modify parts of a tuple, and addition of parts not supported, so append, add features aren’t defined for tuple.

>>> primes
(Three, 5, 7, 11, 13, 17)

>>> primes.append(91)
Traceback (most recent call last):
File ““, line 1, in
primes.append(91)
AttributeError: ‘tuple’ object has no attribute ‘append’

10. Delete an Factor from a Tuple

We can’t modify parts of tuple, can’t delete it. But we will delete entire tuple along with its parts utilizing ‘del’ keyword

>>> primes
(Three, 5, 7, 11, 13, 17)

>>> del primes

>>> primes
Traceback (most recent call final):
File ““, line 1, in
primes
NameError: identify ‘primes’ is just not outlined

11. Iterate Over a Tuple using For Loop

You’ll be able to loop via gadgets in a tuple utilizing for loop as proven under.

>>> primes=(Three,5,7,11,13,17);

>>> primes
(3, 5, 7, 11, 13, 17)
>>> for prime in primes:
print (prime);

3
5
7
11
13
17

For extra particulars on for loop, confer with: 12 Important Python For Loop Command Examples

12. Concatenation of Tuples

You’ll be able to add (concatenate) to tuples utilizing plus (+) as proven under.

>>> primes=(Three,5,7,11,13);

>>> primes
(3, 5, 7, 11, 13)

>>> names=(“c”,”c++”,”java”,”angular”);

>>> names
(‘c’, ‘c++’, ‘java’, ‘angular’)

>>> primes+names
(Three, 5, 7, 11, 13, ‘c’, ‘c++’, ‘java’, ‘angular’)

13. Determine Size of a Tuple

Use len perform as proven under to determine the variety of parts in a tuple.

>>> primes+names
(3, 5, 7, 11, 13, ‘c’, ‘c++’, ‘java’, ‘angular’)

>>> len(names+primes)
9

14. Slice a Tuple

‘:” can be utilized to get slice of a tuple, slice will begin from first index and before the last index. And if one index is absent then all the gadgets until that index.

Component : Three 5 7 11 13
Index : zero 1 2 3 4

>>> primes
(3, 5, 7, 11, 13)

>>> primes[0:3] (Three, 5, 7)

>>> primes[1:2] (5,)

>>> primes[:3] (3, 5, 7)

>>> primes[:-2] (Three, 5, 7)

15. Rely the Number of Parts in a Tuple

This perform will return the no of parts in the tuple these are equal to the input parameter

>>> primes
(Three, 5, 7, 11, 13)

>>> primes=(3,5,7,11,13,11,7);

>>> primes.rely(3)
1

>>> primes.rely(11)
2

>>> primes.rely(101)
zero

16. Determine the Index of an Aspect in a Tuple

This perform will return index of first component that is the same as enter parameter

>>> primes
(3, 5, 7, 11, 13, 11, 7)

>>> primes.index(3)
0

>>> primes.index(11)
Three

>>> primes.index(7)
2

If component doesn’t exist in tuple, it is going to throw an error

>>> primes.index(101)
Traceback (most recent call last):
File ““, line 1, in
primes.index(101)
ValueError: tuple.index(x): x not in tuple

17. All Tuple Examples in one Sample Python Program

vi tuples.py

Copy/Paste the next to your tuples.py

# Creating an empty tuple
emptyTuple=();
emptyTuple

# Creating tuple with constructor
empty=tuple();
empty

# Creating tuple with homogeneous parts
intTuple=(1,2,Three,5,6);
intTuple

# Creating tuple with heterogeneous parts
mixTuple=(1,2,Three,4,”geek1″,”geek2″,”gee3″);
mixTuple

# Creating tuple with single aspect
t=(1,);
t

# Modifying parts of Tuple
primes=(3,5,7,11,13,17)
# The next line will give an error message.
#primes[0]=19

# Accessing tuple from front
primes=(3,5,7,11,13,19)
primes[1]

# Accessing tuple from finish
primes[-1]

# Search inside Tuple
primes=(Three,5,7,11,13,17);
print (3 in primes)
print (43 in primes)

# Including parts to tuple
# The following line will give an error message.
#primes.append(91)

# Deleting a Tuple
del primes

# Iterating over a tuple
primes=(Three,5,7,11,13,17);
for prime in primes:
print (prime);

# Concatenation
primes=(3,5,7,11,13);
names=(“c”,”c++”,”java”,”angular”);
primes+names

# Length of tuple
len(names+primes)

# Slicing operator
primes[0:3]

# Rely perform
primes=(3,5,7,11,13,11,7);
primes.rely(3)

# Index perform
primes.index(3)

In case you loved this article, you may also like..