#!/usr/bin/env python # coding: utf-8 # --- # # #

Department of Data Science

#

Course: Tools and Techniques for Data Science

# # --- #

Instructor: Muhammad Arif Butt, Ph.D.

#

Lecture 2.3

# Open In Colab # ## _01-variables.ipynb_ # #### [Python Built-in Types](https://docs.python.org/3/library/stdtypes.html#) # ## Learning agenda of this notebook # Variables are used to store data in computer memory # 1. Python is dynamically typed # 2. Intellisense / Code Completion # 3. Variables and variable naming conventions # 4. Assigning values to multiple variables in single line # 5. Checking type of a variable using Built-in `type()` function # 6. Checking ID of a variable using Built-in `id()` function # 7. Do we actually store data inside variables? # 8. Deleting a variable from Kernel memory # ## 1. You don't have to specify a data type in Python, since it is a dynamically typed language # > **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*. # In[1]: name_of_instructor = "Arif Butt" name_of_instructor type(name_of_instructor) # In[4]: no_of_lectures = [32.5, 66] no_of_lectures typeq # ## 2. Intellisense/Code Completion # - Intellisense or Code completion is a general term for various code editing features including: code completion, parameter info, quick info, and member lists. # * **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`. # * **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 # * **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 # ## 3. Variable Naming Conventions # - 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: # - An identifier or variable's name must start with a letter or the underscore character `_`. It cannot begin with a number. # - A variable name can only contain lowercase (small) or uppercase (capital) letters, digits, or underscores (`a`-`z`, `A`-`Z`, `0`-`9`, and `_`). # - Spaces are not allowed. Instead, we must use snake_case to make variable names readable. # - Variable names are case-sensitive, i.e., `a_variable`, `A_Variable`, and `A_VARIABLE` are all different variables. # # - 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. # ``` # False class from or # None continue global pass # True def if raise # and del import return # as elif in try # assert else is while # async except lambda with # await finally nonlocal yield # break for not # ``` # # - To get help about these keywords: Type `help('keyword')` in the cell below # In[5]: help('True') # In[ ]: # True is a keyword, can't be used as variable name #True = 100 # In[ ]: # A variable name cannot start with a special character or digit var1 = 25 #1var = 530 #@i = 980 #

Python Datatypes

# # # # # # - 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. # - Integer # - Floating Point # - Complex # - Bolean # - **Boolean:** Also # - 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: # - String # - List # - Tuple # - 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. # - Set (mutable) # - Frozenset (immutable) # - Mapping is an unordered data type in Python. Currently, there is only one standard mapping data type in Python called Dictionary. # In[ ]: a = range(10) # In[ ]: type(a) # ## 4. Assign Multiple values to Multiple variables in one Statement # In[ ]: #Assigning multiple values to multiple variables a, b, c = 5, 3.2, "Hello" print ('a = ',a,' b = ',b,' c = ',c) # ## 5. To Check the Type of a Variable # In[6]: # to check the type of variable name = "Arif Butt" print("name is of ", type(name)) x = 234 print("x is of ", type(x)) y = 5.321 print("y is of ", type(y)) # ## 6. To Check the ID of a Variable # - Every Pyton object has an associated ID (memory address). The Python built-in `id()` function returns the identity of an object # In[7]: x = 234 y = 5.321 id(x), id(y) # ## 7. Do we actually store data inside variables # In[8]: a = 10 b = 10 id(a), id(b) # >- Both the variables `a` and `b` have same ID, i.e., both a and b are pointing to same memory location. # >- Variables in Python are not actual objects, rather are references to objects that are present in memory. # >- So both the variables a and b are refering to same object 10 in memory and thus having the same ID. # In[9]: var1 = "Data Science" id(var1) # In[10]: var1 = "Arif Butt" id(var1) # >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. # ## 8. Use of `dir()`, and `del` Keyword # - The built-in `dir()` function, when called without an argument, return the names in the current scope. # - If passed a # - object name: # - module name: then returns the module's attributes # - class name: then returns its attributes and recursively the attributes of its base classes # - object name: then returns its attributes, its class's attributes, and recursively the attributes of its class's base classes. # In[11]: print(dir()) # In[12]: newvar = 10 print("newvar=", newvar) # In[13]: print(dir()) # In[14]: del var1 print(dir()) # In[15]: var1 # In[16]: import math print(dir(math)) # In[ ]: # ## Check your Concepts # # Try answering the following questions to test your understanding of the topics covered in this notebook: # # 1. What is a variable in Python? # 2. How do you create a variable? # 3. How do you check the value within a variable? # 4. How do you create multiple variables in a single statement? # 5. How do you create multiple variables with the same value? # 6. How do you change the value of a variable? # 7. How do you reassign a variable by modifying the previous value? # 8. What are the rules for naming a variable? # 9. Are variable names case-sensitive? Do `a_variable`, `A_Variable`, and `A_VARIABLE` represent the same variable or different ones? # 10. How do you check the data type of a variable?