It's a small set of hacks to learn the basics of Python. Python is powerful, fast, plays well with others, runs everywhere, is friendly & easy to learn, is Open. I'm using Python 3.7.5 for this project (current LTS).
-
Python comes on a lot of OS's by default but it's more common to find Python 2 instead of Python 3.
-
Python
Virtual Environments
allow you to specify a specific version of Python for that use. Requires first installingvirtualenv
viapip install virtualenv
.Example creation of Python
Virtual Environments
# virtualenv <folder to create> <path to desired python version install> virtualenv py3 -p /usr/local/bin/python3
Example activation of Python
Virtual Environments
# <folder created above via >/bin/activate py3/bin/activate
Example deactivation of Python
Virtual Environments
deactivate
-
Python's package manager
pip
manages dependencies. It leverages arequirments.txt
file detailing project dependencies.Example package install
pip freeze > requirements.txt
Example pip
requirments.txt
filecat requirements.txt
certifi==2019.9.11 chardet==3.0.4 idna==2.8 requests==2.22.0 urllib3==1.25.7
-
pip install
vs.import
?pip install
brings the dependency local for runtime where asimport
uses a looked up asset via putting it somewhere python can expect it to be. -
In Python everything has a id. The id property is immutable.
-
In Python everything has a value.
-
Immutable vs. Mutable Types
Class Description Immutable bool Boolean value ✅ int integer ✅ float floating-point number ✅ list mutable sequence of objects tuple immutable sequence of objects ✅ str character string ✅ set unordered set of distinct objects fronzenset immutable form of set class ✅ dict associative mapping (aka dictionary) b = [] b(id) # 4527447816 b.append(3) print(b) # [3]
a = 4 id(a) # 4321639424 a = a + 1 id(a) # 4321639456
-
Use
None
for null check in Python. Use afoo is None
check for performance vs. slower and more process heavyfoo == None
asfoo is None
checks the id and it exits with less process needed. -
pip
useful commandspip install <package-name>
to install a packagepip list
to list all packages installed to the current virtual environmentpip search <package-name>
can be used to search packages availablepip list --outdated
show out of date dependenciespip uninstall <package-name>
to uninstall package
- Simple
- Forces good styling as there is no braces used
- Key word arguments
def madlibs(name, noun = "shoes", adjective = "red"): return "{0} has {1} {2}".format(name, adjective, noun) madlib = madlibs("Will", adjective="black", noun="boots") print(madlib) # Will has black boots
- Python's package ecosystem doesn't seem to all use Semantic Versioning (example:
idna
as2.8
) - Text file for dependency details feels lackluster
- Underscore as multi word variable convention (
sum_of_two_numbers = 1 + 3
) 😑 global
,nonlocal
, and local varibales are confusing solution that feels clumsier than just having scopes be blocked at first glance. I'm sure there's cool things you could do with it in principal, but I also feel like you'd be a jerk if you actually used them.