Learn practical skills, build real-world projects, and advance your career
!pip install jovian --upgrade --quiet
class Snake:
    name = "python"
    
    def change_name(self, new_name): #first argument in method is always self
        self.name = new_name

snake = Snake()
print(snake.name)

snake.change_name("anaconda")
print(snake.name)
python anaconda
class Snake:
    
    def __init__(self, name): # dunder init runs when class is instantiated. It is taking parameter called name
        self.name = name #this says for the class Snake (self) there will be a name variable set to the argument 
        #called name from the init method

    def change_name(self, new_name): #this function will only run if its called but it takes an argument called new_name
        #and sets the name variable created in init method to the new name
        self.name = new_name
        
python = Snake("python") #python variable hands the class snake the argument python which gets set to a name variable
anaconda = Snake("anaconda") #Anaconda variable has the class snake and its name is anaconda

python.change_name("Cobra") #we use the dot operator on our python variable to access the change_name function
#we pass it the parameter Cobra which is the new_name. The new_name replaces the original name

print(python.name) # This will return "Cobra" because we changed the name of python after creating it. 

print(anaconda.name) #This will simply return "anaconda" because that is the argument we passed.
Cobra anaconda
def make_account(): # This is a basic function that when called wil return a "balance" of 0
    return {'balance': 0}

def deposit(account, amount): #This function requires two arguments to be passed. The first is the account and the second
    #is the amount to be deposited.
    account['balance'] += amount
    return account['balance']

def withdraw(account, amount): # This function is the same as deposit, but instead of adding money we are subtracting it.
    account['balance'] -= amount
    return account['balance']
a = make_account()
b = make_account()
deposit(a, 100)

deposit(b, 50)

withdraw(b, 10)

withdraw(a, 10)
90
#Inheritance Practice

class Rocket:
    def __init__(self, name, distance): # This is the base class it has parameters called name and distance
        self.name = name
        self.distance = distance

    def launch(self): #Method inside the class that will return a string with the name and distance variables included in it
        return "%s has reached %s" % (self.name, self.distance)


class MarsRover(Rocket): # inheriting from the base class
    def __init__(self, name, distance, maker): #when MarsRover is Instatiatied it will require 3 arguments to be given
        Rocket.__init__(self, name, distance) #These two parameters are coming from base class
        self.maker = maker # This is the new parameter unique to MarsRover

    def get_maker(self): #Method
        return "%s Launched by %s" % (self.name, self.maker) # returns string of the name and maker variables


if __name__ == "__main__": #?
    x = Rocket("simple rocket", "till stratosphere") #variable named x instantiates rocket class passes 2 arguments
    y = MarsRover("mars_rover", "till Mars", "ISRO") # variable named y instantiates the MarsRover class and passes 3 arguments
    print(x.launch()) # Looks for the method called launch inside the rocket class and prints the return value
    print(y.launch()) # looks for method called launch inside MarsRover class but uses inheritance and gets it from Rocket class
    print(y.get_maker()) # runs the method get maker inside Marsrover and returns 2 arguments that are passed
simple rocket has reached till stratosphere mars_rover has reached till Mars mars_rover Launched by ISRO