From 101684de2813bc64b3fb69cae8f52bfb81d63006 Mon Sep 17 00:00:00 2001 From: Vibhu Agarwal Date: Mon, 13 Aug 2018 11:12:51 +0530 Subject: [PATCH 1/3] Fixed input-output of 4th example explanation code Deep down we're all the same: Input-Output formatting of the explanation code https://github.com/satwikkansal/wtfpython#-deep-down-were-all-the-same- --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 92f515b..a6eb0ec 100644 --- a/README.md +++ b/README.md @@ -296,16 +296,16 @@ True * But why did the `is` operator evaluated to `False`? Let's see with this snippet. ```py class WTF(object): - def __init__(self): print("I ") - def __del__(self): print("D ") + def __init__(self): print("I", end = " ") + def __del__(self): print("D", end = " ") ``` **Output:** ```py >>> WTF() is WTF() - I I D D + I I D D False >>> id(WTF()) == id(WTF()) - I D I D + I D I D True ``` As you may observe, the order in which the objects are destroyed is what made all the difference here. From 9e624542cf79bd5bd869bc41073b0878a0e40bf8 Mon Sep 17 00:00:00 2001 From: Vibhu Agarwal Date: Sat, 25 Aug 2018 14:19:22 +0530 Subject: [PATCH 2/3] Fixed Output and reverted 4th example code Ref. example: Deep down, we're all the same --- README.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a6eb0ec..8f116bd 100644 --- a/README.md +++ b/README.md @@ -296,16 +296,24 @@ True * But why did the `is` operator evaluated to `False`? Let's see with this snippet. ```py class WTF(object): - def __init__(self): print("I", end = " ") - def __del__(self): print("D", end = " ") + def __init__(self): print("I") + def __del__(self): print("D") ``` **Output:** ```py >>> WTF() is WTF() - I I D D False + I + I + D + D + False >>> id(WTF()) == id(WTF()) - I D I D True + I + D + I + D + True ``` As you may observe, the order in which the objects are destroyed is what made all the difference here. From 4be7c2a7276006f0dc87c9ba290b2c9c486593d4 Mon Sep 17 00:00:00 2001 From: Vibhu Agarwal Date: Mon, 1 Oct 2018 02:38:14 +0530 Subject: [PATCH 3/3] Added Example: Sets discriminating values? --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/README.md b/README.md index 8f116bd..1555f63 100644 --- a/README.md +++ b/README.md @@ -1258,6 +1258,58 @@ False The built-in `ord()` function returns a character's Unicode [code point](https://en.wikipedia.org/wiki/Code_point), and different code positions of Cyrillic 'e' and Latin 'e' justify the behavior of the above example. +--- + + ### ▶ Sets discriminating values? * + + ```py + >>> st = set() + >>> st.add(5) + >>> st.add(5) + >>> st.add(10) + >>> st.add(10) + >>> st.add(10) + >>> st.add(20) + >>> st.add(2) + >>> st.add(345678) + >>> st1 = set(sorted(st)) + ``` + + **Output:** + ```py + >>> st + {2, 5, 10, 345678, 20} + >>> st1 + {2, 5, 10, 345678, 20} + ``` + Everything looks pretty sorted ... just why are sets messing up with 345678? + + + #### 💡 Explanation: + + * This is because a set object is "an unordered collection" of distinct objects. + * So values in a set object are not in a sorted way. Even the values 2, 5, 10 and 20 aren't inserted in sorted manner. + + ```py + >>> st = set() + >>> st.add(5) + >>> st.add(10) + >>> print(st) + >>> st.add(20) + >>> print(st) + >>> st.add(2) + >>> print(st) + >>> st.add(345678) + >>> print(st) + ``` + **Output:** + ```py + {10, 5} + {10, 20, 5} + {10, 2, 20, 5} + {2, 5, 10, 345678, 20} + ``` + **Note:** This is why when we want to iterate on the distinct elements of a sequence in sorted way, we iterate on the list obtained from sorted(st), not on the set itself. --- ### ▶ Teleportation *