Skip to content

Exercise 15 : Class

Alternative course

Python class

Python can create objects. An object is a collection of methods and variables.
Objects are created from classes.

If an object is created, the method __init__ is called. The self keyword is required in methods and variables to tell Python they belong to the class.

Example:

#!/usr/bin/python
class Website:
    def __init__(self,title):
        self.title = title

    def showTitle(self):
        print(self.title)

obj = Website('askpython.com')
obj.showTitle()

What you see on success

The program above creates one object. The objects method showTitle() is called.
class

Study drill

Try the exercises below

1. Using the code above, create another object

2. Add a method to the class: location()

After completing these continue with the next exercise.

Answers

Exercise 17: Time and Date
Published inUncategorized

Be First to Comment

    Leave a Reply