Python magic number

Magic number is number, when sum of digits is reversed and  multiplied with sum of digits it gives original number. For example 1729   magic.py number=int(input(“Please enter number: “)) dsum=0 reverse=0 temp=0 i=number while i>0: digit=i%10 dsum=dsum+digit i=i//10 temp=dsum temp1=dsum while temp>0: digit1=temp%10 reverse=reverse*10+digit1 temp=temp//10 product=temp1*reverse if product==number: print(number , ” is magic number”) else:Continue reading “Python magic number”

Python list pop example

lp.py books=[‘Algebra’,’Geometry’,’Physics’,’Chemistry’] print(“List of books”) print(books) books.append(‘Biology’) books.append(‘English’) print(“\nList of books after adding two more books”) print(books) print(“\nRemove last book from list”) books.pop() print(“\nList of books after removing last book”) print(books) print(“\nRemove last book from list”) books.pop() print(“\nList of books after removing last book”) print(books) Output        

Python function with list as parameter

Using list as parameter in Python user defined function. funlist.py def week(list):  lst.append(“Sunday”)  lst.append(“Monday”)  lst.append(“Tuesday”)  lst.append(“Wednesday”)  lst.append(“Thursday”)  lst.append(“Friday”)  lst.append(“Saturday”)  return list def rem( list,day):  list.remove(day)  return list lst=[] print (“Empty list”) print (lst) w= week(lst) print(“List population”) print(w) print(“Remove day from week”) day = input(“Please enter day to be removed: “) lst=rem(lst,day) print(“List after removingContinue reading “Python function with list as parameter”

Python class variable

student.py class Student: totalStudents=0 schoolName=”XYZ” def __init__(self,name,rollnum): self.name=name self.rollnum=rollnum Student.totalStudents+=1 def show(self): print(“Name:”,self.name,”-“,”Roll No:”,self.rollnum) s1=Student(“Julia”,21) s2=Student(“Ema”,22) s3=Student(“Megan”,23) s4=Student(“Miley”,24) print(“School : “,Student.schoolName) print() print(“Students listing…”) print() s1.show() s2.show() s3.show() s4.show() print() print (“Total Students :”,Student.totalStudents) Output  

Python class example

  customer.py class customer: def __init__(self,id,name,email,contact,type): self.id=id self.name=name self.email=email self.contact=contact self.type=type def show(self): print(“Id: “,self.id,”Name: “,self.name,”Email: “,self.email,”Contact: “,self.contact,”Type: “,self.type) print (“Customer listing”) c1=customer(“100″,”John”,”j@test.com”,9999999999,”Online”) c2=customer(“101″,”Adam”,”a@test.com”,888888888,”Retail”) c3=customer(“102″,”Will”,”w@test.com”,7777777777,”Retail”) c4=customer(“103″,”Smith”,”s@test.com”,5555555555,”online”) c5=customer(“104″,”Robert”,”r@test.com”,4444444444,”online”) c1.show() c2.show() c3.show() c4.show() c5.show() Output      

Python automorphic number

auto.py Automorphic number when it is squared,the last number in square contains original number. For example 5 is automorphic number Square of 5 = 25  ( last number is 5 i.e. original number) iSquare=0; iPower=0; iRem = 0; iNumber = input(“Please enter a number: “) iPower = len(iNumber) iSquare = int(iNumber) ** 2; iNumber =Continue reading “Python automorphic number”

Python duck number

Duck number contains at least one occurrence of 0 as digit For example 101 101 contains 0 as digit. duck.py iDigit=0 iFlag=0 i=0 //Input number iNumber=int(input(“Please enter a number: “)) //Store number in vaiable i i=iNumber //Extract digit and scan it for 0 while i>0:      iDigit=i%10      if iDigit==0:       Continue reading “Python duck number”