Python, like many others, is an Object-Oriented language. This essentially means that you use objects to make a system do things. It's similar to real life. First, imagine a bike. A bike has methods and variables. The gear would be a variable and shifting it up or down would be a method. In Python, we would write it like so:
class Bike:
def ___init___(self):
self.gear = 0
def gearUp(self):
self.gear = self.gear +1
print str(self.gear)
def gearDown(self):
self.gear = self.gear - 1
print str(self.gear)
That would be a bicycle class which could then be initialized with
bi = bicycle()
There, you have a bicycle now. You probably would want to switch gears now. To do that, we'd use the method gearUp() . That way you can change the variable (the bike gear). So, to do this we would write the following:
bi.gearUp()
Which would then output the following:
gear: 1
So then, the full program would be:
class Bike:
def ___init___(self):
self.gear = 0
def gearUp(self):
self.gear = self.gear +1
print str(self.gear)
def gearDown(self):
self.gear = self.gear - 1
print str(self.gear)
bi = bicycle()
bi.gearUp()
bi.gearUp()
bi.gearDown()
That concludes part 3. Thank you for learning from this book!
Note: I won't be updating this book until June, due to schoolwork. Thanks for your understanding!
YOU ARE READING
Computer Programming -- Python (BASICS)
RandomBASICS of Programming in the Python Language. If you want to start programming in Python, this is a good place to start.
