Module 1
module 0
slides at https://letsdodigital.org/learn/learn-python/module-0/3-python-basics.html.a_function.py
Output
Hello World!
Woo Hoo!
raise
raise_error.py
Other types include:
TypeError
ZeroDivisionError
FileNotFoundError
IndexError
KeyError
exception.py
Output
Conversion failed. Please enter a valid number.
classes
are used to organise code at a higher level.attributes
and actions
of real life objects.classes.py
classes.py
snake_case
CamelCase
classes.py
patient_1_vitals = VitalSigns(37.5, 80)
VitalSigns
class.object
.object oriented programming
.classes.py
__init__
method is automatically called.37.5
and 80
as arguments.classes.py
37.5
is set as the temperature
argument.80
is set as the heart_rate
argument.classes.py
self
refers to the instance
of the class that you have just created, eg patient_1_vitals.self
allows the object you have created to keep track of its own data.self
is ALWAYS passed as an argument to methods of a class.self
is ALWAYS the first argument to methods in a class.classes.py
patient_1_vitals.display()
It may look strange, but the .
(dot) in between patient_1_vitals and display() is basically just a way to get data or use a method of a class.
So in the instance above, you are basically saying:
“For the patient_1_vitals instance that I have created, please run the display() method.”
Remember, a method is just a function inside a class.
class VitalSigns:
def __init__(self, temperature, heart_rate):
self.temperature = temperature
self.heart_rate = heart_rate
def display(self):
print(f"Temperature: {self.temperature}°C")
print(f"Heart Rate: {self.heart_rate} bpm")
patient_1_vitals = VitalSigns(37.5, 80)
patient_1_vitals.display()
Output:
Temperature: 37.5°C
Heart Rate: 80 bpm
pip
There are a large collection of preinstalled libraries in python, so you will not need to use pip
to install them.
eg
os
random
datetime
math
json
importing a module
.dot operator
to access the sub-modules, classes, methods and attributes within the imported module.Lesson 1
.True
and False
, e.g. 1
and 0
s. They are literal thinkers.unpaired quotation mark
, look out for that space
that should not be there, and make sure you match your indentations to your if statements
.Zoom break out rooms
.<> Code
button and then the Codespaces
tab.Create codespace on main
$
. This is just how we show this is the command line):