Friday, December 27, 2024

Python - Basics

I have written three python programs in increasing order of complexity.

First one is:

x=int(input("Enter a number"))
y=int(input("Enter another number"))
z=int(input("Enter third number"))
sum=x + y + z
print(sum)


The program reads three numbers (Integers) and finds their sum

class simple:
    def simple_one():
        print("Hello mukesh")
        print("Hello manjunath")


if __name__=="__main__":
    simple.simple_one()


This next program creates a simple class and calls a method on the class object.
The if statement equates __name__ to "__main__" and executes the method if so.

class simple():
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def simple_one(self):
        print("Hello %s %s"%(self.name,self.age))

if __name__=="__main__":
    s = simple("mukesh",45)
    s.simple_one()

This next program above instantiates an object of
class simple by calling its constructor. Then a
method simple_one() on the created object.

1 comment:

  1. Nice article Mukesh ! Keep up the good work and write more in coming days.

    ReplyDelete