28/05/2023
python tuples

basic to advacne python tuples

No exemplary definition is needed if you wish to secure a web application and must need to know python tuple functions and operations strategy.

In this article, you will learn basic to advanced python tuples and its working area. Read attentively, you must benefit from this article.

Key Points

# definition of tuples # creating tuple

# change and add elements to tuples # how to join tuples

# delete tuples # update tuples # using loop with tuples

# parking an unpacking tuples # tuples methods # Advantages of tuples

Definition of python Tuples

Tuple is an immutable list that cannot be changed. If you create a list of items, it cannot be changed by program. Tuple allows you to help change this type of item.

Tuple is the store for multiple items. 

Here 4 types of data include tuples, lists, sets and dictionaries. All storage data value must be different and unchangeable.

Tuples lists work parentheses () and instances of square brackets [].

Example of tuples –

servicestuple = (“web development”, “SEO”, “Graphic Design”)

print(servicestuple)

How to create a tuple

A tuple is created by parentheses () and inverted commas. Here I will share how to create a tuple using different kinds of variables.

You can create tuples using 5 methods. These are the below lists.

  • Empty tuple – create this type tuple using empty() function, opening and closing parentheses. 

Example of –

emptyTuple = ()

  • Single elements with tuples – if you want to create a tuple using 1 element, here you need to add a comma. Like-

# a1 is a tuple

a1= (4.12, )

print(type(a1) )

# prints

< class ‘tuple’ >

  • Different data types with tuples – when you want to create tuples using any kind of data type, at that point you need to use this system.

Example of this –

Tup2 = (‘SAT’, True, 2.4, 78, [4, 5, 6])

# prints

(‘SAT’, True, 2.4, 78, [4, 5, 6 ] )

  • tuple() contractor with tuple – you also created tuples by using tuple() contractor.

Example of this tuples-

tup2 = tuple((4, ,5, 6) )

print(tup2)

# prints

(4, 5, 6 )

  • Nested tuple – if you want to nest inside to other tuples you can do it easily by using nested tuples. 

Example of nested tuple –

tup2= (4, 5, 6)

tup 3= (‘Anik’, tup2, 70 )

print(tup3)

#prints

(‘Anik’, (4, 5, 6), 70)

How to change and add elements to python tuples

No one can add or change the items in a tuple because it is unchangeable and immature. If you attempt to change or add some items, you see an error message like this –

But you can access tuples to convert them into lists. Here you can add, modify, delete, and more tuples. Also you can change the elements of the list by using tuple_name

Example of this –

newtuple = (“Bangladesh”, “India”, “USA”)

p = list(newtuple)

p.append(“UK”)

newtuple = tuple(p)

print(newtuple)

Output-

How to join python tuples

If you want to add two or more tuples at a time, you can use the ( + ) operators to complete this process.

Example of join tuples-

tuplex = (“Web development”, “Digital Marketing” , “Content Writting”)

tupley = (100, 200, 300)

tuplez = tuplex + tupley

print(tuplez)

Output-

Delete process of  python tuples

Since tuples are unchangeable, it is not possible to delete any kinds of items. So that we cannot delete elements directly from tuples. Tuple doesn’t use remove() attribute in this method. 

But you use the same trick to convert tuples from a list then you are able to delete these items. You can do this by using the del keyword.

Example of join tuples –

 tup2= (4, 5, 6)

del tup2

Update system of python tuples

Once you create a tuple then you cannot change anything. But you will be able to change or update anything when you convert tuples from lists. 

Example of convert to tuples from lists –

a = (“bangladesh”, “Pakistan”, “India”)

b = list(a)

b[1] = “USA”

a = tuple(b)

print(a)

Output –

Using process of looping tuples

When you want to represent a python tuple again, at this point you can use loop statements.

If you want to declare a tuple, just use a for loop statement.

Example of this loop-

newtuple = (“web development”, “SEO”, “Graphic Design”)

for a in newtuple:

  print(a)

Output – 

Also you can loop through the items by using while loop (). Here you need to use the len() function to determine the length of tuples. Then you need to press 0 to start. 

// Note: increase the index number by 1.

Example while loop-

servicestuple = (“web development”, “SEO”, “Graphic Design”)

i = 0

while i < len(servicestuple):

print(servicestuple[i])

i = i + 1

Output –

How to work packing and unpacking tuples

When anyone creates a tuple, they basically assign a value of it  that is called packing tuples.

Example of packing tuples below-

services = (“web development”, “SEO”, “Graphic Design”)

print(services)

Output –

But, sometimes anyone allows in python to extract the value back in items that are called unpacking tuples. 

Example of unpacking tuples – 

services = (“web development”, “SEO”, “Graphic Design”)

(good, best, better) = services

print(good)

print(best)

print(better)

Output –

Methods of python tuples

Python tuples do not have many methods. It has two kinds of methods and uses these to complete every work perfectly.

Here include there are two types of methods below –

  1. count() – it provides the number of times a specified value.
  2. index() – find out specific values and return the position of it.

Example of these two methods of python that you can use on tuples –

new_tuple = (‘t’, ‘o’, ‘m’, ‘m’, ‘a’, ‘t’, ‘o’)

print(new_tuple.count(‘m’)) 

# output :2

print(new_tuple.index(‘1’))

# output: 5

Total output –

2

5

Advantages of python tuples

Here you can got some bullets points of core advantages:-

  • Tuples has fix size and elements
  • Tuples are super fast items
  • Its allow to output specific elements
  • Include here some index function
  • More comfort with other combine
  • Provide to access calculate the length of tuple
  • Touple safe code, other modifications and more.

Limitations of python tuples

  • Tuples can not provide access elements
  • Don’t use short in elements in tuples
  • Can’t remove or delete tuples

In short, through this content, I tried to present every detail in a simpler way for tuple python beginners to advance to levels that are very functional to you. Although there are some limitations, python tuples will help to ensure more secure web applications.

Do you have any questions feel free drop it in the comment box. Our experts will come back soon to help you.

Total concept of JSON in python

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Open chat
1
Hey, how can I help you?