Year 1 Computer Programming - Lecture 6
Year 1 Computer Programming - Lecture 6
(Based on Gaddis, T., Starting out with Python 3e, Pearson Education)
Classes
– Procedures typically operate on data items that are separate from the
procedures
Object-oriented programming is
centered on creating objects rather Object
than procedures.
Attributes (data)
def clock_inner_operation():
for hours in range(24):
for minutes in range(60):
for seconds in range(60):
print(hours, ':', minutes, ':', seconds)
– A minus (-) sign in front of an attribute (or method) signifies private attribute (or method)
– A plus (+) sign in front of an attribute (or method) signifies a public attribute (or method)
– Simple relation (known as association) between two classes is represented by solid lines
Class definition: set of statements that define a class’s methods and data
attributes
– Format: begin with class Class_name:
- Class names often start with uppercase letter
– Method definition like any other python function definition
- self parameter: required in every method in the class – references
the specific object that the method is working on
Initializer method: automatically executed when an instance of the class is
created
– Initializes object’s data attributes and assigns self parameter to the
object that was just created
– Format: def __init__ (self):
Class methods can have multiple parameters in addition to self
• To calculate the running cost per month ( in kWh) you should use
the formula (electricity rate * energy consumption).
Example:
A refrigerator consumes 300 watts and is used for 24
hours per day, and the energy cost is £0.10 per kWh. To
calculate its running cost per month you need to do the
following:
Monthly consumption (kWh) = ((300*24)/1000) * 30
Monthly cost (£)= Monthly consumption (kWh) * £0.10
From above the refrigerator cost £21.60 per month.
CMP4266 , School of CS & DT, Birmingham City University.