Python tuple – Share with care of data!

Image credit: trytoprogram.com

Recently, I have to share data with someone. But I have a fear of a manipulation of data.

So, I’m thinking about what should I do?

Finally, after some research, I have found that I can share with the care of my data using python tuple.

You can protect your data with python tuple.

Your next question is “what is a tuple in python?”

Let’s see what it is?

Python tuple

1.Tuple

A tuple is an ordered and unchangeable data type in python.

It is used to store the unchangeable data items in python.

2.How to create a tuple?

You can create a tuple with the help of (). The items are going to store inside the ().

Let’s see how can you do?

tuple of names

3.Tuple elements

In this section, you are going to learn:-

•Ordered
•Unchangable
•Allow duplicates
•Tuple with one item
•Tuple length
•Data type of tuple item

1) Ordered

Image credit:booster.io

When it comes to ordering. What do you understand by the order of python tuple?

In the case of a tuple, it’s the same as a list. Order means indexing of tuple items. The position of the tuple item.

The indexing of tuple item denoted by []. Let’s see how can you see it!

names are in order

The first name shows the order

2) Unchangeable (Immutable)

Immutable tuple
Image credit:radar.oreilly.com

What do you understand by unchanged tuple?

It means you can’t remove, add or change the size of the tuple. Tuple has a fixed size.

Try the append method

The output shows the fixed size of the tuple.

The same thing will happen for the remove method. That shows tuple is unchangeable.

Q. Why tuple is immutable?

Ans:- 1) Tuples are following the same sequential operation as a string. 2) Tuples are used for the order of data that’s why the tuple is immutable.

3) Tuple – Allow duplicates

Duplicate item
image credit:istockphoto.com

Suppose you need a duplicates item in a tuple. So, what you will do?

You will simply put the duplicate items. Let’s see how can you put duplicate items in a tuple?

Created duplicate items

“vipul” has come two times

4) Tuple items – Data type

Data type: tuple

In python programming, everything is an object. Suppose, you have to find the data type of tuple items. What you will do?

You can use the type() method.

type() method:-

With help of type(), you can find the data type of tuple items.

Let’s see how can you do!

type() method

type ‘tuple’

5) tuple() Constructor

tuple constructor
Image credit: codespeedy.com

Suppose you have stored your data as a list and you want to change it as a tuple. How will you do it?

You will use tuple() constructor. Let’s see how can you do!

Example:-

tuple created

6) Tuple length

len() method
Image credit: master programming.com

Suppose you have to find the length of the tuple or items present in the tuple. You will use the len() method in the tuple.

Let’s see how can you do!

Example:-

Number of items

7) Tuple with one item

Suppose you have one item in a tuple.

How will you represent one item in a tuple?

If you only represent with (“vipul”) then the data type of it’s become string.

Let’s see how it’s going to see!

Example:-

Data type ‘str’

The right approach to do this is using a comma after first item(“vipul”,). You will see the data type has changed to ‘tuple’.

You Can code:-

Example:-

Type ‘tuple’

In this part, you have seen how comma(,) is changing the data type.

A comma at the end of the item shows the data type ‘tuple’.

You have seen ordered, unchangeable, duplicates, length and type of tuple.

Also, shown why we used a tuple!

Tuple has interesting behaviour of privacy of data.

You can use a tuple to make the data as secure as you want!

Conclusion:-

Tuple and list have many similarities. But some differences make a tuple faster than a list.

Because of fixed size tuple taking less memory than a list.

Tuple’s unchangeable behaviour makes tuple use in sharing data.

Leave a comment