Fixing some common shit
>In windows, python
points to the store
- In cases, when Python is not in the ****
$PATH
or not installed, python
form the command line will point to the Windows Store
- In such cases, we might need to disable App Execution Alias from the settings, and reinstall Python and check mark the
PATH
option.
>Backward Compatibilty
- Python interpreters (prior to 3.0) are backward compatible, viz. if we install Python 2.5, we will be able to run scripts in Python versions < 2.5, but if we run Python 2.7 scripts with a 2.5 interpreter, we will face issues.
- Python 3 is different, it makes no promises of backward compatibility, they may or may not work?!
>Multiple Versions Dilemma
- On a computer, multiple versions of Python can coexist, but
python
only points to a specific version by default, which is python2.7
as defined in one of the PEPs
python3
will point to only one which is defined in $PATH
- In such cases, when we want to run a specific version, we must use its version number as well (if we have it), such as
python3.9 hello.py
, or use its absolute path.
/usr/bin/env python3
is more reliable than using a hardcoded path to launch Python, because it could be we moved the files to somewhere else, and it will run the Python from what is defined in $PATH
- In Windows,
py.exe
will automatically find and run Python if it's installed, and using py.exe -2
or py.exe -3
will launch specific versions
Python Basics
>Python Language vs Implementations
- Python as a language is just a specification that defined the set of rules and grammar for writing Pythonic code
- Implementation software are programs that can understand those rules and execute the Pythonic code
- The default implementation is CPython, but there are a lot of them such as Jython (Java), IronPython (C#), PyPy (Subset Of Python) etc


- In these different implementations, the bytecode differs, and we also will be able to use some parts of the core language the implementation software is based upon
- If there are any new features in Python, fastest development is done in CPython because it's the more active ansd default.
>How is Python code executed?
- It's interpreted from top-to-down, from the first command and then proceeds downwards.
- We can define the Python3 interpreter path manually by using the shebang at the top, and it will be helpful when executing the script with a relative command
./script.py
- It doesn't process the entire script looking for logic errors before it runs
- It's an interpreted language, in which, the interpreter reads and processes the script during its execution, and thus, the execution of an interpreter is a must wherever we want to run a script.
- Doesn't natively produce executables, but we can create binary executables which package the interpreter and the code together to form an executable, with tools like:
- WIndows: Py2exe, Pyinstaller and Nuitka (Python2C transpiling)
- Linux: Freeze, Pyinstaller
- Mac: py2app
- Python source code are
.py
, and its byte code is .pyc
(CPython bytecode)
- The Python interpreter compiles the Pythonic code to its bytecode, which is optimised for the interpreter to read and execute
import sys
def doesexist():
print("hello")
if len(sys.argv) > 2:
doesntexist() #Python will not even raise an exception here until the number of arguments is greater than two
else:
doesexist()
![- The bytecode is independent of the machine we're running on.
import py_compile
py_compile.compile("script")
- We can also decompile Pythonic bytecode
zrax/pycdc
>PEPs (Python Enhancement Proposals)
- PEPs are the new way features get added to Python viz. if we want something to be added to Python, we submit a PEP, the main players will then look at our shit and then respond accordingly.
- They also document how things officially mu st be done (guidelines, best practices and shit)
PEP 0 -- Index of Python Enhancement Proposals (PEPs)
>Running Pythonic code
#From the command line
python3 -c "print('Hello world')"
#By using scripts, passing .py or .pyc scripts to the Python interpreter
python3 script.py
By using the Python shell for quickly writing and testing code snippets and the Python history during this shell is stored in ~/.python_history
python3
Proper Script Structure
>Defining the encoding
Python assumes ASCII by default, if not provided otherwise.
#-*- coding: UTF-8 -*-
>Defining the interpreter path using shebang
We must define the interpreter path, which will be used if the file is run via a relative path
#!/usr/bin/env python3
>Code indentation error if wrongly formatted
- We must never mix tabs and spaces together, they may look alike, but are interpreter as very different.
- According to one of the PEPs, we should use 4 spaces for each indentation level.
- We can also set option in our code editor that would amount a TAB to four spaces.
Stop with an error if the code is formatted with both spaces and tabs
**#!/usr/bin/env python3 -tt**
Generate a warning when both tabs& spaces are used for indentation
**#!/usr/bin/env python3 -t**
>Comments and Docstrings
- Defining comments and docstrings is optional but a good practice as it tells what the certain function/ program/ method does.
#This is a single line comment
"""
This is a
multi line comment, or call em docstrings
"""
- To define a docstring, include a string as the first line of a module, function, class or method definition
- The docstring is displayed when someone uses the
help()
function to determine what our code does, or we also can use function_name.__doc__
def func():
"""This is a function that does nothing at all"""
return
help(func)
print(func.__doc__)
>Defining main()
as we do in C
- It isn't required, because Python automatically begins executing any Pythonic commands that are defined in the script, but is a best practice to put the main body of our script into the
main()
function
- Can be very helpful if we decide to turn our program into a module that can be imported into another program, we won't need to make so many changes.
- The
if __name__ == "__main__":
compares variable __name__
to the string __main__
, it checks if the script is being executed as the main program is is it imported into another program
Some cool shit I learnt
>Base-2 and Base-16 Assignment
>Padding the binary upto 8 places with zeroes if less