In this article, we will learn the introduction of python Object-oriented programming.
Hello All, today we are starting object-oriented programming with python.
Start learning Python from scratch for Free.
- Introduction to Programming – Day1
- Introduction to Algorithms – Day 2
- Python operators and variables – Day 3
- Python Control Structures – Day 4
- Functions in Python – Day 5 of Python Series
- How to setup Eclipse IDE for Python? – 6th Day of Python Series
- Collection Data types in Python – 7th Day of Python Series
- Python Debugging and Testing –Python Series-8th day
- Python Built-in Functions, Libraries, and modules – Day 9
- Python regular expressions and lambda functions – Day 10
- Introduction of Python Object-oriented programming(Python Oops) – Day 11
So far you have been coding quite a bit in Python, and are familiar with its syntax. You have written small programs using functions. What if you have to write a real-world complex application like an online shopping app? How would you go about doing it and where you will start?
In this course, you are going to learn how to build complex real-world applications using a technique known as object-oriented programming.
Python supports multiple approaches to solve a problem with, so-called multiparadigm programming approach.
Object-oriented programming is used to solve real-life problems and it is easier to relate to real-life problems in object-oriented programming.
Object oriented programming concepts
Let’s see by example.
Suppose we want to make an online shopping app on which we can sell shoes and mobile and accept the return of these products.
Our current way of programming could represent the logic like this, but it is very complicated and it is also not able to cover all logic.
Current way of programming(structured programming)
total_price_mobile = 0 total_price_shoe = 0 def purchase_mobile(price,brand): global total_price_mobile if brand == "Apple": discount = 10 else: discount = 5 total_price_mobile = price - price * discount / 100 print("Total price for Mobile is "+str(total_price_mobile)) def purchase_shoe(price,material): global total_price_shoe if material == "leather": tax = 5 else: tax = 2 total_price_shoe = price + price * tax / 100 print("Total price for Shoe is "+str(total_price_shoe)) def return_mobile(): print("Refund price for Mobile is ",total_price_mobile) def return_shoe(): print("Refund price for Shoe is ",total_price_shoe) purchase_mobile(20000,"Apple") purchase_shoe(200,"leather") purchase_mobile(2000,"Samsung") return_mobile()
We can see that with our current style of programming, we quickly run into complications trying to simulate real world scenarios, like purchasing and returning a product.
The problem arises due to the fact that in real life everything has some data/characteristic associated with it and some behavior associated with it and we are not able to replicate this in a code. For example:
- All mobiles have price and brand as its data and purchase and return as its behavior.
- All shoes have price and material as its data and purchase and return as its behavior.
We need a way of programming which allows to club together the data and behavior so that it becomes easier to code real world scenarios.
Object oriented programming
In object-oriented programming, we make a template for a problem so we can relate it with real-life scenarios.
Classes and object
Objects are real world entities. Anything you can describe in this world is an object. Classes on the other hand are not real. They are just a concept. Class is a short form of Classification. A class is a classification of certain objects and it is just a description of the properties and behavior all objects of that classification should possess.
When we can use a single function with different signatures for different purposes called polymorphism. More About polymorphism – click here
When we want to secure our code for unwanted or accidental change then we can secure Methods and attributes using double underscore(__).
More about Encapsulation – click here
Sometimes we need to inherit properties of any class to another class for increasing it’s functionality. This process of inheriting properties of other classes to the current class is called Inheritance.
Examples on introduction to python programming
If you like this article, please share it with your friends. if you have any question regarding this article feel free to ask anytime. we will answer as soon as possible.
comment with your question, we will reply as soon as possible.
GIPHY App Key not set. Please check settings