Python Tuple Tutorial with Examples

  • date 17th July, 2019 |
  • by Prwatech |
  • 0 Comments

Creating Tuples in Python

 

Python Tuple Tutorial with Examples, Welcome to the world of Tuples in Python Tutorial. Are you the one who is looking forward to knowing the Tuples in Python? Or the one who is very keen to explore the Tuples in online python course with Examples that are available? Then you’ve landed on the Right path which provides the standard information of Tuples in python training course online.

Here, one can learn what is Python Tuples Tutorials with Examples, how to create a tuple in Python, nested tuples, repeating tuples, and tuple operations in python. If you are the one who wanted to become an expert in Python? Or the one who wants to explore the technology like a Pro under the certified experts with a world-class training environment, then asks your Python training institute experts who offer Advanced advanced advanced online python course with a certificate. Follow the below-mentioned Python Tuple tutorials with examples and enhance your skills to become a professional Python Developer.

What is Python Tuple?

A tuple is an immutable ordered collection of elements in python enclosed with round brackets. The difference between tuples and lists is, a tuple is immutable means we can’t change tuple like a list. Lists are created with square brackets whereas tuples are created using parentheses.

How to Create a Tuple in Python?

Creating Tuples: To create a tuple we just have to put values with separated commas in round brackets.  tup1 = (1,2,3,6,7,8); we can also put strings in tuple as follows: tup2 = (‘green’,’yellow’,’red’); Or we can put string with double inverted commas as : tup3 = “green”, “red”, “yellow”;

The empty tuple can be created with two parentheses having no value:

tup12 = ();

To create the tuple with a single value we have to write comma after that value although it is having no further values.

test = (15,);

Tuples start with index ‘0’. They can be manipulated by slicing, concatenating, and further processes like lists.

Repeating Tuples N times:

If we want to repeat the string ‘n’ times then we can do it as follows:

test=(‘stats’)*4

Output:

statsstatsstatsstats

It will give a continuous string as output.

If we want separate strings as output we have to write

Test= (‘stats’,)*4

Output:

(‘Stats’, ‘stats’, ‘stats’, ‘stats’)

Nested Tuples in Python

Nested tuples can be created as follows:

tup1 =(0, 4,8,12)

tup2 =(‘tuple’,’python’)

tuplnest =(tup1, tup2)

Print(tuplnest)

Output

(0, 4, 8, 12,’tuple’,’python’)

Immutable tuple in Python

Tuples are immutable, unlike lists. The elements in tuple can’t be edited.

Let’s see one example:

tup =(0, 1, ‘x’, ‘y’)

tuple1[0] =4

print(tup)

Output:

TypeError: ‘tuple’ object does not support item assignment.

Slicing  Tuple in Python

In tuple slicing we can select the part of tuple as output , defined by starting and ending index as follows:

Ex)

test=(0,2,4,5,6,7,8,9,12)

print(test[1:])

Output: (2,4,5,6,7,8,9,12)

print(test[2:5])

Output: (4,5,6)

print(test[::-1]) 

Output: (12,9,8,7,6,5,4,2,1,0)

# We get output in exact reverse order.

print(test[-3:-1])

Output: (8,9) 

Tuple Operations in Python

Deleting an item from Tuple: 

We can delete the tuple by using the simply del command

tup1=( 0, 1,’a’,’b’)

deltup1

For confirmation, if we again print it will give an error

print(tuple3)

Output:

NameError: name ‘tup1’ is not defined

Length of Tuple:

To find the length of a tuple we use function len()

test=(1,2,5,’testing’,’length’,’of’,’tuple’)

print(len(test))

Output:

7

 

Use of count()

To find the count of a particular element in tuple this function is used.

test=(0,1,2,3,1,2,4,5,6,1,2,1)

print(test.count(1))

Output:

4

Use of index()

It returns index of specified element in tuple.

tup=(1,2,5,’a’,’python’,’tuple’)

print(tup.index(‘python’))

Output:

4

Converting a list to a Tuple

To convert a list into a tuple following method can be used

Ex) lis1=[1,2,4,’a’,’b’,’c’,’e’]

print(lis1)

Output:

(1,2,4,’a’,’b’,’c’,’e’)

If we take a single string and we applied the same method as above, we get a tuple having all separated characters.

Ex) print(tuple(‘python tuple’)

Output:

(‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘ ‘, ‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)

Note: Here a space is also considered as character.

To compare tuples

For comparison purpose cmp(), min(), max() are used.

Let’s consider following tuples

tup1=(1,2,3,4,5)

tup2=(1,2,3,4,5,6)

To find minimum out of these tuples we use.

Ex) min(tup1,tup2)

Output:

(1,2,3,4,5)

To find the maximum out of these tuples we use.

Ex) max(tup1,tup2)

Output:

(1,2,3,4,5,6)

Checking for an element in Tuple

For this, we have to use the keyword ‘in’. It will give output in the form of True or False.

Ex) test=(1,2,3,’a’,’b’,’c’)

3 in test

Output:

True

Ex) test=(1,2,3,’a’,’b’,’c’)

‘x’ in test

Output:

False

We hope you understand the Python Tuples Tutorials with Examples concepts. Get success in your career as a Python Developer by being a part of the Prwatech, India’s leading Python training institute in Bangalore.

0
0

Quick Support

image image