ch01
This commit is contained in:
@@ -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.
|
||||||
@@ -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!
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
>>> from math import factorial
|
||||||
|
>>> factorial(5)
|
||||||
|
120
|
||||||
@@ -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 "<stdin>", line 1, in <module>
|
||||||
|
NameError: name 'other_name' is not defined
|
||||||
|
>>>
|
||||||
@@ -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)
|
||||||
@@ -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()
|
||||||
@@ -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()
|
||||||
@@ -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>
|
||||||
Reference in New Issue
Block a user