• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Are Tuples Mutable in Python? No, Tuples are not Mutable

Are Tuples Mutable in Python? No, Tuples are not Mutable

July 8, 2022 Leave a Comment

In Python, there are mutable and inmutable data types. Mutable data types can be changed after it has been created. Inmutable data types cannot be changed after they have been created.

Are tuples mutable in Python?

No, tuples are not mutable in Python and are inmutable.

With tuples, we cannot add or remove elements of a tuple, or change the tuple’s items.

For example, let’s say we have the following tuple.

t = (0, 1, 2)

There are no tuple methods which allow us to add or remove elements, such as append() or pop(), like other data types have.

Also, if you try to change an item in a tuple, then you will get the following TypeError.

t = (0, 1, 2)

t[0] = 2

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

Given these properties of tuples, we’ve now know that tuples are not mutable.

Examples of Mutable Data Types in Python

Some data types which are mutable in Python include lists, dictionaries, sets and user-defined classes.

For example, with lists, we can add items to a list, remove items from lists, and easily change the items in a list.

Dictionaries are also mutable because we can add new key/value pairs, remove existing key/value pairs, change the name of the keys and change existing values.

Basically, if an object can be change over time, then it is mutable. If it cannot change, then it is inmutable.

Hopefully this article has been useful for you to learn that tuples are not mutable and about mutable data types in Python.

Other Articles You'll Also Like:

  • 1.  Difference Between read(), readline() and readlines() in Python
  • 2.  Find Quotient and Remainder After Division in Python
  • 3.  Comparing Datetimes in Python
  • 4.  Selenium maximize_window() Function to Maximize Window in Python
  • 5.  Get Substring from String in Python with String Slicing
  • 6.  Python Replace Space with Underscore Using String replace() Function
  • 7.  Difference Between // and / When Dividing Numbers in Python
  • 8.  Python power function – Exponentiate Numbers with math.pow()
  • 9.  Zip Two Lists in Python
  • 10.  Python not in – Check if Value is Not Included in Object

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy