From 7a07fa89746e70f1539ae57912ed19e5690a561a Mon Sep 17 00:00:00 2001 From: SavagePastaMan <69145546+SavagePastaMan@users.noreply.github.com> Date: Mon, 12 Apr 2021 10:09:43 -0400 Subject: Create identity.md Tag to demonstrate the difference between `is` and `==`. --- bot/resources/tags/identity.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 bot/resources/tags/identity.md diff --git a/bot/resources/tags/identity.md b/bot/resources/tags/identity.md new file mode 100644 index 000000000..32995aef6 --- /dev/null +++ b/bot/resources/tags/identity.md @@ -0,0 +1,25 @@ +**Identity vs. Equality** + +Should I be using `is` or `==`? + +To check if two things are equal, use the equality operator (`==`). +```py +x = 5 +if x == 5: + print("x equals 5") +if x == 3: + print("x equals 3") +# Prints 'x equals 5' +``` + +To check if two things are actually the same thing in memory, use the identity comparison operator (`is`). +```py +x = [1, 2, 3] +y = [1, 2, 3] +if x is [1, 2, 3]: + print("x is y") +z = x +if x is z: + print("x is z") +# Prints 'x is z' +``` -- cgit v1.2.3