{ "cells": [ { "cell_type": "markdown", "id": "14894adf", "metadata": {}, "source": [ "--- \n", " \n", "\n", "

Department of Data Science

\n", "

Course: Tools and Techniques for Data Science

\n", "\n", "---\n", "

Instructor: Muhammad Arif Butt, Ph.D.

" ] }, { "cell_type": "markdown", "id": "8a0b5128", "metadata": {}, "source": [ "

Lecture 2.3

" ] }, { "cell_type": "markdown", "id": "f878d5ac", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "217504e7", "metadata": {}, "source": [ "## _01-variables.ipynb_\n", "#### [Python Built-in Types](https://docs.python.org/3/library/stdtypes.html#)" ] }, { "cell_type": "markdown", "id": "570224a3", "metadata": {}, "source": [ "## Learning agenda of this notebook\n", "Variables are used to store data in computer memory\n", "1. Python is dynamically typed\n", "2. Intellisense / Code Completion\n", "3. Variables and variable naming conventions\n", "4. Assigning values to multiple variables in single line\n", "5. Checking type of a variable using Built-in `type()` function\n", "6. Checking ID of a variable using Built-in `id()` function\n", "7. Do we actually store data inside variables?\n", "8. Deleting a variable from Kernel memory" ] }, { "cell_type": "markdown", "id": "db818c1d", "metadata": {}, "source": [ "## 1. You don't have to specify a data type in Python, since it is a dynamically typed language" ] }, { "cell_type": "markdown", "id": "e690e63b", "metadata": {}, "source": [ "> **Variables**: While working with a programming language such as Python, information is stored in *variables*. You can think of variables as containers for storing data. The data stored within a variable is called its *value*." ] }, { "cell_type": "code", "execution_count": 1, "id": "518a55a9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name_of_instructor = \"Arif Butt\"\n", "name_of_instructor\n", "type(name_of_instructor)" ] }, { "cell_type": "code", "execution_count": 4, "id": "401c5e99", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "no_of_lectures = [32.5, 66]\n", "no_of_lectures\n", "typeq" ] }, { "cell_type": "markdown", "id": "b128f324", "metadata": {}, "source": [ "## 2. Intellisense/Code Completion\n", "- Intellisense or Code completion is a general term for various code editing features including: code completion, parameter info, quick info, and member lists. \n", " * **Autocompletion:** Type few characters of a variable or function name and then press tab to get a list, press enter to complete. While typing the name of an existing variable in a code cell within Jupyter, just type the first few characters and press the `Tab` key to autocomplete the variable's name. Try typing `nam` in a code cell below and press `Tab` to autocomplete to `name_of_instructor`.\n", " * **Intellisense:** After the name of a object, put a `Dot`, then press `Tab` to get a list of all the attributes and methods of that object\n", " * **Tooltip:** With the cursor on the name of the method, press to get a tool tip, that describes what the method does, and what parameters it takes" ] }, { "cell_type": "markdown", "id": "ab29c96a", "metadata": {}, "source": [ "## 3. Variable Naming Conventions\n", "- In programming languages, **identifiers** are names used to identify a variable, function, or other entities in a program. Variable names can be short (`a`, `x`, `y`, etc.) or descriptive ( `my_favorite_color`, `profit_margin`, `the_3_musketeers`, etc.). However, you must follow these rules while naming Python variables:\n", " - An identifier or variable's name must start with a letter or the underscore character `_`. It cannot begin with a number.\n", " - A variable name can only contain lowercase (small) or uppercase (capital) letters, digits, or underscores (`a`-`z`, `A`-`Z`, `0`-`9`, and `_`).\n", " - Spaces are not allowed. Instead, we must use snake_case to make variable names readable.\n", " - Variable names are case-sensitive, i.e., `a_variable`, `A_Variable`, and `A_VARIABLE` are all different variables.\n", "\n", "- Keywords are reserved words. Each keyword has a specific meaning to the Python interpreter. A reserved keyword may not be used as an identifier. Here is a list of the Python keywords.\n", "```\n", "False class from or\n", "None continue global pass\n", "True def if raise\n", "and del import return\n", "as elif in try\n", "assert else is while\n", "async except lambda with\n", "await finally nonlocal yield\n", "break for not \n", "```\n", "\n", "- To get help about these keywords: Type `help('keyword')` in the cell below" ] }, { "cell_type": "code", "execution_count": 5, "id": "ae532168", "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on bool object:\n", "\n", "class bool(int)\n", " | bool(x) -> bool\n", " | \n", " | Returns True when the argument x is true, False otherwise.\n", " | The builtins True and False are the only two instances of the class bool.\n", " | The class bool is a subclass of the class int, and cannot be subclassed.\n", " | \n", " | Method resolution order:\n", " | bool\n", " | int\n", " | object\n", " | \n", " | Methods defined here:\n", " | \n", " | __and__(self, value, /)\n", " | Return self&value.\n", " | \n", " | __or__(self, value, /)\n", " | Return self|value.\n", " | \n", " | __rand__(self, value, /)\n", " | Return value&self.\n", " | \n", " | __repr__(self, /)\n", " | Return repr(self).\n", " | \n", " | __ror__(self, value, /)\n", " | Return value|self.\n", " | \n", " | __rxor__(self, value, /)\n", " | Return value^self.\n", " | \n", " | __xor__(self, value, /)\n", " | Return self^value.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Static methods defined here:\n", " | \n", " | __new__(*args, **kwargs) from builtins.type\n", " | Create and return a new object. See help(type) for accurate signature.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Methods inherited from int:\n", " | \n", " | __abs__(self, /)\n", " | abs(self)\n", " | \n", " | __add__(self, value, /)\n", " | Return self+value.\n", " | \n", " | __bool__(self, /)\n", " | self != 0\n", " | \n", " | __ceil__(...)\n", " | Ceiling of an Integral returns itself.\n", " | \n", " | __divmod__(self, value, /)\n", " | Return divmod(self, value).\n", " | \n", " | __eq__(self, value, /)\n", " | Return self==value.\n", " | \n", " | __float__(self, /)\n", " | float(self)\n", " | \n", " | __floor__(...)\n", " | Flooring an Integral returns itself.\n", " | \n", " | __floordiv__(self, value, /)\n", " | Return self//value.\n", " | \n", " | __format__(self, format_spec, /)\n", " | Default object formatter.\n", " | \n", " | __ge__(self, value, /)\n", " | Return self>=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getnewargs__(self, /)\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __hash__(self, /)\n", " | Return hash(self).\n", " | \n", " | __index__(self, /)\n", " | Return self converted to an integer, if self is suitable for use as an index into a list.\n", " | \n", " | __int__(self, /)\n", " | int(self)\n", " | \n", " | __invert__(self, /)\n", " | ~self\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __lshift__(self, value, /)\n", " | Return self<>self.\n", " | \n", " | __rshift__(self, value, /)\n", " | Return self>>value.\n", " | \n", " | __rsub__(self, value, /)\n", " | Return value-self.\n", " | \n", " | __rtruediv__(self, value, /)\n", " | Return value/self.\n", " | \n", " | __sizeof__(self, /)\n", " | Returns size in memory, in bytes.\n", " | \n", " | __sub__(self, value, /)\n", " | Return self-value.\n", " | \n", " | __truediv__(self, value, /)\n", " | Return self/value.\n", " | \n", " | __trunc__(...)\n", " | Truncating an Integral returns itself.\n", " | \n", " | as_integer_ratio(self, /)\n", " | Return integer ratio.\n", " | \n", " | Return a pair of integers, whose ratio is exactly equal to the original int\n", " | and with a positive denominator.\n", " | \n", " | >>> (10).as_integer_ratio()\n", " | (10, 1)\n", " | >>> (-10).as_integer_ratio()\n", " | (-10, 1)\n", " | >>> (0).as_integer_ratio()\n", " | (0, 1)\n", " | \n", " | bit_length(self, /)\n", " | Number of bits necessary to represent self in binary.\n", " | \n", " | >>> bin(37)\n", " | '0b100101'\n", " | >>> (37).bit_length()\n", " | 6\n", " | \n", " | conjugate(...)\n", " | Returns self, the complex conjugate of any int.\n", " | \n", " | to_bytes(self, /, length, byteorder, *, signed=False)\n", " | Return an array of bytes representing an integer.\n", " | \n", " | length\n", " | Length of bytes object to use. An OverflowError is raised if the\n", " | integer is not representable with the given number of bytes.\n", " | byteorder\n", " | The byte order used to represent the integer. If byteorder is 'big',\n", " | the most significant byte is at the beginning of the byte array. If\n", " | byteorder is 'little', the most significant byte is at the end of the\n", " | byte array. To request the native byte order of the host system, use\n", " | `sys.byteorder' as the byte order value.\n", " | signed\n", " | Determines whether two's complement is used to represent the integer.\n", " | If signed is False and a negative integer is given, an OverflowError\n", " | is raised.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Class methods inherited from int:\n", " | \n", " | from_bytes(bytes, byteorder, *, signed=False) from builtins.type\n", " | Return the integer represented by the given array of bytes.\n", " | \n", " | bytes\n", " | Holds the array of bytes to convert. The argument must either\n", " | support the buffer protocol or be an iterable object producing bytes.\n", " | Bytes and bytearray are examples of built-in objects that support the\n", " | buffer protocol.\n", " | byteorder\n", " | The byte order used to represent the integer. If byteorder is 'big',\n", " | the most significant byte is at the beginning of the byte array. If\n", " | byteorder is 'little', the most significant byte is at the end of the\n", " | byte array. To request the native byte order of the host system, use\n", " | `sys.byteorder' as the byte order value.\n", " | signed\n", " | Indicates whether two's complement is used to represent the integer.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors inherited from int:\n", " | \n", " | denominator\n", " | the denominator of a rational number in lowest terms\n", " | \n", " | imag\n", " | the imaginary part of a complex number\n", " | \n", " | numerator\n", " | the numerator of a rational number in lowest terms\n", " | \n", " | real\n", " | the real part of a complex number\n", "\n" ] } ], "source": [ "help('True')" ] }, { "cell_type": "code", "execution_count": null, "id": "35ab7da4", "metadata": {}, "outputs": [], "source": [ "# True is a keyword, can't be used as variable name\n", "#True = 100" ] }, { "cell_type": "code", "execution_count": null, "id": "d791c813", "metadata": {}, "outputs": [], "source": [ "# A variable name cannot start with a special character or digit\n", "var1 = 25\n", "#1var = 530\n", "#@i = 980" ] }, { "cell_type": "markdown", "id": "9b51a095", "metadata": {}, "source": [ "

Python Datatypes

\n", "\n", " \n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "387f3d01", "metadata": {}, "source": [ "- Python's Number data types are created by numeric literals and returned as results by arithmetic operators and arithmetic built-in functions. ALL Numeric objects are immutable; once created their value never changes.\n", " - Integer\n", " - Floating Point\n", " - Complex\n", " - Bolean\n", " - **Boolean:** Also \n", "- A Python sequence is an ordered collection of items, where each item is indexed by an integer value. There are three types of sequence types in Python:\n", " - String\n", " - List\n", " - Tuple\n", "- In Python, a Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined.\n", " - Set (mutable)\n", " - Frozenset (immutable)\n", "- Mapping is an unordered data type in Python. Currently, there is only one standard mapping data type in Python called Dictionary." ] }, { "cell_type": "code", "execution_count": null, "id": "349c1e82", "metadata": {}, "outputs": [], "source": [ "a = range(10)" ] }, { "cell_type": "code", "execution_count": null, "id": "03f15a5b", "metadata": {}, "outputs": [], "source": [ "type(a)" ] }, { "cell_type": "markdown", "id": "a0f875ce", "metadata": {}, "source": [ "## 4. Assign Multiple values to Multiple variables in one Statement" ] }, { "cell_type": "code", "execution_count": null, "id": "25a3f57b", "metadata": {}, "outputs": [], "source": [ "#Assigning multiple values to multiple variables\n", "a, b, c = 5, 3.2, \"Hello\"\n", "\n", "print ('a = ',a,' b = ',b,' c = ',c)" ] }, { "cell_type": "markdown", "id": "b7bddc0d", "metadata": {}, "source": [ "## 5. To Check the Type of a Variable" ] }, { "cell_type": "code", "execution_count": 6, "id": "87bc5c37", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name is of \n", "x is of \n", "y is of \n" ] } ], "source": [ "# to check the type of variable\n", "name = \"Arif Butt\"\n", "print(\"name is of \", type(name))\n", "x = 234\n", "print(\"x is of \", type(x))\n", "y = 5.321\n", "print(\"y is of \", type(y))" ] }, { "cell_type": "markdown", "id": "66c6f33f", "metadata": {}, "source": [ "## 6. To Check the ID of a Variable\n", "- Every Pyton object has an associated ID (memory address). The Python built-in `id()` function returns the identity of an object" ] }, { "cell_type": "code", "execution_count": 7, "id": "a1056c2b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4447430624, 140474726550384)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 234\n", "y = 5.321\n", "id(x), id(y)" ] }, { "cell_type": "markdown", "id": "d4478bda", "metadata": {}, "source": [ "## 7. Do we actually store data inside variables" ] }, { "cell_type": "code", "execution_count": 8, "id": "a9edba24", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4447423456, 4447423456)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 10\n", "b = 10\n", "id(a), id(b)" ] }, { "cell_type": "markdown", "id": "0492d480", "metadata": {}, "source": [ ">- Both the variables `a` and `b` have same ID, i.e., both a and b are pointing to same memory location.\n", ">- Variables in Python are not actual objects, rather are references to objects that are present in memory. \n", ">- So both the variables a and b are refering to same object 10 in memory and thus having the same ID." ] }, { "cell_type": "code", "execution_count": 9, "id": "bd2b73f2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140474713366768" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var1 = \"Data Science\"\n", "id(var1)" ] }, { "cell_type": "code", "execution_count": 10, "id": "e5251a01", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140474713404016" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var1 = \"Arif Butt\"\n", "id(var1)" ] }, { "cell_type": "markdown", "id": "34c35a04", "metadata": {}, "source": [ ">Note that the string object \"Data Science\" has become an orphaned object, as no variable is refering to it now. This is because the reference `var1` is now pointing/referring to a new object \"Arif Butt\". All orphan objects are reaped by Python garbage collector." ] }, { "cell_type": "markdown", "id": "6a3d895b", "metadata": {}, "source": [ "## 8. Use of `dir()`, and `del` Keyword\n", "- The built-in `dir()` function, when called without an argument, return the names in the current scope.\n", "- If passed a \n", " - object name: \n", " - module name: then returns the module's attributes\n", " - class name: then returns its attributes and recursively the attributes of its base classes\n", " - object name: then returns its attributes, its class's attributes, and recursively the attributes of its class's base classes." ] }, { "cell_type": "code", "execution_count": 11, "id": "54bbdd4c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '_1', '_10', '_2', '_3', '_4', '_7', '_8', '_9', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i10', '_i11', '_i2', '_i3', '_i4', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'a', 'b', 'exit', 'get_ipython', 'name', 'name_of_instructor', 'no_of_lectures', 'quit', 'var1', 'x', 'y']\n" ] } ], "source": [ "print(dir())" ] }, { "cell_type": "code", "execution_count": 12, "id": "9a07a4af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "newvar= 10\n" ] } ], "source": [ "newvar = 10\n", "print(\"newvar=\", newvar)" ] }, { "cell_type": "code", "execution_count": 13, "id": "ac57a2d1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '_1', '_10', '_2', '_3', '_4', '_7', '_8', '_9', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i2', '_i3', '_i4', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'a', 'b', 'exit', 'get_ipython', 'name', 'name_of_instructor', 'newvar', 'no_of_lectures', 'quit', 'var1', 'x', 'y']\n" ] } ], "source": [ "print(dir())" ] }, { "cell_type": "code", "execution_count": 14, "id": "0c52b477", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['In', 'Out', '_', '_1', '_10', '_2', '_3', '_4', '_7', '_8', '_9', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i2', '_i3', '_i4', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'a', 'b', 'exit', 'get_ipython', 'name', 'name_of_instructor', 'newvar', 'no_of_lectures', 'quit', 'x', 'y']\n" ] } ], "source": [ "del var1\n", "print(dir())" ] }, { "cell_type": "code", "execution_count": 15, "id": "74a3a05f", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'var1' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_27018/2376987541.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mvar1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'var1' is not defined" ] } ], "source": [ "var1" ] }, { "cell_type": "code", "execution_count": 16, "id": "b5dd1ab6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']\n" ] } ], "source": [ "import math\n", "print(dir(math))" ] }, { "cell_type": "code", "execution_count": null, "id": "65e74a95", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "731c9721", "metadata": {}, "source": [ "## Check your Concepts\n", "\n", "Try answering the following questions to test your understanding of the topics covered in this notebook:\n", "\n", "1. What is a variable in Python?\n", "2. How do you create a variable?\n", "3. How do you check the value within a variable?\n", "4. How do you create multiple variables in a single statement?\n", "5. How do you create multiple variables with the same value?\n", "6. How do you change the value of a variable?\n", "7. How do you reassign a variable by modifying the previous value?\n", "8. What are the rules for naming a variable?\n", "9. Are variable names case-sensitive? Do `a_variable`, `A_Variable`, and `A_VARIABLE` represent the same variable or different ones?\n", "10. How do you check the data type of a variable?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }