Untitled

#example taken from the net
class Employee:
    company='Tutorial Gateway'
    
    def __init__(self):
        print('Hello World')
    def __init__(self):
        print('Welcome to programming Language')

emp1 = Employee()

print(emp1.company)
emp1.func_message()
#to avoid outsider modify your code
setName()
getName()
#Example
class Book:
    def __init__(self,title,publisher,price,pub_date,isbn):
        self.title = title
        self.publisher = publisher
        self.price = price
        self.pub_date = pub_date
        self.isbn = isbn
    def showInfo(self):
        print('book title: {}'.format(self.title))

book1 = Book('python','GoTop',483,'2022-02-15','123456789')
book1.showInfo()

Untitled

hello was printed proving the class has been created

Untitled

Untitled

#Example, let the user name the book
class Book:
    def __init__(self,bTitle):
        self.title = bTitle

if __name__=='__main__':
    book1 = Book(str(input()))
	    print('book name:',book1.title)
#Example, let the user name the book and publisher 
class Book:
    def __init__(self,bTitle,publisher):
        self.title = bTitle
        self.publisher = publisher

if __name__=='__main__':
    book1 = Book(str(input()),str(input()))
    print('book name:',book1.title,'publisher:',book1.publisher)
#Example, let the user name the book and publisher 
class Book:
    def __init__(self,bTitle,publisher,isbn,author,price):
        self.title = bTitle
        self.publisher = publisher
        self.isbn = isbn
        self.author = author
        self.price = price
    def showInfo(self):
        print(self.title,self.publisher,self.isbn,self.author,self.price)

if __name__=='__main__':
    book1 = Book(str(input()),str(input()),str(input()),str(input()),str(input()))
    book1.showInfo()
#Example, let the user name the book and publisher 
class Book:
    def __init__(self,bTitle,publisher,isbn,author,price):
        self.title = bTitle
        self.publisher = publisher
        self.isbn = isbn
        self.author = author
        self.price = price
#to rename book title
    def setTitle(self,newTitle):
        self.title = newTitle
    def showInfo(self):
        print(self.title,self.publisher,self.isbn,self.author,self.price)
if __name__=='__main__':
    book1 = Book(str(input()),str(input()),str(input()),str(input()),str(input()))
    book1.showInfo()
    book1.setTitle(str(input('rename book title:')))
    book1.showInfo()

Untitled

Untitled

to enable encription, the sample above is no longer available till py10

Untitled

recursion in class

Untitled

Untitled

#Example to create a cart, question on the above
class good:
    def __init__(self,name,price):
        self.item = name
        self.price = price
        
    # def p(self):
    #     return self.price
    
    # def n(self):
    #     return self.item
        

class cart:
    def __init__(self):
        self.cart={}

    def add_(self,name,price):
        if name in self.cart:
            self.cart[name] += int(price)
        else :
            self.cart[name] = int(price)
            
    def show(self):
        print(self.cart)
    
    def count(self):
        b=0
        for i in self.cart:
            b=b+self.cart[i]
        print(b)
        b=0
        
    def clear(self):
        self.cart=self.cart.clear()

if __name__=='__main__':
    mycart = cart()
    
    i1 = good('bag',100)
    i2 = good('car',10000)
    i3 = good('pen',11)
    
    while True:
        a = int(input("if add to cart press 1, 2 for show cart, 3 for count price, 4 to clear cart, 5 to check out:"))

        if a == 1:
            c = input('1 for bag, 2 for car, 3 for pen')
            if int(c) == 1:
                mycart.add_(i1.item,i1.price)
            if int(c) == 2:
                mycart.add_(i2.item,i2.price)
            if int(c) == 3:
                mycart.add_(i3.item,i3.price)
                
        elif a == 2:
            mycart.show()
            
        elif a == 3:
            mycart.count()
            
        elif a == 4:
            try:
                mycart.clear()
            except:
                None
                
        elif a ==5:
            mycart.show()
            print('total Price')
            mycart.count()
            mycart.clear