
- 22 students
- 41 lessons
- 0 quizzes
- 10 week duration
-
Python Basics
- Installing Anaconda and running a sample Python Program
- How to use the Print function in Python?
- Arithmetic Operations in Python
- Running a Web Server using Python in Spyder
- Input Output in Python
- Starting Plots in Python
- For loop in Python
- Installing and running DJango
- Linear and Polynomial Regression using Python
- Some Problems on Lists
- Creating Classes in Python
- Inheritance in Python
- How to implement Multiple Inheritance in Python
- Using Flask-1
- Lists in Python
- Lists in Python — 1
- Sorting of Lists
- Lists in Python — Insertions
- How to make a list in python and save it in files ……
- Importing Excel Files in Python
- Mailer in Python
- How to add a simple view to a Django site?
- Python Print and I/O questions
- Print answers
- If then else in Python
- Summation of Series
- Python Print Assignment
- Loop Assignments
- Pickle in Python
- Dictionary in Python-1
- Dictionaries in Python-2
- Python OOPs Assignment
- Operator Overloading in Python
- Equation Solving using Numpy
- Starting Numpy
- Passing Data from a Python Controller to a HTML Page
- Downloading Data from a URL in Python
- Sending a Dictionary to a HTML Template and displaying it using Flask.
- Web Scraping Library-1
- Downloading data from a remote website and displaying data in an html page
- Arithmetic Operations in Python — Assignment
Creating Classes in Python
We start with the obvious question?
What is a class
Imagine that you are developing a piece of machinery. Maybe a car. How would you go about developing it?
First of all we will define the car as a finished product. List out all the components of the car.
Steering wheel, wheels, tyre, seats, doors etc.
Furthermore we will divide these smaller components into still smaller components.
Thus the steering wheel itself would be collection of components. These smaller components would be made up of still smaller components and so on. All these components are Objects. This is Object Oriented Programming.
How will these Objects be made?
We will make designs of these objects on paper and write down the parts needed and the functions of the parts.
Car
{
- Steering Wheel
- Steers the car by rotating the front wheels.
- Wheels
- Front Wheel
- Rear Wheel
- Doors
A class in Python is created by using the class keyword.
We shall write
class ClassName:
Body of the class.
class Book: #Class Body Here
Next, we will create a constructor and a toString method. The constructor is used to create an object of the class. In Python the constructor is written as __init__ , and will contain a list of parameters start with self. Self is the name of the this pointer in Python.
class Book: def __init__(self,bookname,bookprice,booksubject): self.bookname=bookname self.bookprice=bookprice self.booksubject=booksubject b1=Book("Basic C",150,"C") print(b1)
Output
<__main__.Book object at 0x000000955E661198>
To get a proper output create a __str__ method in the class that returns a string.
class Book: def __init__(self,bookname,bookprice,booksubject): self.bookname=bookname self.bookprice=bookprice self.booksubject=booksubject def __str__(self): return "Name={0}, Price={1}, Subject={2}".format(self.bookname,self.bookprice,self.booksubject) b1=Book("Basic C",150,"C") print(b1)
Output
Name=Basic C, Price=150, Subject=C
end