.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/operators.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basics_operators.py: ================ 1.3 operators ================ This lesson describes Basic operators in python. The traiditional mathematical operations can be performed on python objects just by using their symbels. This means addition, subtraction, multiplication and divitsion can be perfomred just by using ``+``, ``-``, ``*`` and ``/`` symbols/operators between two objects respectively. .. GENERATED FROM PYTHON SOURCE LINES 14-17 Basic operations --------------------- ``+`` is used for addition .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: default a = 5 + 12 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 17 .. GENERATED FROM PYTHON SOURCE LINES 24-25 If we want to add something to variable 'a', one way of doint it is using ``+=``. .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: default a +=14 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 31 .. GENERATED FROM PYTHON SOURCE LINES 29-31 Above we added 10 to `a` and assigned the new value again to `a`. In this way, the value of `a` is updated. .. GENERATED FROM PYTHON SOURCE LINES 34-35 Similarly we can use ``-`` for subtracting one object from another\ .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: default a = 14 - 12 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: default a -= 5 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none -3 .. GENERATED FROM PYTHON SOURCE LINES 45-46 ``*`` is used for for multiplication .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. code-block:: default a = 2*12 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 24 .. GENERATED FROM PYTHON SOURCE LINES 54-58 .. code-block:: default a *= 2 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 48 .. GENERATED FROM PYTHON SOURCE LINES 59-60 The modulo operator ``%`` returns remainder .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: default print(14 % 5) .. rst-class:: sphx-glr-script-out .. code-block:: none 4 .. GENERATED FROM PYTHON SOURCE LINES 67-68 If one of the value is float, result will be a float. .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default print(17 % 5.0) .. rst-class:: sphx-glr-script-out .. code-block:: none 2.0 .. GENERATED FROM PYTHON SOURCE LINES 74-78 Above `17` was integer and `5.0` was float. Since one value was float, the result is also a float The sign of the result will be same as sign of divider. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default print(17 % -5.0) .. rst-class:: sphx-glr-script-out .. code-block:: none -3.0 .. GENERATED FROM PYTHON SOURCE LINES 84-85 ``/`` is used for division .. GENERATED FROM PYTHON SOURCE LINES 87-91 .. code-block:: default a = 20/6 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.3333333333333335 .. GENERATED FROM PYTHON SOURCE LINES 92-93 `//` is used for truncated division .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: default print(20//6) .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 99-102 .. code-block:: default print(20//6.0) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.0 .. GENERATED FROM PYTHON SOURCE LINES 103-105 If the answer of the truncated division is negative, the answer is rounded to the next smallest iteger (greater negative) .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: default print(20//-6.0, -20//6.0) .. rst-class:: sphx-glr-script-out .. code-block:: none -4.0 -4.0 .. GENERATED FROM PYTHON SOURCE LINES 111-114 .. code-block:: default print(-20 // -6.0) .. rst-class:: sphx-glr-script-out .. code-block:: none 3.0 .. GENERATED FROM PYTHON SOURCE LINES 115-121 **Question** What is the output of the following code .. code-block:: python print(20//-6.0, -20//6.0) .. GENERATED FROM PYTHON SOURCE LINES 123-129 **Question** What is the output of the following code .. code-block:: python print( 20//60, 20/60) .. GENERATED FROM PYTHON SOURCE LINES 131-133 ``**`` is used for exponentiation i.e. to raise one object over another. For example two to the power of three can be done as below .. GENERATED FROM PYTHON SOURCE LINES 133-136 .. code-block:: default print(2**3) .. rst-class:: sphx-glr-script-out .. code-block:: none 8 .. GENERATED FROM PYTHON SOURCE LINES 137-139 The application of basic mathematical operators is not limited to floats or integers. We can also apply ``+`` to strings in python .. GENERATED FROM PYTHON SOURCE LINES 139-144 .. code-block:: default a = "Materialism leads to " b = "injustice." print(a + b) .. rst-class:: sphx-glr-script-out .. code-block:: none Materialism leads to injustice. .. GENERATED FROM PYTHON SOURCE LINES 145-147 However we can not do same for ``-`` operator. This means we can not subtract two strings. .. GENERATED FROM PYTHON SOURCE LINES 149-153 .. code-block:: default # uncomment following line # a - b # -> TypeError .. GENERATED FROM PYTHON SOURCE LINES 154-156 But when we multiply a ``string`` by an ``integer``, the string is repeated. .. GENERATED FROM PYTHON SOURCE LINES 156-158 .. code-block:: default print(a * 2) .. rst-class:: sphx-glr-script-out .. code-block:: none Materialism leads to Materialism leads to .. GENERATED FROM PYTHON SOURCE LINES 159-166 Above we are multiplying a string with an integer because `a` is a string and `2` is integer. If however, we do ``a +2``, we will again get ``TypeError``. What kind of mathematical operations can be applied on an object or between two objects, depends purely upon the objects. To our surprise, we can even modify the behavior of these mathematical operators in python. More about this will come later in :ref:`sphx_glr_auto_examples_oop_magical_methods.py`. .. GENERATED FROM PYTHON SOURCE LINES 168-172 Comparisons -------------------------- If we want to compare one object with another and tell whether both are equal or not, we make use of ``==`` operator. .. GENERATED FROM PYTHON SOURCE LINES 174-177 .. code-block:: default print(2 == 3) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 178-179 The ``==`` operator returns either True or False depending upon the values being compared. .. GENERATED FROM PYTHON SOURCE LINES 179-181 .. code-block:: default print(2 ==2) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 182-185 .. code-block:: default print(2.2 == 1.1 + 1.1) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 186-189 However, we should avoid comparing floats in this way. This is because `computers can not represent accurate values of floats `_. 1.1 + 2.2 results in an approximated answer, so we better avoid comparing floats. .. GENERATED FROM PYTHON SOURCE LINES 191-195 **Question** Find out the type of the output returned by ``==`` operator. type(2==3) ######################################## .. GENERATED FROM PYTHON SOURCE LINES 195-198 .. code-block:: default print(3.3 == 1.1 + 2.2) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 199-202 .. code-block:: default print(abs((1.1 + 2.2) - 3.3) < 1e-15) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 203-208 ``>`` can be read as `greater than` or `smaller than`` depending upon its direction. %% If you are getting the output as ``True`` for the above code, it is because of the subtle differences in floating point implementation in your hardware. .. GENERATED FROM PYTHON SOURCE LINES 210-212 If we want to check whether a numerical value lies between two numbers or not we can make use of ``<`` or ``>`` operators twice. .. GENERATED FROM PYTHON SOURCE LINES 214-217 .. code-block:: default print(8<10<12) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 218-219 ``!=`` can be considered as opposite of ``==`` and can be read as `not equal to` .. GENERATED FROM PYTHON SOURCE LINES 219-224 .. code-block:: default capitalism = 'a system based on individualism' print(capitalism != 'justice') .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 225-228 Logical operators -------------------------------- ``not`` results in opposite to what comes after it. .. GENERATED FROM PYTHON SOURCE LINES 230-233 .. code-block:: default print(not True) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 234-237 .. code-block:: default print(not False) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 238-242 .. code-block:: default x = 5.2 print(not x<=10) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 243-249 .. code-block:: default capitalism = False communism = False justice = True print(capitalism and communism) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 250-254 .. code-block:: default print(capitalism or communism) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 255-258 .. code-block:: default print(capitalism and justice) .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 259-265 Above, since one statement on left side of ``and`` is False, the output is False. For ``and`` to return True, both statements/expressions on its right and left side, must return True. If one is True and one is False, the output will be False. However, for ``or``, even if one side evaluates to True, the output is True. This becomes clear if you understand, why the output of below code is True. .. GENERATED FROM PYTHON SOURCE LINES 265-268 .. code-block:: default print(capitalism or justice) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 269-272 .. code-block:: default print(capitalism is not justice) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 273-286 **Question** The above code ``print(capitalism is not justice)`` printed True. This is because ``justice`` was True and thus ``not justice`` becomes False. Therefore, what we are implicitly saying `False is False`, which is True. Now reset the values of variables `capitalism` and `justice` in such a way that the code ``print(capitalism is not justice)`` should return False. This is shown below .. code-block:: python capitalism = justice = print(capitalism is not justice) # must print False .. GENERATED FROM PYTHON SOURCE LINES 288-290 Default values ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 292-297 .. code-block:: default food = 'bread' lunch = food or 'curry' print(lunch) .. rst-class:: sphx-glr-script-out .. code-block:: none bread .. GENERATED FROM PYTHON SOURCE LINES 298-300 The statement on left of ``or`` was not False/None, therefore, variable `lunch` became `bread` . .. GENERATED FROM PYTHON SOURCE LINES 300-305 .. code-block:: default food = None lunch = food or 'curry' print(lunch) .. rst-class:: sphx-glr-script-out .. code-block:: none curry .. GENERATED FROM PYTHON SOURCE LINES 306-307 If the first argument before ``or`` is ``True`` or not None, the value after ``or`` is discarded. .. GENERATED FROM PYTHON SOURCE LINES 309-314 .. code-block:: default food = None lunch = 'currey' or food print(lunch) .. rst-class:: sphx-glr-script-out .. code-block:: none currey .. GENERATED FROM PYTHON SOURCE LINES 315-320 .. code-block:: default food = 'bread' lunch = 'currey' or food print(lunch) .. rst-class:: sphx-glr-script-out .. code-block:: none currey .. GENERATED FROM PYTHON SOURCE LINES 321-325 Identity --------------------- ``is`` operator compares whether both variables on its right and left side refer to same memory location or not. .. GENERATED FROM PYTHON SOURCE LINES 327-332 .. code-block:: default a = 257 b = 257 print(a == b) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 333-336 .. code-block:: default id(a), id(b) .. rst-class:: sphx-glr-script-out .. code-block:: none (139721348137264, 139721348137264) .. GENERATED FROM PYTHON SOURCE LINES 337-339 Because ali and hasan are stored at different location at different location, thus the answer is False .. GENERATED FROM PYTHON SOURCE LINES 341-344 .. code-block:: default print(a is b) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 345-352 **Note** If the above code prints ``True``, it could be that the python version that you are using saves even large integers in memory. Actually, python already stores some commonly used smaller numbers in memory, so when they are created, python refers to that same memory location and does not really create them. Thus, for smaller numbers (from -5 to 256 integers) `is` returns True. .. GENERATED FROM PYTHON SOURCE LINES 354-359 .. code-block:: default a = 256 b = 254 + 2 print(a is b) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 360-363 .. code-block:: default print(id(a), id(b)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721726204304 139721726204304 .. GENERATED FROM PYTHON SOURCE LINES 364-370 .. code-block:: default feudalism = 'slavery' capitalism = 'slavery' print(feudalism is capitalism) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 371-377 .. code-block:: default feudalism = 'a system of slavery' capitalism = 'a system of slavery' print(feudalism is capitalism) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 378-380 Order of operations ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 382-384 The multiplication ``*`` is performed before addition ``+`` even if ``+`` appears before ``*``. .. GENERATED FROM PYTHON SOURCE LINES 384-386 .. code-block:: default print(20 + 4 * 10) .. rst-class:: sphx-glr-script-out .. code-block:: none 60 .. GENERATED FROM PYTHON SOURCE LINES 387-388 Similarly `exponentiation` (``**``) is performed before multiplication. .. GENERATED FROM PYTHON SOURCE LINES 388-390 .. code-block:: default print(2 * 3 ** 4 * 5) .. rst-class:: sphx-glr-script-out .. code-block:: none 810 .. GENERATED FROM PYTHON SOURCE LINES 391-393 **Question:** Write the code to convert 1000 seconds into hours. .. GENERATED FROM PYTHON SOURCE LINES 395-397 **Question:** Why the output of ``2/3+4`` and ``2/(3+4)`` is different? .. GENERATED FROM PYTHON SOURCE LINES 399-401 Complete order of precedence of operators in python can be found from :ref:`here ` . .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.014 seconds) .. _sphx_glr_download_auto_examples_basics_operators.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/basics/operators.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: operators.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: operators.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_