From 1db4fe2cfe63a9199eb84f8c0ee6daff6efa41dc Mon Sep 17 00:00:00 2001 From: decorator-factory <42166884+decorator-factory@users.noreply.github.com> Date: Sun, 17 May 2020 13:30:23 +0300 Subject: Change standalone programs to interactive sessions --- bot/resources/tags/mutability.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/bot/resources/tags/mutability.md b/bot/resources/tags/mutability.md index b37420fc7..8b98da43a 100644 --- a/bot/resources/tags/mutability.md +++ b/bot/resources/tags/mutability.md @@ -4,9 +4,11 @@ Imagine that you want to make all letters in a string upper case. Conveniently, You might think that this would work: ```python -string = "abcd" -string.upper() -print(string) # abcd +>>> string = "abcd" +>>> string.upper() +'ABCD' +>>> string +'abcd' ``` `string` didn't change. Why is that so? @@ -14,8 +16,10 @@ print(string) # abcd That's because strings in Python are _immutable_. You can't change them, you can only pass around existing strings or create new ones. ```python -string = "abcd" -string = string.upper() +>>> string = "abcd" +>>> string = string.upper() +>>> string +'ABCD' ``` `string.upper()` creates and returns a new string which is like the old one, but with all the letters turned to upper case. @@ -24,9 +28,10 @@ string = string.upper() Mutable data types like `list`, on the other hand, can be changed in-place: ```python -my_list = [1, 2, 3] -my_list.append(4) -print(my_list) # [1, 2, 3, 4] +>>> my_list = [1, 2, 3] +>>> my_list.append(4) +>>> my_list +[1, 2, 3, 4] ``` Other examples of mutable data types in Python are `dict` and `set`. -- cgit v1.2.3