Python Tuples

Tuples are another fundamental Python data structure.

They allow you to create immutable groups of objects. This means that once a tuple is created, it can’t be modified. You can’t add or remove items.

They are created in a way similar to lists, but using parentheses instead of square brackets:

names = ("Roger", "Syd")

A tuple is ordered, like a list, so you can get its values referencing an index value:

names[0] # "Roger"
names[1] # "Syd"

You can also use the index() method:

names.index('Roger') # 0
names.index('Syd')   # 1

As with strings and lists, using a negative index will start searching from the end:

names[-1] # True

You can count the items in a tuple with the len() function:

len(names) # 2

You can check if an item is contained into a tuple with the in operator:

print("Roger" in names) # True

You can also extract a part of a tuple, using slices:

names[0:2] # ('Roger', 'Syd')
names[1:] # ('Syd',)

Get the number of items in a tuple using the len() global function, the same we used to get the length of a string:

len(names) #2

You can create a sorted version of a tuple using the sorted() global function:

sorted(names)

You can create a new tuple from existing tuples using the + operator:

newTuple = names + ("Vanille", "Tina")


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Tuples are another fundamental Python data structure.

They allow you to create immutable groups of objects. This means that once a tuple is created, it can’t be modified. You can’t add or remove items.

They are created in a way similar to lists, but using parentheses instead of square brackets:

names = ("Roger", "Syd")

A tuple is ordered, like a list, so you can get its values referencing an index value:

names[0] # "Roger"
names[1] # "Syd"

You can also use the index() method:

names.index('Roger') # 0
names.index('Syd')   # 1

As with strings and lists, using a negative index will start searching from the end:

names[-1] # True

You can count the items in a tuple with the len() function:

len(names) # 2

You can check if an item is contained into a tuple with the in operator:

print("Roger" in names) # True

You can also extract a part of a tuple, using slices:

names[0:2] # ('Roger', 'Syd')
names[1:] # ('Syd',)

Get the number of items in a tuple using the len() global function, the same we used to get the length of a string:

len(names) #2

You can create a sorted version of a tuple using the sorted() global function:

sorted(names)

You can create a new tuple from existing tuples using the + operator:

newTuple = names + ("Vanille", "Tina")


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2020-12-19T05:00:00+00:00) Python Tuples. Retrieved from https://www.scien.cx/2020/12/19/python-tuples/

MLA
" » Python Tuples." flaviocopes.com | Sciencx - Saturday December 19, 2020, https://www.scien.cx/2020/12/19/python-tuples/
HARVARD
flaviocopes.com | Sciencx Saturday December 19, 2020 » Python Tuples., viewed ,<https://www.scien.cx/2020/12/19/python-tuples/>
VANCOUVER
flaviocopes.com | Sciencx - » Python Tuples. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/12/19/python-tuples/
CHICAGO
" » Python Tuples." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2020/12/19/python-tuples/
IEEE
" » Python Tuples." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2020/12/19/python-tuples/. [Accessed: ]
rf:citation
» Python Tuples | flaviocopes.com | Sciencx | https://www.scien.cx/2020/12/19/python-tuples/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.