Tue. Mar 19th, 2024

Python for Beginners: When and How to Use Tuples

Python is a popular programming language

In recent times, I think you have seen the demand for the Python programming language. It is one of the popular programming languages which is being used world wide for the rapid development of web and GUI applications. To gain a deeper understanding on Python and tuples, let’s begin this article to learn about how to use and when to use tuples. You can also get trained and certified in the Python training in Chennai to be capable of clearing the Python certification exam.

What is Python?

As mentioned earlier, python is the common programming language which offers dynamic typing and helps in building and developing the applications efficiently. It is simply called a powerful programming language and is very easy to learn when compared to the other programming languages. Python provides its extensive support through the libraries and also helps in integrating with the web services easily.

What is a Tuple?

A tuple is referred to as an immutable sequence of items or variables represented in an order. You also need to know that these items cannot be changed and also new variables or items cannot be added once the sequence is created and defined. A tuple is represented using parentheses and the items are separated by commas.

Process of Creation of Python Tuple:

As infomed, a tuple is represented using parentheses which is capable of holding different data types that are separated by commas.

Below represented is the syntax used for tuple.

Syntax: tuplename = (“element1”, “element2”, “element3”) Let’s think that you are defining the variables for the names

name1 = “rohith”

name2 = “deepak”

name3 = “bhanu”

name4 = “manoj”

A tuple can be created as follows:

Print (name1, name2, name3, name4)

A tuple can be represented as below with the list of the names. name = [‘rohith’,’deepak’,’bhanu’,’manoj’]

The tuple can be printed as represented below. <i>print (name[0], name[1], name[2], name[3]) </i>

Different types of Tuples:

There are different types of tuples based on the value that you are inputting in the tuple during its creation.

Let’s discuss about different tuples representation:

//Tuple with String

City = (‘Bangalore”, “Chennai”, “Delhi”, “Hyderabad”)

print(City)

//Tuple with Integers

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

print(Num)

//Tuple with mixed datatypes

Mixed_tuple = (“Hyderabad”, “Delhi”, 1, 2, 3)

print(Mixed_tuple)

How to access Tuple elements?

This part is a bit tricky and let’s understand more here.

Let us create a basic tuple first.

abc_tuple = (‘A”, ‘B’, ‘C’, ‘D’)

Once you issue the command this way

print(abc_tuple[1])

The output will be : B

In python, the position will start from 0, from 0, 1, 2, 3 and so on.

Let us begin with a nested tuple.

ABC_tuple = (“Chennai”, “Delhi”, [ 5,6, 7, 8, 9, 10])

For a nested tuple, the positions will go this way.

Tuple 0 = Chennai

Tuple 1 = Delhi

Tuple 3 = 5, 6, 7, 8, 9, 10

Let us say that you want to print the third element in the tuple like letter e in Chennai, then the syntax will be represented as

print(ABC_tuple[0][3])

How to add elements to an already created tuple?

You need to know that you will not be able to add or change already created in a tuple. If you try to do the same, an error message will occur.

You can modify the elements inside the tuple as represented below.

Syntax: tuple_name [index of list in the tuple] [index of element inside a list]

Example : nest_tuple = (“Hyderabad”, “Bangalore”, [5, 6, 7] , (“Maharashtra”, “Shimla”)) nest_tuple[2][0]=9

print(nest_tuple)

How to delete the elements from a tuple?

It is not possible to delete the items from the tuple directly which reveals an error message. This takes place because the remove() method is not a tuple attribute

How to join tuples?

Two tuples can be joined using a + operator.

Syntax: tuple_a + tuple_b

City = (“Delhi”, “Bangalore’, “Pune”, “Chennai”)

State = (“Rajasthan”, “Karnataka”, “Gujarat”)

Multi = city+state

print(newtuple)

Differences between tuples and list:

Below listed are the differences between a list and a tuple.

Lists are mutable while tuples are immutable.

Lists come up with a variable length while tuples are included within a fixed length. Lists utilize more memory when compared to tuples.

Tuple operations are safe while list operations are error prone.

Conclusion:

It is easy and convenient to use the python tuples while trying to create and access the elements in programming. It is important that you gain an idea on how to create and access the tuple along with the nested tuples. This will definitely help you while dealing with real-time development of the applications in the long run. Get kickstarted with a prosperous career by getting in-depth knowledge in Python through the professional Python course training and certification process. All the best!

By StatusMessagesQuotes

StatusMessagesQuotes is one of the most new popular blog of latest trends, news & update on World, Business, Technology, Education, Health, Sports, Travel, And More. We Welcome Budding Writers And Established Authors Who Want To Submit A Guest Post For Tech, Health, Education, Finance, Photography, Shopping Real Estate SEO, Digital Marketing.

Related Post

error: Content is protected !!