.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python\demo_operations.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_demo_operations.py: Basic operations ================ Arithmetic operations .. GENERATED FROM PYTHON SOURCE LINES 6-36 .. code-block:: Python a = 21 b = 10 c = 0 c = a + b print("line 1 - value of c is ", c) c = a - b print("line 2 - value of c is ", c) c = a * b print("line 3 - value of c is ", c) c = a / b print("line 4 - value of c is ", c) c = a % b print("line 5 - value of c is ", c) a = 2 b = 3 c = a**b print("line 6 - value of c is ", c) a = 10 b = 5 c = a // b print("line 7 - value of c is ", c) .. rst-class:: sphx-glr-script-out .. code-block:: none line 1 - value of c is 31 line 2 - value of c is 11 line 3 - value of c is 210 line 4 - value of c is 2.1 line 5 - value of c is 1 line 6 - value of c is 8 line 7 - value of c is 2 .. GENERATED FROM PYTHON SOURCE LINES 38-39 Comparison operations .. GENERATED FROM PYTHON SOURCE LINES 39-80 .. code-block:: Python a = 21 b = 10 c = 0 if a == b: print("line 1 - a is equal to b") else: print("line 1 - a is not equal to b") if a != b: print("line 2 - a is not equal to b") else: print("line 2 - a is equal to b") if a != b: print("line 3 - a is not equal to b") else: print("line 3 - a is equal to b") if a < b: print("line 4 - a is less than b") else: print("line 4 - a is not less than b") if a > b: print("line 5 - a is greater than b") else: print("line 5 - a is not greater than b") a = 5 b = 20 if a <= b: print("line 6 - a is either less than or equal to b") else: print("line 6 - a is neither less than nor equal to b") if b >= a: print("line 7 - b is either greater than or equal to b") else: print("line 7 - b is neither greater than nor equal to b") .. rst-class:: sphx-glr-script-out .. code-block:: none line 1 - a is not equal to b line 2 - a is not equal to b line 3 - a is not equal to b line 4 - a is not less than b line 5 - a is greater than b line 6 - a is either less than or equal to b line 7 - b is either greater than or equal to b .. GENERATED FROM PYTHON SOURCE LINES 81-82 Bitwise operations .. GENERATED FROM PYTHON SOURCE LINES 82-110 .. code-block:: Python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b # 12 = 0000 1100 print("line 1 - Value of c is ", c) c = a | b # 61 = 0011 1101 print("line 2 - Value of c is ", c) c = a ^ b # 49 = 0011 0001 print("line 3 - Value of c is ", c) c = ~a # -61 = 1100 0011 print("line 4 - Value of c is ", c) c = a << 2 # 240 = 1111 0000 print("line 5 - Value of c is ", c) c = a >> 2 # 15 = 0000 1111 print("line 6 - Value of c is ", c) .. rst-class:: sphx-glr-script-out .. code-block:: none line 1 - Value of c is 12 line 2 - Value of c is 61 line 3 - Value of c is 49 line 4 - Value of c is -61 line 5 - Value of c is 240 line 6 - Value of c is 15 .. GENERATED FROM PYTHON SOURCE LINES 111-112 Logical operations .. GENERATED FROM PYTHON SOURCE LINES 112-116 .. code-block:: Python (a and b) is True (a or b) is True not (a and b) is False .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 117-118 Membership operations .. GENERATED FROM PYTHON SOURCE LINES 118-138 .. code-block:: Python a = 10 b = 20 list = [1, 2, 3, 4, 5] if a in list: print("line 1 - a is available in the given list") else: print("line 1 - a is not available in the given list") if b not in list: print("line 2 - b is not available in the given list") else: print("line 2 - b is available in the given list") a = 2 if a in list: print("line 3 - a is available in the given list") else: print("line 3 - a is not available in the given list") .. rst-class:: sphx-glr-script-out .. code-block:: none line 1 - a is not available in the given list line 2 - b is not available in the given list line 3 - a is available in the given list .. GENERATED FROM PYTHON SOURCE LINES 139-140 Identify operations .. GENERATED FROM PYTHON SOURCE LINES 140-164 .. code-block:: Python a = 20 b = 20 if a is b: print("line 1 - a and b have same identity") else: print("line 1 - a and b do not have same identity") if id(a) == id(b): print("line 2 - a and b have same identity") else: print("line 2 - a and b do not have same identity") b = 30 if a is b: print("line 3 - a and b have same identity") else: print("line 3 - a and b do not have same identity") if a is not b: print("line 4 - a and b do not have same identity") else: print("line 4 - a and b have same identity") .. rst-class:: sphx-glr-script-out .. code-block:: none line 1 - a and b have same identity line 2 - a and b have same identity line 3 - a and b do not have same identity line 4 - a and b do not have same identity .. GENERATED FROM PYTHON SOURCE LINES 165-166 Operators Precedence .. GENERATED FROM PYTHON SOURCE LINES 166-185 .. code-block:: Python a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d # ( 30 * 15 ) / 5 print("value of (a + b) * c / d is ", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print("value of ((a + b) * c) / d is ", e) e = (a + b) * (c / d) # (30) * (15/5) print("value of (a + b) * (c / d) is ", e) e = a + (b * c) / d # 20 + (150/5) print("value of a + (b * c) / d is ", e) .. rst-class:: sphx-glr-script-out .. code-block:: none value of (a + b) * c / d is 90.0 value of ((a + b) * c) / d is 90.0 value of (a + b) * (c / d) is 90.0 value of a + (b * c) / d is 50.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.008 seconds) .. _sphx_glr_download_11_demos_python_demo_operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_operations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_operations.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_operations.zip `