Skip to content

Commit

Permalink
Merge pull request #773 from SHAIMA-HAQUE/Python
Browse files Browse the repository at this point in the history
Python 3.3: What are Membership and Identity Operators Fixes#477
  • Loading branch information
adityabisoi authored Sep 19, 2021
2 parents 53c87be + 064b14b commit 621550d
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 3 deletions.
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"
]
}
]
}
6 changes: 3 additions & 3 deletions Python/Operators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ Operators are special symbols in Python that carry out arithmetic or logical com
2. [**Assignment Operators**](./2.%20Assignment%20Operators.ipynb)
3. [**Comparison Operators**]()
4. [**Logical Operators**]()
5. [**Identity operators**]()
6. [**Membership operators**]()
7. [**Bitwise operators**]()
5. [**Identity operators**](https://github.com/SHAIMA-HAQUE/winter-of-contributing/blob/Python/Python/Operators/Python_3_3_What_are_Membership_and_Identity_Operators_.ipynb)
6. [**Membership operators**](https://github.com/SHAIMA-HAQUE/winter-of-contributing/blob/Python/Python/Operators/Python_3_3_What_are_Membership_and_Identity_Operators_.ipynb)
7. [**Bitwise operators**]()

0 comments on commit 621550d

Please sign in to comment.