Created
January 4, 2018 11:00
-
-
Save nikhilkumarsingh/67a40ddf57cefa336b93d5cf737d2547 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# <center>Bonus</center>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 1. How Python runs?\n", | |
"\n", | |
"When the Python software is installed on your machine, minimally, it has:\n", | |
"- an interpreter \n", | |
"- a support library. \n", | |
"\n", | |
"\n", | |
"\n", | |
"### Interpreter\n", | |
"\n", | |
"Interpreter is nothing but a software which can run your Python scripts.\n", | |
"\n", | |
"Interestingly, it can be implemented in any programming language!\n", | |
"\n", | |
"- **CPython** is the default interpreter for Python which is written in **C language**.\n", | |
"- **Jython** is another popular implementation of python interpreter written using **Java**.\n", | |
"\n", | |
"\n", | |
"#### Programmer's view\n", | |
"\n", | |
"\n", | |
"\n", | |
"#### Python's view\n", | |
"\n", | |
"\n", | |
"\n", | |
"- **What does the compiler do?**\n", | |
"\n", | |
"Compiler compiles your **source code (the statements in your file)** into a format known as **byte code**.\n", | |
"Compilation is simply a **translation step**!\n", | |
"\n", | |
"Byte code is a:\n", | |
"- lower level,\n", | |
"- platform independent,\n", | |
"- efficient and \n", | |
"- intermediate\n", | |
"\n", | |
"representation of the source code.\n", | |
"\n", | |
"Roughly, each of your source statements is translated into a group of byte code instructions. \n", | |
"\n", | |
"This byte code translation is performed to speed execution—byte code can be run much quicker than the original source code statements.\n", | |
"\n", | |
"- **What does the Python Virtual Machine do?**\n", | |
"\n", | |
"As soon as **source code** gets converted to **byte code**, it is fed into **PVM (Python Virtual Machine)**.\n", | |
"\n", | |
">The PVM sounds more impressive than it is; really, it’s just a big loop that iterates through your byte code instructions, one by one, to carry out their operations. The PVM is the runtime engine of Python; it’s always present as part of the Python system, and is the component that truly runs your scripts. Technically, it’s just the last step of what is called the Python interpreter.\n", | |
"\n", | |
"\n", | |
"## .pyc files\n", | |
"\n", | |
"Whenever a Python script is executed, ByteCode is generated in memory and simply discarded when program exits.\n", | |
"\n", | |
"But, if a Python module is imported, a **.pyc** file for the module is generated which contains its Byte code.\n", | |
"\n", | |
"Thus, when the module is imported next time, the byte code from **.pyc** file is used, hence skipping the compilation step!\n", | |
"\n", | |
"\n", | |
"If you need to create a **.pyc** file for a module that is not imported, you can use the **py_compile** module.\n", | |
"\n", | |
"The **py_compile** module can manually compile any module. One way is to use the py_compile.compile function in that module interactively:\n", | |
"\n", | |
"```python\n", | |
"import py_compile\n", | |
"py_compile.compile('abc.py')\n", | |
"```\n", | |
"\n", | |
"This will write the .pyc to the same location as abc.py.\n", | |
"\n", | |
"You can also automatically compile all files in a directory or directories using the **compileall** module.\n", | |
"\n", | |
"```python\n", | |
"python -m compileall .\n", | |
"```\n", | |
"\n", | |
"You can look at the byte code using the dis module!\n", | |
"```python\n", | |
"def hello():\n", | |
" print \"hello!\"\n", | |
" \n", | |
"print(dis.dis(hello))\n", | |
"```\n", | |
"\n", | |
"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 2. Underscore (_) in Python\n", | |
"\n", | |
">The underscore (_) is special in Python.\n", | |
"\n", | |
"## Case 1: For storing the value of last expression in interpreter.\n", | |
"\n", | |
"The python interpreter stores the last expression value to the special variable called ‘_’. \n", | |
"\n", | |
"\n", | |
"\n", | |
"## Case 2: For Ignoring the values\n", | |
"\n", | |
"The underscore is also used for ignoring the specific values. If you don’t need the specific values or the values are not used, just assign the values to underscore.\n", | |
"\n", | |
"\n", | |
"\n", | |
"## Case 3: Give special meanings to name of variables and functions\n", | |
"\n", | |
"### _single_leading_underscore\n", | |
"\n", | |
"This convention is used for declaring **private** variables, functions, methods and classes in a module.\n", | |
"Anything with this convention are ignored when you try to import your module in some other module by:\n", | |
"```python\n", | |
"from module import *\n", | |
"```\n", | |
"\n", | |
">However, if you still need to import a private variable, import it directly.\n", | |
"\n", | |
"### single\\_trailing\\_underscore\\_\n", | |
"\n", | |
"This convention can be used for avoiding conflict with Python keywords or built-ins.\n", | |
"\n", | |
"For example, to avoid conflict with 'list' built-in type:\n", | |
"\n", | |
"```python\n", | |
"list_ = [1,2,3,4,5]\n", | |
"```\n", | |
"\n", | |
"### __double_leading_underscore\n", | |
"\n", | |
"double underscore will change the way a method/attribute can be called using an object of a class.\n", | |
"\n", | |
"You cannot access such methods/attributes with syntax **\"ObjectName.\\_\\_method\"** as shown below:\n", | |
"\n", | |
"\n", | |
"\n", | |
"You can access them with syntax **\"ObjectName.\\_ClassName\\_\\_method\"** as shown below:\n", | |
"\n", | |
"\n", | |
"\n", | |
"This naming convention is useful in case of inheritance when you want to use **methods of same name in child and parent class** separately. See the example below:\n", | |
"\n", | |
"\n", | |
"\n", | |
"### __double_leading_and_trailing_underscore__\n", | |
"\n", | |
"This convention is used for special variables or methods (so-called “magic method”) such as \\_\\_init\\_\\_, \\_\\_len\\_\\_, etc. These methods provides special syntactic features or do special things., etc" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class A:\n", | |
" def __init__(self):\n", | |
" print(\"Object initialized!\")\n", | |
"\n", | |
" def __len__(self):\n", | |
" print(\"Get object length!\")\n", | |
" return 10\n", | |
"\n", | |
" def __call__(self):\n", | |
" print(\"Object used as a function!\")\n", | |
"\n", | |
" def __eq__(self, a):\n", | |
" print(\"Equate the two objects!\")\n", | |
"\n", | |
"a = A() # call __init__\n", | |
"print(len(a)) # call __len__\n", | |
"a() # call __call__\n", | |
"b = A() # call __init__\n", | |
"a==b # call __eq__" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Case 4: To separate the digits of number literal value\n", | |
"\n", | |
"It is used for separating digits of numbers using underscore for readability." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"num = 1_000_000\n", | |
"print(num)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# 3. Progress bars\n", | |
"\n", | |
"```\n", | |
"pip install tqdm\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"from tqdm import tqdm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"url = \"http://www.greenteapress.com/thinkpython/thinkpython.pdf\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"chunk_size = 256\n", | |
"r = requests.get(url, stream=True)\n", | |
"\n", | |
"with open(\"python.pdf\", \"wb\") as f:\n", | |
" for chunk in r.iter_content(chunk_size=chunk_size):\n", | |
" f.write(chunk)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment