diff --git a/ch01/README.md b/ch01/README.md new file mode 100644 index 0000000..56d08a5 --- /dev/null +++ b/ch01/README.md @@ -0,0 +1,6 @@ +Chapter 1 data files +==================== + +The files in this folder are not supposed to work if run. +They serve as source for the book chapters, and to provide a +quick copy/paste tool for whoever would need their content. diff --git a/ch01/bike.py b/ch01/bike.py new file mode 100644 index 0000000..223c483 --- /dev/null +++ b/ch01/bike.py @@ -0,0 +1,22 @@ +# let's define the class Bike +class Bike: + + def __init__(self, colour, frame_material): + self.colour = colour + self.frame_material = frame_material + + def brake(self): + print("Braking!") + +# let's create a couple of instances +red_bike = Bike('Red', 'Carbon fiber') +blue_bike = Bike('Blue', 'Steel') + +# let's inspect the objects we have, instances of the Bike class. +print(red_bike.colour) # prints: Red +print(red_bike.frame_material) # prints: Carbon fiber +print(blue_bike.colour) # prints: Blue +print(blue_bike.frame_material) # prints: Steel + +# let's brake! +red_bike.brake() # prints: Braking! diff --git a/ch01/example/core.py b/ch01/example/core.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/example/run.py b/ch01/example/run.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/example/util/__init__.py b/ch01/example/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/example/util/db.py b/ch01/example/util/db.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/example/util/math.py b/ch01/example/util/math.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/example/util/network.py b/ch01/example/util/network.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/factorial.py b/ch01/factorial.py new file mode 100644 index 0000000..2944556 --- /dev/null +++ b/ch01/factorial.py @@ -0,0 +1,3 @@ +>>> from math import factorial +>>> factorial(5) +120 \ No newline at end of file diff --git a/ch01/files_only/core.py b/ch01/files_only/core.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/files_only/db.py b/ch01/files_only/db.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/files_only/math.py b/ch01/files_only/math.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/files_only/network.py b/ch01/files_only/network.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/files_only/run.py b/ch01/files_only/run.py new file mode 100644 index 0000000..e69de29 diff --git a/ch01/names.py b/ch01/names.py new file mode 100644 index 0000000..2faca7f --- /dev/null +++ b/ch01/names.py @@ -0,0 +1,19 @@ +>>> n = 3 # integer number +>>> address = "221b Baker Street, NW1 6XE, London" # Sherlock Holmes' address +>>> employee = { +... 'age': 45, +... 'role': 'CTO', +... 'SSN': 'AB1234567', +... } +>>> # let's print them +>>> n +3 +>>> address +'221b Baker Street, NW1 6XE, London' +>>> employee +{'age': 45, 'role': 'CTO', 'SSN': 'AB1234567'} +>>> other_name +Traceback (most recent call last): + File "", line 1, in +NameError: name 'other_name' is not defined +>>> diff --git a/ch01/scopes1.py b/ch01/scopes1.py new file mode 100644 index 0000000..4d82c3b --- /dev/null +++ b/ch01/scopes1.py @@ -0,0 +1,14 @@ +# Local versus Global + +# we define a function, called local +def local(): + m = 7 + print(m) + +# we define m within the global scope +m = 5 + +# we call, or `execute` the function local +local() + +print(m) diff --git a/ch01/scopes2.py b/ch01/scopes2.py new file mode 100644 index 0000000..70b115b --- /dev/null +++ b/ch01/scopes2.py @@ -0,0 +1,12 @@ +# Local versus Global + +def local(): + # m doesn't belong to the scope defined by the local function + # so Python will keep looking into the next enclosing scope. + # m is finally found in the global scope + print(m, 'printing from the local scope') + +m = 5 +print(m, 'printing from the global scope') + +local() diff --git a/ch01/scopes3.py b/ch01/scopes3.py new file mode 100644 index 0000000..535a80b --- /dev/null +++ b/ch01/scopes3.py @@ -0,0 +1,20 @@ +# Local, Enclosing and Global + + +def enclosing_func(): + m = 13 + + def local(): + # m doesn't belong to the scope defined by the local + # function so Python will keep looking into the next + # enclosing scope. This time m is found in the enclosing + # scope + print(m, 'printing from the local scope') + + # calling the function local + local() + +m = 5 +print(m, 'printing from the global scope') + +enclosing_func() diff --git a/ch01/virtualenv.creation.txt b/ch01/virtualenv.creation.txt new file mode 100644 index 0000000..1ffaf27 --- /dev/null +++ b/ch01/virtualenv.creation.txt @@ -0,0 +1,65 @@ +Ubuntu 20.04 + +https://www.liquidweb.com/kb/how-to-install-and-update-python-to-3-9-in-ubuntu + +# optional installation steps +fab@fvm:~$ sudo apt-get update +fab@fvm:~$ sudo apt-get install software-properties-common +fab@fvm:~$ sudo add-apt-repository ppa:deadsnakes/ppa +fab@fvm:~$ sudo apt-get update +fab@fvm:~$ sudo apt-get install python3.9 python3.9-venv python3.9-dev + +fab@fvm:~/srv$ mkdir my-project # step 1 +fab@fvm:~/srv$ cd my-project + +fab@fvm:~/srv/my-project$ which python3.9 # check system python +/usr/bin/python3.9 # <-- system python3.9 + +fab@fvm:~/srv/my-project$ python3.9 -m venv lpp3ed # step 2 +fab@fvm:~/srv/my-project$ source ./lpp3ed/bin/activate # step 3 + +# check python again: now using the virtual environment's one +(lpp3ed) fab@fvm:~/srv/my-project$ which python +/home/fab/srv/my-project/lpp3ed/bin/python + +(lpp3ed) fab@fvm:~/srv/my-project$ python # step 4 +Python 3.9.2 (default, Feb 20 2021, 20:56:08) +[GCC 9.3.0] on linux +Type "help", "copyright", "credits" or "license" for more information. +>>> exit() + +(lpp3ed) fab@fvm:~/srv/my-project$ deactivate # step 5 +fab@fvm:~/srv/my-project$ + + +---------------------------------------------------------------------------------------- + +Windows 10 +Simply install from python website, then from terminal: + +C:\Users\Fab\srv>mkdir my-project # step 1 + +C:\Users\Fab\srv>cd my-project + +C:\Users\Fab\srv\my-project>where python # check system python +C:\Users\Fab\AppData\Local\Programs\Python\Python39\python.exe +C:\Users\Fab\AppData\Local\Microsoft\WindowsApps\python.exe + +C:\Users\Fab\srv\my-project>python -m venv lpp3ed # step 2 + +C:\Users\Fab\srv\my-project>lpp3ed\Scripts\activate # step 3 + +# check python again, now virtual env python is listed first +(lpp3ed) C:\Users\Fab\srv\my-project>where python +C:\Users\Fab\srv\my-project\lpp3ed\Scripts\python.exe +C:\Users\Fab\AppData\Local\Programs\Python\Python39\python.exe +C:\Users\Fab\AppData\Local\Microsoft\WindowsApps\python.exe + +(lpp3ed) C:\Users\Fab\srv\my-project>python # step 4 +Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) +⇢ [MSC v.1928 64 bit (AMD64)] on win32 +Type "help", "copyright", "credits" or "license" for more information. +>>> exit() + +(lpp3ed) C:\Users\Fab\srv\my-project>deactivate # step 5 +C:\Users\Fab\srv\my-project>