Python Tuples

A tuple is a set of Python subtracted items by room. In the past in the same list as the marks, the product and repetitive feedback but not anonymous as a list.




Definition of Tuple:

In order to determine a plunger, we only force one single variable with a quartet value and tuple variables.
You can see the old example, the variable myTuple is actually one of the 1, 2, 3 and 4 dots. Also, keep the circle displayed in print, in whole numbers, this helps to sort out the list of lists, because if on a list, we have a list of the listings.

Creating Tuples:


# An empty tuple
empty_tuple = ()
print (empty_tuple)
Run on IDE
Output:
 ()
# Creating non-empty tuples
  
# One way of creation
tup = 'python', 'geeks'
print(tup)
  
# Another for doing the same
tup = ('python', 'geeks')
print(tup)
Run on IDE

Output:


('python', 'geeks')
('python', 'geeks')

Concatenation of Tuples:


# Code for concatenating 2 tuples
tuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'geek')
# Concatenating above two
print(tuple1 + tuple2)
Run on IDE

Output:


(0, 1, 2, 3, 'python', 'geek')

 Nesting of Tuples:


# Code for creating nested tuples
tuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'geek')
tuple3 = (tuple1, tuple2)
print(tuple3)
Run on IDE

Output:


((0, 1, 2, 3), ('python', 'geek'))

 Repetition in Tuples:


# Code to create a tuple with repetition
tuple3 = ('python',)*3
print(tuple3)
Run on IDE
Output
 ('python', 'python', 'python')
Try the above without a comma and check. You will get tuple3 as a string ‘pythonpythonpython’.

 Immutable Tuples:


#code to test that tuples are immutable
tuple1 = (0, 1, 2, 3)
tuple1[0] = 4
print(tuple1)
Run on IDE
Output
Traceback (most recent call last):
  File "e0eaddff843a8695575daec34506f126.py", line 3, in
   tuple1[0]=4
TypeError: 'tuple' object does not support item assignment

 Slicing in Tuples:


# code to test slicing
tuple1 = (0 ,1, 2, 3)
print(tuple1[1:])
print(tuple1[::-1])
print(tuple1[2:4])
Run on IDE
Output
(1, 2, 3)
(3, 2, 1, 0)
(2, 3)

 Deleting a Tuple:


# Code for deleting a tuple
tuple3 = ( 0, 1)
del tuple3
print(tuple3)
Run on IDE

Error:


Traceback (most recent call last):
 File "d92694727db1dc9118a5250bf04dafbd.py", line 6, in <module>
       print(tuple3)
NameError: name 'tuple3' is not defined

Output:


(0, 1)

 Finding Length of a Tuple:


# Code for printing the length of a tuple
tuple2 = ('python', 'geek')
print(len(tuple2))
Run on IDE

Output:


2

Converting a list to a Tuple:


# Code for converting a list and a string into a tuple
list1 = [0, 1, 2]
print(tuple(list1))
print(tuple('python')) # string 'python'
Run on IDE

Output:


(0, 1, 2)
('p', 'y', 't', 'h', 'o', 'n')
Takes a single parameter which may be a list,string,set or even a dictionary( only keys are taken as elements) and converts them to a tuple.

Tuples in a loop:


#python code for creating tuples in a loop
  tup = ('geek',)
n = 5  #Number of time loop runs
for i in range(int(n)):
    tup = (tup,)
    print(tup)
Run on IDE

Output:


(('geek',),)
((('geek',),),)
(((('geek',),),),)
((((('geek',),),),),)
(((((('geek',),),),),),)

 Using cmp(), max() , min():


# A python program to demonstrate the use of 
# cmp(), max(), min()
  tuple1 = ('python', 'geek')
tuple2 = ('coder', 1)
  if (cmp(tuple1, tuple2) != 0):
    # cmp() returns 0 if matched, 1 when not tuple1 
    # is longer and -1 when tuple1 is shoter
    print('Not the same')
else:
    print('Same')
print ('Maximum element in tuples 1,2: ' + 
        str(max(tuple1)) +  ',' + 
        str(max(tuple2)))
print ('Minimum element in tuples 1,2: ' + 
     str(min(tuple1)) + ','  + str(min(tuple2)))
Run on IDE

Output:


Not the same
Maximum element in tuples 1,2: python,coder



Comments

  1. It has been simply incredibly generous with you to provide openly what exactly many individuals wouldíve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    python training in chennai | best python training in chennai |
    python course in chennai

    ReplyDelete
  2. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage contribution from other ones on this subject while our own child is truly discovering a great deal. Have fun with the remaining portion of the year.

    matlab Training in chennai | matlab training class in chennai | matlab course in chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Python – Functions

What is Django in python?

Python Shallow Copy and Deep Copy