Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,866 members, 7,802,787 topics. Date: Friday, 19 April 2024 at 09:35 PM

Tutorial: Object Oriented Programming Paradigm For Beginners in Python - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Tutorial: Object Oriented Programming Paradigm For Beginners in Python (2223 Views)

I Need Help In Object Oriented Programming / Object Oriented Programming (OOP) Tutorial / Object Oriented Programming (2) (3) (4)

(1) (Reply) (Go Down)

Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 5:48pm On Apr 14, 2020
Hello Nairalands,this was demanded from my thread "common python programming practice". So in this thread,i will try as much as possible to make OOP understandable for anyone who want to learn it.

So stay tune as the tutorial will be coming
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by koyla: 6:02pm On Apr 14, 2020
Following
gbolly1151:
Hello Nairalands,this was demanded from my thread "common python programming practice". So in this thread,i will try as much as possible to make OOP understandable for anyone who want to learn it.

So stay tune as the tutorial will be coming
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 7:26pm On Apr 14, 2020
Now let me write description of a dog,this will be the basis from which we will learn OOP

DESCRIPTION OF A DOG
Dog is an Animal
It has 4 legs
It has 1 tail
It is fat
It can bark
It can bite
It can run


OOP is a programming paradigm that deal with object and class relationship... That statement look too complex to me so am going to break it down in a way we can quickly grab

What is an object?
Object is anything you can describe e.g the above is the description about Dog,therefore Dog is an object. Noun is an object i.e any name of animal,place or things... Lol i prefer that definition to naming words.other example of object are
man,woman,Hausa,pen,pencil, shoe

What is a class?
Class refer to the framework of an object or noun,in order word what object is made up, it composed of 3 part;

1.name - this is general name of object,that share 'am a', 'is a' or 'is an' relationship with an object
e.g1 man is a person,woman is a person. Person is the class name.
e.g2 Hausa is a tribe,Youruba is a tribe,Igbo is a tribe. Tribe is the class name
In the above description of a dog,we say Dog is an Animal. Animal is the class name

2.Attribute - this hold information about an object,that share 'is' or 'has' relationship with the value e.g it has 4 leg(information about leg),it is fat(information about body) . Look at those statement we can rewrite those as Dog legs is 4, Dog body is fat.
The attribute is legs and value is 4. I.e legs=4
The second attribute is body and value is fat i.e body ='fat'

3.method - this tell us about the ability of the object,what the object 'can' do or 'will' do,it is the function the object will perform in the system. From the description of a dog,its abilities are;
It can bark,it can bite,it can run

Method are bark(),bite(),run()

Now let us decrypt that description of Dog to form a class for object Dog


Object - Dog
Class;
name - Animal
Attribute - Legs,Tail,Body
Method - bark(),bite(),run()

Just snack to chop, write a description about yourself and write out the class name,attribute and method
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 7:26pm On Apr 14, 2020
koyla:
Following
Posted now
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by koyla: 9:03pm On Apr 14, 2020
Thanks much for your effort
gbolly1151:

Posted now

1 Like

Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 9:51pm On Apr 14, 2020
koyla:
Thanks much for your effort
You are welcome sir
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 10:19am On Apr 16, 2020
I will post some description and will like you guys to write the class structure in this way before will move to next topic in inheritance

Class:
Name -
Attributes -
Method -
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 1:10pm On Apr 16, 2020
A lion is a species with a short,rounded head, a reduced neck,round ears, and a hairy tuft at the end of its tail. It has prominent mane,which is the most recognisable feature of the species. They can run,jump and do hunt prey.


Who is following?
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by Predstan: 1:07am On Apr 20, 2020
gbolly1151:

A lion is a species with a short,rounded head, a reduced neck,round ears, and a hairy tuft at the end of its tail. It has prominent mane,which is the most recognisable feature of the species. They can run,jump and do hunt prey.


Who is following?

Object - Lion


Class:
Name - Animal
Attributes - short, rounded head, , reduced neck, round ears, hairy tufts at the ending of its tail
Method - run(), jump(), hunt prey()

1 Like

Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 2:36am On Apr 20, 2020
Predstan:


Object - Lion


Class:
Name - Animal
Attributes - short, rounded head, , reduced neck, round ears, hairy tufts at the ending of its tail
Method - run(), jump(), hunt prey()

Well done boss.... but let me comment on your answer

head is the attribute name while rounded and short are the values same as other attribute name


attribute - head = [short,rounded] <== value
neck=reduce <== value
ears= round <== value
tuff = hairy <==value


You can practice more,just pick up objects around you and describe them

I will start inheritance in next update
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by Predstan: 4:47am On Apr 20, 2020
gbolly1151:


Well done boss.... but let me comment on your answer

head is the attribute name while rounded and short are the values same as other attribute name


attribute - head = [short,rounded] <== value
neck=reduce <== value
ears= round <== value
tuff = hairy <==value


You can practice more,just pick up objects around you and describe them

I will start inheritance in next update

Ok Thanks.
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 10:23am On Apr 21, 2020
gbolly1151:


Object - Dog
Class;
name - Animal
Attribute - Legs,Tail,Body
Method - bark(),bite(),run()

Just snack to chop, write a description about yourself and write out the class name,attribute and method

Before we move to inheritance, let implement this in python


class Animal:
def __init__(self,legs,Tail=True,Body):
self.legs=legs
self.Tail = Tail
Sel.Body = Body

