Part 3: Concepts

1.4K 21 2
                                        

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've reached the end of published parts.

⏰ Last updated: Oct 18, 2015 ⏰

Add this story to your Library to get notified about new parts!

Computer Programming -- Python (BASICS)Where stories live. Discover now