0% found this document useful (0 votes)
23 views3 pages

06 Tuples

Tuples are similar to lists but are immutable, meaning their elements cannot be changed. Tuples use parentheses instead of brackets and can contain mixed data types. They are indexed and sliced like lists but have fewer built-in methods since they cannot be modified. Tuples are useful when immutability is needed, like to ensure passed objects remain unchanged.

Uploaded by

sandip kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

06 Tuples

Tuples are similar to lists but are immutable, meaning their elements cannot be changed. Tuples use parentheses instead of brackets and can contain mixed data types. They are indexed and sliced like lists but have fewer built-in methods since they cannot be modified. Tuples are useful when immutability is needed, like to ensure passed objects remain unchanged.

Uploaded by

sandip kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/25/2019 06-Tuples

Tuples
In Python tuples are very similar to lists, however, unlike lists they are immutable meaning they can not be
changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or
dates on a calendar.

In this section, we will get a brief overview of the following:

1.) Constructing Tuples


2.) Basic Tuple Methods
3.) Immutability
4.) When to Use Tuples

You'll have an intuition of how to use tuples based on what you've learned about lists. We can treat them very
similarly with the major distinction being that tuples are immutable.

Constructing Tuples
The construction of a tuples use () with elements separated by commas. For example:

In [1]:

# Create a tuple
t = (1,2,3)

In [2]:

# Check len just like a list


len(t)

Out[2]:

In [3]:

# Can also mix object types


t = ('one',2)

# Show
t

Out[3]:

('one', 2)

In [4]:

# Use indexing just like we did in lists


t[0]

Out[4]:

'one'

localhost:8888/nbconvert/html/Desktop/Complete-Python-3-Bootcamp-master/00-Python Object and Data Structure Basics/06-Tuples.ipynb?dow… 1/3


7/25/2019 06-Tuples

In [5]:

# Slicing just like a list


t[-1]

Out[5]:

Basic Tuple Methods


Tuples have built-in methods, but not as many as lists do. Let's look at two of them:

In [6]:

# Use .index to enter a value and return the index


t.index('one')

Out[6]:

In [7]:

# Use .count to count the number of times a value appears


t.count('one')

Out[7]:

Immutability
It can't be stressed enough that tuples are immutable. To drive that point home:

In [8]:

t[0]= 'change'

--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
<ipython-input-8-1257c0aa9edd> in <module>()
----> 1 t[0]= 'change'

TypeError: 'tuple' object does not support item assignment

Because of this immutability, tuples can't grow. Once a tuple is made we can not add to it.

localhost:8888/nbconvert/html/Desktop/Complete-Python-3-Bootcamp-master/00-Python Object and Data Structure Basics/06-Tuples.ipynb?dow… 2/3


7/25/2019 06-Tuples

In [9]:

t.append('nope')

--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
<ipython-input-9-b75f5b09ac19> in <module>()
----> 1 t.append('nope')

AttributeError: 'tuple' object has no attribute 'append'

When to use Tuples


You may be wondering, "Why bother using tuples when they have fewer available methods?" To be honest,
tuples are not used as often as lists in programming, but are used when immutability is necessary. If in your
program you are passing around an object and need to make sure it does not get changed, then a tuple
becomes your solution. It provides a convenient source of data integrity.

You should now be able to create and use tuples in your programming as well as have an understanding of
their immutability.

Up next Sets and Booleans!!

localhost:8888/nbconvert/html/Desktop/Complete-Python-3-Bootcamp-master/00-Python Object and Data Structure Basics/06-Tuples.ipynb?dow… 3/3

You might also like