Python Tuples

Tuple is an unusual product of Python. The thieves are diagrams, such as the list. The difference between burglary and lists, which can not be changed, such as lists and burglars that use the media, and make a list of shoulders.

What is Tuple in Python?

Tuple is similar to the list of substances that are inaccurate. The difference between the list and the tuple list is specified in a mixed tape and can be modified by tuple showing that the cards can not be changed. However, you can take part in your thieves as a new thief.

Enroll Now: Python Training in Chennai

Tuple Syntax

Tup = ('Jan','feb','march')

Python Tuple Example

>>> data=(10,20,'ram',56.8)

>>> data2="a",10,20.9

>>> data

(10, 20, 'ram', 56.8)

>>> data2

('a', 10, 20.9)

>>>

Tuple Assignment

Tuple Python shows that the work takes longer than one variable. Here, we have included a copy and information such as the name of the person, last name, date of birth, and 2 and other tuple values ​​such as numbers (1,2,3, …., 7).

Enroll Now: Python Training in Bangalore

For example,
(Name, last name, date of birth, favorite film and one year, skill, place of birth) Robert
Here is the law,

tup1 = ('Robert', 'Carlos','1965','Terminator 1995', 'Actor','Florida');

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

print(tup1[0])

print(tup2[1:4])
  1. Tuple 1 contains the Robert List
  2. Tuple 2 includes a list of numbers
  3. We call the price [0] for lunch and 2 burgers that we call the price between 1 and 4
  4. Actual Implementation – This gives Robert the first name of the genius compared to both (2,3 and 4)

Creating a Tuple

Tuple is made by placing all elements in the space of two (), separating them from the symbol. Cage is an option, but it is a good writing.
Tuple can have any number of items and can be different types (all, flow, lists, symbols, etc.).

# empty tuple

# Output: ()

my_tuple = ()

print(my_tuple)

# tuple having integers

# Output: (1, 2, 3)

my_tuple = (1, 2, 3)

print(my_tuple)

# tuple with mixed datatypes

# Output: (1, "Hello", 3.4)

my_tuple = (1, "Hello", 3.4)

print(my_tuple)

# nested tuple

# Output: ("mouse", [8, 4, 6], (1, 2, 3))

my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

print(my_tuple)

# tuple can be created without parentheses

# also called tuple packing

# Output: 3, 4.6, "dog"

my_tuple = 3, 4.6, "dog"

print(my_tuple)

# tuple unpacking is also possible

Output:

# 3

# 4.6

# dog

a, b, c = my_tuple

print(a)

print(b)

print(c)

Changing a Tuple

Unlike the list, the thieves are not able to.
This means that the tuple substances can not be changed when placed. But, if the self itself is the kind of information that is available on the list, nested items can be changed.
We can also do different values ​​that can be stamped (modified).

my_tuple = (4, 2, 3, [6, 5])

# we cannot change an element

# If you uncomment line 8

# you will get an error:

# TypeError: 'tuple' object does not support item assignment

#my_tuple[1] = 9

# but item of mutable element can be changed

# Output: (4, 2, 3, [9, 5])

my_tuple[3][0] = 9

print(my_tuple)

# tuples can be reassigned

# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

my_tuple = ('p','r','o','g','r','a','m','i','z')

print(my_tuple)

We can use + the company to combine two burglars. This is also called a combination.
We can also repeat a string of symbols using the operator *.
Works both + and * new tuple symbols.

# Concatenation

# Output: (1, 2, 3, 4, 5, 6)

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

# Repeat

# Output: ('Repeat', 'Repeat', 'Repeat')

print(("Repeat",) * 3)

Deleting the Tuple

As mentioned above, we can not change the contents of the tube. This means that we are unable to delete or delete any of the burglars.
But tuple completely eliminates the possibility of using the keyword.

my_tuple = ('p','r','o','g','r','a','m','i','z')

# can't delete items
 # if you uncomment line 8,
 # you will get an error:
 # TypeError: 'tuple' object doesn't support item deletion

#del my_tuple[3]

# can delete entire tuple
 # NameError: name 'my_tuple' is not defined
 del my_tuple
 my_tuple

The benefits of Tuple Over List

Because, thieves are similar to a list, both in such cases.
However, there are special advantages to implement a more tuple list. Here are some key benefits:

  1. Generally, we use the different types of data (types) of data and types of data that are integrated into the same (same).
  2. Because the thieves are inaccessible, sank slows faster than the list. Therefore, there is a slight performance increase.
  3. The apples in the equipment may be used in the language key. On the list, this is not possible.
  4. If you have unnecessary data, do the same as tuple to make sure that it protects.

To getting expect level training for Python training in your Location – Python Training in Chennai | Python Training in Bangalore | Python Training in Pune | Python Training institute in Chennai Python Interview Questions | Python Course in Chennai

Leave a Reply

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