.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/oop/introduction.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_oop_introduction.py: ================= 3.1 introduction ================= .. GENERATED FROM PYTHON SOURCE LINES 9-15 The first thing to understanding in object oriented programming in python is the concept of ``type``. In python everything is an ``object`` and it has a ``type``. The ``type`` of an ``object`` tells, what kind of characteristics it has, or what kind of operations it can perform or can be performed on it. .. GENERATED FROM PYTHON SOURCE LINES 17-21 .. code-block:: default a_int = 12 print(type(a_int)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 22-26 .. code-block:: default a_float = 12.0 print(type(a_float)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: default a_name = 'Ali' print(type(a_name)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 32-33 Even the functions have their types .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: default def print_name(the_name): print('name is: ', the_name) print(type(print_name)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: default import math print(type(math)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: default looters = ['zardari', 'nawaz', 'establishment'] print(type(looters)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: default insan = {'name': 'ali', 'age': 30, 'weight': 72.5} print(type(insan)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: default for key, val in insan.items(): print(type(key), type(val)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 61-63 Since `insan` is an instance of class ``dict``, therefore we can access all functions (methods) of ``dict`` class through its instance `insan`. These methods include ``pop`` or ``update`` etc. .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: default insan.pop('weight') .. rst-class:: sphx-glr-script-out .. code-block:: none 72.5 .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default print(insan) .. rst-class:: sphx-glr-script-out .. code-block:: none {'name': 'ali', 'age': 30} .. GENERATED FROM PYTHON SOURCE LINES 73-75 We can always check the methods and attributes available for the instance of a class by ``dir(object)`` .. GENERATED FROM PYTHON SOURCE LINES 75-78 .. code-block:: default print(dir(insan)) .. rst-class:: sphx-glr-script-out .. code-block:: none ['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] .. GENERATED FROM PYTHON SOURCE LINES 79-84 Any method/attribute that starts with single or double underscore "_" is not for public use. Considering this, we can use any attribute/method of an `insan` which we printed above. For example, we can do `insan.update` or `insan.vlaues` or `insan.keys` etc. For ``methods``, we have to `call` them like insan.values() and for other attributes, we don't need to call them. .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: default print(dir(a_name)) .. rst-class:: sphx-glr-script-out .. code-block:: none ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] .. GENERATED FROM PYTHON SOURCE LINES 88-90 The attributes of `a_name` are different as compared to those of `insan`. Therefore, we can not do `insan.startswith()` or `a_name.values()`. .. GENERATED FROM PYTHON SOURCE LINES 92-96 **Question**: What methods/functions can be applied on `a_int` and `a_float` objects defined at the start of this lesson? Give examples of five such functions by applying them. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.004 seconds) .. _sphx_glr_download_auto_examples_oop_introduction.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/oop/introduction.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: introduction.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: introduction.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_