Methods and Functions

Creating Tuple() Python

Tuple() Python refers to one of the four built-in data types in Python that are used to store collections of objects separated by commas within a single variable. 

Data types are used to store various items. Therefore, all programming languages have certain data types. Python is no exception. The other data types in Python are Set, List, and Dictionary. However, we shall limit the discussion to the tuple(). Tuple in Python refers to a collection that is ordered. The elements cannot be changed once it is assigned. Therefore, these are immutable after being assigned. A tuple collects the same or different collections of objects. Thus, the round brackets () separate each element in a tuple.

Characteristics of Tuples

Tuples have the given characteristics:

  • They are indexed
  • Tuples are ordered
  • These are immutable
  • They can contain duplicate items

tuple() Parameters

Tuple() Python is a variable that may collect no, one, or many elements. Therefore, there can be different types of the tuple() in Python. 

Firstly, let us see an example of an empty tuple().

Empty Tuple() Python:

                    

tuple_empty= ()
print ( tuple_empty)


Output:

()

tuple() python

Example: Create Tuples Using Tuples() Python

We have already seen an empty tuple creation. Therefore, let us now create different types of tuples using tuple() python.

  • Firstly, let us consider the example of a Tuple() with Integers
                    

our_tuple = ( 1, 2, 3 )

print(our_tuple)


Output: ( 1, 2, 3 )

  • Secondly, let us consider the example of a Tuple() with mixed data types.
                    

our_tuple = ( ‘python’ , ‘lesson’, 1)

print(our_tuple)


Output: ( ‘python’, ‘lesson’, 1)

  • Thirdly, let us consider the example of a tuple with duplicate items.
                    

our_tuple = ( ‘python’, 1, 0, ‘python’ )

print(our_tuple)

Output: ( ‘python’, 1, 0, ‘python’ )

You may even alternately create tuples without using the parenthesis:

                    

our_tuple = ‘python, ‘lesson’

print(our_tuple)



Output: ‘python’, ‘lesson’

Create Single Element Tuples

tuple() python can also be used to create tuples with a single element. However, the element must be followed with a comma to indicate that it is a tuple indeed.

Consider the given two examples so that you can better understand the concept. Here, we can see the entry of element with and without a trailing comma.

                    

tuple1 = ( ‘apple’)
print (type(tuple1))

tuple1 = (‘apple’, )
print (type(tuple1))

Output:

< class ‘str’>

<class ‘tuple’>

 

Thus we can observe that an element with a trailing comma is a tuple. On the other hand, an element without a trailing comma is a string.

Repetition in a Tuple

We may repeat the same item in a tuple() python

Example:

                    

tuple2 = ( ‘apple’, ) * 3

print ( tuple2 )

Output:

( ‘apple’, ‘apple’, ‘apple’ )

Indexing in tuple() Python

Indexing is a process to access the tuple elements. These can be normal indexing or negative indexing. 

By using the index operator [], we can access elements in a tuple. The indexing of elements includes assigning a number to each element starting from 0 and then 1, 2, 3, and so on.

This will be clear with the given example:

                    

tuple_one = ( ‘python’ , ‘ lessons’ , ‘basics’ )

print (tuple_one[1])

Output:

‘lessons’

You may now understand negative indexing to access tuple elements. Tuple() python allows for negative indexing with -1 indexed for the last element, -2 indexed for the second-last element, and so on.

                    

tuple_two = ( ‘p’ , ‘q’ , ‘r’ , ‘s’ )

print ( tuple_two [-1] )

print ( tuple_two [-3] )



Output:

‘s’

‘q’

FAQs for tuple() Python

Q1: What is a tuple in Python with an example?

A1: A tuple() python refers to a data type that stores the collection of data. Thus the tuple() may contain no, one, or more elements. Thus, they maintain a specific order. Moreover, the tuple is immutable. As a result, tuples do not change after the initial assigning of elements. 

For example,

                    

tuple1 = ( ‘apple’, ‘mango’, ‘kiwi’ )

print (tuple1 )



Output: ‘apple’, ‘mango’, ‘kiwi’ 

Q2: What is the tuple method?

A2: The tuple method refers to operations on a tuple like counting the number of times an item appears in the tuple or searching for the index concerning a specific item. As a result, the tuple methods return some specific results. Therefore, they are significant for programming.

Q3: What are the tuple methods in Python?

A3: There are only two built-in methods that can be used on a tuple. This is because methods that can add or remove items are not available in the tuple.

Since tuple() is immutable, therefore, it does not change after the initial assigning of items. The methods used on a tuple are count() and index().

Count() – This method returns the number of times a specific value occurs in a tuple

index() – This method searches the tuple for a specific value after which it returns the position of the specific value.

Q4: How do you input a tuple in Python?

A4:  You can input a tuple in Python by separating the various items with commas and enclosing them within parenthesis. As a result, the tuple() will store the collection of items.

Example:

                    

my_tuple = ( ‘flower’ , 1, ‘yellow’ )

print ( my_tuple )

                    

Output:

‘Flower’, 1, ‘yellow’

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.