-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #773 from SHAIMA-HAQUE/Python
Python 3.3: What are Membership and Identity Operators Fixes#477
- Loading branch information
Showing
2 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
207 changes: 207 additions & 0 deletions
207
Python/Operators/Python_3_3_What_are_Membership_and_Identity_Operators_.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"name": "Python 3.3: What are Membership and Identity Operators?.ipynb", | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "UyOeoIVvDS-I" | ||
}, | ||
"source": [ | ||
"# **Membership Operator in Python🐍**" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "TNxg6_-IDeSO" | ||
}, | ||
"source": [ | ||
"Python **Membership operators** are used to validate the membership of a value. It tests for memberships in sequences, for example, strings, lists and tuples.\n", | ||
"\n", | ||
"**In simple words**, *Membership operators are used to test if a sequence is present in an object*. \n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"* `in` operator checks whether an element is present in a sequence or not. It evaluates to `True` if the element is present, else it evaluates to `False`.\n", | ||
"\n", | ||
"* Whereas, the `not in` operator evaluates to `True` if the element is not present in the sequence, else it evaluates to `False`.\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "YEZRDjG0CVaY", | ||
"outputId": "0715e897-6599-4254-83ab-ab1c1c0307bf" | ||
}, | ||
"source": [ | ||
"# Example of in operator\n", | ||
"x = 'Participant'\n", | ||
"gwoc_list = ['Contributor','Mentor','Supervisor']\n", | ||
"\n", | ||
"print(x in gwoc_list)\n" | ||
], | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"False\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "earHgoGyGfGX", | ||
"outputId": "edadd055-1488-4f8b-f8e9-6d05e42a7251" | ||
}, | ||
"source": [ | ||
"# Example of not in operator\n", | ||
"x = 'Participant'\n", | ||
"gwoc_list = ['Contributor','Mentor','Supervisor']\n", | ||
"\n", | ||
"print(x not in gwoc_list)" | ||
], | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"True\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "d0eb-IC4G10G" | ||
}, | ||
"source": [ | ||
"# **Identity Operators in Python**🐍\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "-id1PavqLkD7" | ||
}, | ||
"source": [ | ||
"Before learning about **Identity Operators** in Python, we should keep something in mind.\n", | ||
"\n", | ||
"***Everything in Python is an object, and each object is stored in a specific memory location.***\n", | ||
"\n", | ||
"\n", | ||
"* `is` operator checks whether two variables refer to the same object in memory or not. It evaluates to `True` if both operands refer to the same object, else it evaluates to `False`.\n", | ||
"\n", | ||
"* Whereas, the `is not` operator evaluates to `False` if both operands refer to the same object, else it evaluates to `True`\n", | ||
"\n", | ||
"The `is` and `is not` operators can be used to \n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "kmjnD52bIxAK", | ||
"outputId": "db51c7fd-79ef-42d0-cf1f-f52b7d5a89ba" | ||
}, | ||
"source": [ | ||
" #Example of is operator\n", | ||
" a = 5\n", | ||
" if type(a) is int:\n", | ||
" print(\"Yes\")\n", | ||
" else:\n", | ||
" print(\"No\")\n" | ||
], | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Yes\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "TnopwDYWNzwt", | ||
"outputId": "fc453384-18ed-4417-8215-7edc2ee93fca" | ||
}, | ||
"source": [ | ||
"#Example of is not operator\n", | ||
"a = 5.5\n", | ||
"if type(a) is not int:\n", | ||
" print(\"not int\")\n", | ||
"else:\n", | ||
" print(\"int\")\n" | ||
], | ||
"execution_count": null, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"not int\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "QsUI-UAFOiCP" | ||
}, | ||
"source": [ | ||
"\n", | ||
"**What is the difference between `==` , `!=` and `is` and `is not` operator?**\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"* When do we use `==` and `!=`?\n", | ||
"\n", | ||
" In this case, we are checking for the **equality** of two objects.\n", | ||
" We are not concerned about the memory addresses of the objects in question.\n", | ||
"* When do we use `is` and `is not`?\n", | ||
"\n", | ||
" In this case, we check whether or not two variables point to the same object in **memory**. \n", | ||
"\n", | ||
" *The main use case for these operators is when you’re comparing to `None`.*\n" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters