Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Mutable or Immutable? | Getting Familiar With Numbers in Python
Data Types in Python

Glissez pour afficher le menu

book
Mutable or Immutable?

In Python, data types fall into two categories: mutable and immutable.

  • Mutable types can be changed after they are created;
  • Immutable types cannot be changed after creation — any "change" creates a brand new object in memory.

Immutable Types

Types like int, float, str, and tuple are immutable. That means if you "change" a variable holding one of these types, Python actually creates a new object under the hood.

123456789
# Assign an integer value to var1 var1 = 40 print("var1 =", var1) # Output: var1 = 40 print("ID of var1:", id(var1)) # Shows the memory ID of the value 40 # Reassign a new integer value to var1 var1 = 50 print("var1 =", var1) # Output: var1 = 50 print("New ID of var1:", id(var1)) # Shows a different ID — it's a new object in memory
copy

Even though we're reusing the same variable name (var1), the id() function shows that the variable is pointing to a completely new object after reassignment. That’s because integers are immutable — they cannot be modified in-place.

Mutable Types

On the other hand, types like list and dict are mutable. They can be changed without creating new objects.

Tâche

Swipe to start coding

Imagine you are managing a budget for a small project. Initially, your available budget is set to $100. Later, you receive an additional funding of $50.

Your task is to:

  1. Print the initial value of project_budget and its memory ID using the id() function.
  2. Update the value of project_budget to reflect the total amount.
  3. Print the updated value and its new ID.

This will help you see how immutable types like int behave when reassigned.

Once you've completed this task, click the button below the code to check your solution.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

Awesome!

Completion rate improved to 3.03

book
Mutable or Immutable?

In Python, data types fall into two categories: mutable and immutable.

  • Mutable types can be changed after they are created;
  • Immutable types cannot be changed after creation — any "change" creates a brand new object in memory.

Immutable Types

Types like int, float, str, and tuple are immutable. That means if you "change" a variable holding one of these types, Python actually creates a new object under the hood.

123456789
# Assign an integer value to var1 var1 = 40 print("var1 =", var1) # Output: var1 = 40 print("ID of var1:", id(var1)) # Shows the memory ID of the value 40 # Reassign a new integer value to var1 var1 = 50 print("var1 =", var1) # Output: var1 = 50 print("New ID of var1:", id(var1)) # Shows a different ID — it's a new object in memory
copy

Even though we're reusing the same variable name (var1), the id() function shows that the variable is pointing to a completely new object after reassignment. That’s because integers are immutable — they cannot be modified in-place.

Mutable Types

On the other hand, types like list and dict are mutable. They can be changed without creating new objects.

Tâche

Swipe to start coding

Imagine you are managing a budget for a small project. Initially, your available budget is set to $100. Later, you receive an additional funding of $50.

Your task is to:

  1. Print the initial value of project_budget and its memory ID using the id() function.
  2. Update the value of project_budget to reflect the total amount.
  3. Print the updated value and its new ID.

This will help you see how immutable types like int behave when reassigned.

Once you've completed this task, click the button below the code to check your solution.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

close

Awesome!

Completion rate improved to 3.03

Glissez pour afficher le menu

some-alt