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

Swipe um das Menü anzuzeigen

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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

close

Awesome!

Completion rate improved to 3.03

Swipe um das Menü anzuzeigen

some-alt