Change language

Tuples in Python

|

Creating Tuples

# Empty tuple

empty_tuple = ()

print (empty_tuple)

Output:

 () 

# Create non-empty tuples

 
# One way to create

tup = ’python’ , ’geeks’

print (tup)

  
# Another for the same

tup = ( ’python’ , ’geeks’ )

print (tup)

Output

 (’python’,’ geeks’) (’python’,’ geeks’) 

Note. If you are generating a single element tuple, be sure to add a comma after the element.

Tuple Concatenation

# Code to combine 2 tuples

 

tuple1 = ( 0 , 1 , 2 , 3 )

tuple2 = ( ’python’ , ’geek’ )

  
# About Combine above two

print (tuple1 + tuple2)

Output:

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

Tuple nesting

# Code to create nested tuples

 

tuple1 = ( 0 , 1 , 2 , 3 )

tuple2 = ( ’python’ , ’geek’ )

tuple3 = (tuple1, tuple2)

print (tuple3)

Output:

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

Repetition in tuples

Exit

 (’python’,’ python’, ’python’) 

Try above without comma and check ... You will get tuple3 as the string "pythonpythonpython".

Immutable Tuples

# Code to create a repetition tuple

 

tuple3 = ( ’python’ ,) * 3

print (tuple3 )

# code to check that tuples are immutable

 

tuple1 = ( 0 , 1 , 2 , 3 )

tuple1 [ 0 ] = 4

print (tuple1)

Exit

 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 check slicing

 

tuple1 = ( 0 , 1 , 2 , 3 )

print (tuple1 [ 1 :])

print (tuple1 [:: - 1 ])

print (tuple1 [ 2 : 4 ])

Exit

 (1, 2, 3) (3, 2, 1 , 0) (2, 3) 

Deleting a tuple

# Code to remove a tuple

  

tuple3 = ( 0 , 1 )

del tuple3

print (tuple3)

Error:

 Tracebac k (most recent call last): File "d92694727db1dc9118a5250bf04dafbd.py", line 6, in "module" print (tuple3) NameError: name ’tuple3’ is not defined 

Output:

 (0, 1) 

Find length tuple

# Code for printing tuple length

 

tuple2 = ( ’python’ , ’ geek’ )

print ( len (tuple2))

Exit

 2 

Convert list to tuple

# Code to convert i list and line in a tuple

 

list1 = [ 0 , 1 , 2 ]

print ( tuple (list1))

print ( tuple ( ’python’ )) # string & # 39; python & # 39;

Exit

 (0, 1, 2) (’p’,’ y’, ’t’,’ h’, ’o’,’ n’) 

Takes one parameter, which can be a list, string, set, or even a dictionary (only keys are accepted as elements) and converts them to a tuple.

Tuples in a loop

# python code to create tuples in a loop

 

tup = ( ’geek’ ,)

n = 5   # Number of time cycles

for i in range ( int (n)):

  tup = (tup,)

print (tup)

Output:

 ((’geek’,),) (((’ geek’,),),) ((((’geek’,),),),) (((((( ’geek’,),),),),) ((((((’ geek’,),),),),),) 

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

# Python program demonstrating usage
# cmp (), max (), min ()

 

tuple1 = ( ’python’ , ’geek’ )

tuple2 =  ( ’coder’ , 1 )

 

if ( cmp (tuple1, tuple2)! = 0 ):

 

# cmp () returns 0 if it matches, 1 if not tuple1

# longer and -1 when tuple1 is an arrow

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)))

Exit

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

Note: max () and min () are checked against ASCII values. If there are two lines in a tuple, then the first different characters in the lines are checked.

This article is courtesy of Sri Sanket Uppalapati. Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically