instance variable python code example

Example 1: instance variable in python

class Car:    wheels = 4    # <- Class variable    def __init__(self, name):        self.name = name    # <- Instance variable

Example 2: instance variable python

class Car:    
  wheels = 4    # <- Class variable    
  
  def __init__(self, name):        
  	self.name = name    # <- Instance variable

Example 3: what is a ython instance

An object belonging to a class. e.g. if you had an Employee class, each 
individual employee would be an instance of the Employee class

Example 4: python class

class Dog(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def speak(self):
        print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')

JUB0T = Dog('JUB0T', 55)
Friend = Dog('Doge', 10)
JUB0T.speak()
Friend.speak()

Example 5: class python

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

p1.age = 40

print(p1.age)
---------------------------------------------------------------
40

Example 6: instance method in python

# Instance Method Example in Python 
class Student:
    
    def __init__(self, a, b):
        self.a = a
        self.b = b 
    
    def avg(self):
        return (self.a + self.b) / 2

s1 = Student(10, 20)
print( s1.avg() )