.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/variables.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_variables.py: ============= 1.1 variables ============= This lesson introduces data types and variables in python. .. GENERATED FROM PYTHON SOURCE LINES 9-18 A variable is a way to link some data to a memory location. The memory here, does not mean the storage such as hard drive or USB etc. rather memory such as RAM. The memory size which is allocated for a variable depends on the `kind` and the amount of data linked to that variable. For example, a variable consisting 10 integers will hold less memory as compared to a variable consisting of a million integers. Similarly, a variable holding an integer (e.g. 92) will have different amount/size of memory as compared to a variable holding a string say ``Ali``. When we define a variable in python, the python checks the type of the variable and allocates some memory for that variable. .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: default a = 12 .. GENERATED FROM PYTHON SOURCE LINES 24-27 So what has been done above is that a variable named ``a`` is assigned a value of 12. Behind the scenes python created an ``object`` and the variable name ``a`` is a reference for that object. .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: default a = 14 .. GENERATED FROM PYTHON SOURCE LINES 33-35 When the variable ``a`` is redefined, it means the location of memory which was holding the value 12 before, now holds 14. This means we have changed the contents of memory. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: default a = a + 12 print(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 26 .. GENERATED FROM PYTHON SOURCE LINES 42-47 The ``print`` is a (builtin) function in python which we can use to `print the value of a variable`. This is not always true but more about it will come in :ref:`sphx_glr_auto_examples_basics_print_function.py` and :ref:`sphx_glr_auto_examples_oop_magical_methods.py`. The kind of object python creates, depends upon the type of data. We can check the `type` of a variable by using the command/function ``type(VariableName)`` .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: default print(type(a)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 53-58 A ``function`` is a different creature in python. We will cover more about it later in :ref:`sphx_glr_auto_examples_basics_functions.py` . At this point, just keep in mind that when some object is a function, we can `call` it by appending paranthesis ``()`` after its name Above we have called the ``print`` function. Don't worry if you don't understand the meaning of calling a function at this point. .. GENERATED FROM PYTHON SOURCE LINES 60-62 The function ``type`` is **the most important function in python**. Whenver you don't know about some object in python, the first thing you should do is to check its ``type`` using ``type(object)`` .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: default b = 30.0 print(type(b)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 66-69 **Question** Both ``a`` and ``b`` contained the value `thirty`, then why they had different types? .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: default c = a + b print(c, type(c)) .. rst-class:: sphx-glr-script-out .. code-block:: none 56.0 .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: default d = a + a print(d, type(d)) .. rst-class:: sphx-glr-script-out .. code-block:: none 52 .. GENERATED FROM PYTHON SOURCE LINES 81-93 It is important to note that the python is able to change the type of new variable based on the kind of value assigned to it. If a float value is assigned to a variable, python will change the type of this variable to float. Another significant thing is that, we can assign any type of data to a variable. For example, we can assign ``int`` to variable ``a``, later we can assign ``float`` to the variable ``a`` and then we can assign a completely different type like ``string`` to the variable ``a``. This is a blessing (in terms of ease of use) as well as a curse (in terms of its slow speed) of python and for python users. .. GENERATED FROM PYTHON SOURCE LINES 95-99 .. code-block:: default a = 'Ali' print(type(a)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 100-104 **Question** Find out 14 different ``types`` in python. We have already seen three types above i.e. ``str``, ``int`` and ``float``. .. GENERATED FROM PYTHON SOURCE LINES 106-109 When we assign a value to a variable and then assign that variable to a new variable, then both of these variables actually refer to the same object. We can verify this using the function ``id(VariableName)`` .. GENERATED FROM PYTHON SOURCE LINES 111-116 .. code-block:: default a = 12 b = a id(a), id(b) .. rst-class:: sphx-glr-script-out .. code-block:: none (139692357458576, 139692357458576) .. GENERATED FROM PYTHON SOURCE LINES 117-122 Above we have checked the identity of both ``a`` and ``b``. The identity of an object in python is the memory address of that object. It means the address in memory (RAM) where that objected is stored/put. Since both ``a`` and ``b`` refer to the same object, they have the same memory address. We can also say that since both ``a`` and ``b`` have the same memory address, that means they are same objects. .. GENERATED FROM PYTHON SOURCE LINES 124-127 .. code-block:: default b = 14 id(a), id(b) .. rst-class:: sphx-glr-script-out .. code-block:: none (139692357458576, 139692357458640) .. GENERATED FROM PYTHON SOURCE LINES 128-134 So when we assigned a different data to ``b``, a new object was created and now ``b`` refers to this new object and thus its identity (memory address) changes now. Just as there are conventions for naming people in a society, there is convention and rules for naming variables in python. The name of a variable can be any alpha-numeric combination with some exceptions. Following are valid variable names .. GENERATED FROM PYTHON SOURCE LINES 136-142 .. code-block:: default ali9 = 12 Ali9 = 14 아타르 = 2 print(아타르) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 143-144 A variable name must not start from a number. .. GENERATED FROM PYTHON SOURCE LINES 146-150 .. code-block:: default # uncomment following line # 1_ali = 29 .. GENERATED FROM PYTHON SOURCE LINES 151-153 **Question:** Explain the error which will result from the above code. .. GENERATED FROM PYTHON SOURCE LINES 155-157 We can not name certain keywords as variable names. These keywords can be seen official python docs website [1]_ .. GENERATED FROM PYTHON SOURCE LINES 159-166 Data Types ========== Data type signifies the type of operation that can be performed on that data. The three common data types in python are as follows * numeric * sequence * boolean .. GENERATED FROM PYTHON SOURCE LINES 168-174 Numbers ----------- To represent numerical values, python has three types * integer * float * complex .. GENERATED FROM PYTHON SOURCE LINES 176-187 .. code-block:: default a = 1 b = 0b101 # binary with base 2 x = 0o14 # octal with base 8 y = 0xe # hexadecimals with base 16 print(a, type(a)) print(b, type(b)) print(x, type(x)) print(y, type(y)) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 5 12 14 .. GENERATED FROM PYTHON SOURCE LINES 188-191 `0b101`, `0o14` and `0xe` are examples of integers represented in binary, octal and hexadecimal bases respectively. We can convert a number to binary, octal and hexadecimal using the functions ``bin``, ``oct`` and ``hex`` respectively as follows .. GENERATED FROM PYTHON SOURCE LINES 191-196 .. code-block:: default print(bin(5)) print(oct(12)) print(hex(14)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0b101 0o14 0xe .. GENERATED FROM PYTHON SOURCE LINES 197-200 However, if you don't understand the meaning of binary, octal and hexadecimal bases, don't worry. This is not important at this point. Just keep in mind that we can represent numbers in different bases in python. .. GENERATED FROM PYTHON SOURCE LINES 200-204 .. code-block:: default a = 12.5e3 print(type(a)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 205-208 Since the value of ``a`` is a float, the type of ``a`` is float. The value of ``a`` is 12.5 times 10 raised to the power 3. This is called scientific notation. We can also represent a number in scientific notation as follows .. GENERATED FROM PYTHON SOURCE LINES 208-211 .. code-block:: default a = 12.5e-3 .. GENERATED FROM PYTHON SOURCE LINES 212-214 **Question** What is the value of ``a`` in the above code? .. GENERATED FROM PYTHON SOURCE LINES 216-220 **Question:** Define variables to store the information on your ID card and print each of them. The variables should not be of same type. .. GENERATED FROM PYTHON SOURCE LINES 222-226 .. code-block:: default x = 3 + 4j # consist of real and imaginary part print(type(x)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 227-232 .. code-block:: default coke = False water = True print(type(coke)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 233-235 **Question** What will the suitable data type to store currency values? Explain your reasoning. .. GENERATED FROM PYTHON SOURCE LINES 237-239 Sequence --------- .. GENERATED FROM PYTHON SOURCE LINES 241-243 boolean -------- .. GENERATED FROM PYTHON SOURCE LINES 245-246 .. [1] ``_ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.007 seconds) .. _sphx_glr_download_auto_examples_basics_variables.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/variables.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: variables.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: variables.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_