Classes in Python

In this video, you’ll be introduced to object-oriented programming using Python. You’ll learn classes and objects and how to create and use them in your Python applications.

Following are two examples of classes in Python. The Course class has a constructor (def __init__() ) explicitly defined whereas the Car class doesn’t have a constructor defined. Any class that doesn’t have a constructor explicitly defined will use Python’s default empty constructor.

# Composition example

class Course:
  def __init__(self, cid, title, credits):
    self._cid = cid
    self._title = title
    self._credits = credits
class Car:
  def start_engine(self):
    print("Starting engine...")

Want to learn more? My recommendations:

Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code (Zed Shaw’s Hard Way Series)

Head First Python: A Brain-Friendly Guide

Introduction to Computing and Programming in Python (3rd Edition)

Verified by MonsterInsights