Mutability Immutability vs. Mutability Immutable

Mutability Immutability vs. Mutability Immutable
1 / 1
Mutability Immutability vs. Mutability Immutable vs. Mutable An immutable value is unchanging once created. Immutable types (that weve covered): int, float, string A mutable value can change in value throughout the course of computation.

Related Topics

Download this presentation From Below

"Mutability Immutability vs. Mutability Immutable" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

01
Mutability<br>
02
Immutability vs. Mutability<br>
03
Immutable vs. Mutable An immutable value is unchanging once created.
Immutable types (that we've covered): int, float, string




A mutable value can change in value throughout the course of computation. All names that refer to the same object are affected by a mutation.
Mutable types (that we've covered): list, dictionaries a_string = "Hi y'all"
a_string[1] = "I"
a_string += ", how you doing?"
an_int = 20
an_int += 2 # 🚫 Error! String elements cannot be set.
# 🤔 How does this work?

# 🤔 And this? grades = [90, 70, 85]
grades_copy = grades
grades[1] = 100 # [90, 70, 85]
# [90, 70, 85]
# grades=[90, 100, 85], grades_copy=[90, 100, 85]<br>