def bark(self):
return 'barking'

def bite(self):
return 'biting'

def run(self):
return 'running'

#let create our object

Dog=Animal (4,Tail=True,'fat')

#you can leave out tail if true and create like this Dog=Animal(4,'fat')
print(Dog.legs)
print(Dog.Tail)
print(Dog.Body)
print(Dog.bark())
print(Dog.run())
print(Dog.bit())

Output:
4
True
fat
barking
running
biting
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by Predstan: 12:38pm On Apr 21, 2020
gbolly1151:


Before we move to inheritance, let implement this in python


class Animal:
def __init__(self,legs,Tail=True,Body):
self.legs=legs
self.Tail = Tail
Sel.Body = Body

def bark(self):
return 'barking'

def bite(self):
return 'biting'

def run(self):
return 'running'

#let create our object

Dog=Animal (4,Tail=True,'fat')

#you can leave out tail if true and create like this Dog=Animal(4,'fat')
print(Dog.legs)
print(Dog.Tail)
print(Dog.Body)
print(Dog.bark())
print(Dog.run())
print(Dog.bit())

Output:
4
True
fat
barking
running
biting

Making sense. Thanks Boss

1 Like

Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 4:46pm On Apr 21, 2020
Object Relationship
Interaction among objects is what lead to a system, the working of a system depends on how these objects interact and these interaction is what we called Relationship. These relationship are of 3 type in OOP;

1. Inheritance
2. Association
3. Composition and Aggregation

1. INHERITANCE : This is the most common relationship among objects that share a relationship 'is a type of' between objects. when two or more objects have slight difference then they must have belong to the same object. When object A,object B,object C are type of Object D we call it inheritance,they are also called generalization.
Examples:
Teaching staff and Non Teaching staff are type of Staff,
saving account and current account are type of bank account
Yoruba,igbo,hausa are type of Tribes.
Parrot and penguin are type of type

Let see a description to understand better

DESCRIPTION OF PARROT
Parrot is a bird
It has feathers
It can fly but can't swim

DESCRIPTION OF PENGUIN
Penguin is a bird
It has feather
It can't fly but can swim

Looking at these two description,we can see that both have common name(bird) and attribute i.e they have feather but different method

To prevent repetition of code,we can create a class bird and allow both parrot and penguin to inherit it. To inherit here ,we mean they should have assess to bird class


Class:
Name - Bird
Attribute - feather

class(bird):
#we are going to inherit the bird's attribute so we won't create attribute for parrot #again
Name - parrot
method - fly()

class(bird):
Name - penguin
method - swin()
#since penguin can't fly,we dont need to include it into the method

#implementation in python
class bird:
def __init__(self):
self.feather= True

class Parrot(bird): #to indicate that we want to inherit bird
def __init__(self):
#calling bird.__init__()(super().__init__()) is necessary to implement
# bird's attribute in parrot
super().__init__()

def fly(self):
return 'i am flying'

class Penguin(bird):
def __init__(self):
super().__init__()

def swim(self):
return 'I am swimming'

myparrot=Parrot()
mypenguin = Penguin()

print(myparrot.feather) # i can call feather from bird because parrot inherit it
print(mypenguin.feather)
print(myparrot.fly())
print(mypenguin.swim())

Output:
True
True
I am flying
I am swimming


This actually help us to structure our code and object
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by kunlegboye(m): 6:58pm On May 02, 2020
Boss we appreciate your efforts a lot,really educative opened my understanding on the concept of oop. ...We are waiting for the remaining ones...and God will bless you mightily for this

1 Like

Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 7:21pm On May 02, 2020
kunlegboye:
Boss we appreciate your efforts a lot,really educative opened my understanding on the concept of oop. ...We are waiting for the remaining ones...and God will bless you mightily for this
Amen sir....I will try my best sir to keep updating the thread
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by yemyke001(m): 7:34pm On May 02, 2020
gbolly1151:

Amen sir....I will try my best sir to keep updating the thread


Following boss.. This is actually great

1 Like

Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 7:43pm On May 02, 2020
yemyke001:



Following boss.. This is actually great
Alright sir
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 12:02pm On May 04, 2020
I dont understand why this forum is giving me on and off....thank God am on today
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by Raalsalghul: 6:48pm On May 04, 2020
gbolly1151:
I dont understand why this forum is giving me on and off....thank God am on today

I want to learn this stuff, but I'm afraid it might get too complex and I'll stop along the way. sad
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 6:34am On May 07, 2020
Guys...anytime i tried to post the composition tutorial, i always get kick on,what may cause this?
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by Predstan: 10:47am On May 07, 2020
.
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by 6ixT8: 5:25pm On May 08, 2020
interesting... gboll1151 where are you
Re: Tutorial: Object Oriented Programming Paradigm For Beginners in Python by gbolly1151(m): 7:06pm On May 09, 2020
6ixT8:
interesting... gboll1151 where are you
Am here, i should have posted the next topic but all the time have been trying to post it,Nairaland will delete it and ban me for a day.....i don't know what is going wrong.....you can get the next topic here on my facebook page

https://www./2604470076495701/permalink/2611250559150986/?app=fbl

(1) (Reply)

Official Thread For C# Experts And Beginners / Contribute What You Know About Artificial Intelligence / Remitly Review - Can I Use It?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 43
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.