Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, March 9, 2017

Inheritance Python

Inheritance is the process of reusing the existing code .
Define new classes from the old one i.e including all features of old classes.

Following Program will illustrate the concept:

Code::


class bankaccount:
    def __init__ (self):
        self.bal=int(input("Enter Initial Balance:"))
   
    def withdraw(self,amt):
        if amt<self.bal:
            self.bal=self.bal-amt
        else:
            raise ValueError ("Insufficient Funds")
   
    def deposit(self,amt):
        self.bal=self.bal+amt
   
    def transfer(self,amt,toacc):
        try:
            self.withdraw(amt)
            toacc.deposit(amt)
        except ValueError as e:
            print("Try Again",e)
    def __str__(self):
        return"Balance is: "+str(self.bal)

class cheque(bankaccount):
    def __init__ (self):
        bankaccount.__init__(self)
        self.cheq={}
    def process(self,amt,number,org):
        try:
            self.withdraw(amt)
        except ValueError as e:
            print("Cheque Returned for",e)
        self.cheq[number]=(org,amt)
    def cheqinfo(self,number):
        if self.cheq.get(number):
            print(self.cheq[number])
        else:
            print("No Such Cheque")
    def withdraw(self,amt):
        print('Withdrawing')
        bankaccount.withdraw(self, amt)

nt=bankaccount()        #object of bankaccount type created
nt.deposit(1500)
print(nt)
nt.withdraw(1500)
print(nt)
nti=bankaccount()          #new object created
nt.transfer(1500, nti)
print(nt,nti)

dn=cheque()
dn.process(1500, 1001, 'nearur')

dn.cheqinfo(1001)

Output::

Thursday, February 16, 2017

Telephone Database (Persistent Variables) Python

Following Code will help in maintaining a telephone database including following commands

1.whois phonenumber -tell the details of a particular Number

2. add phonenumber information-add the new number with the information

3.search keyword-return all numbers having keyword in their information

4.delete-deleting any number from database

5.quit-Halt the application

Code::

import string
import shelve

database=shelve.open("information.db")
print("Commands are: whois,add,search,delete or quit")
c=0
line=input("Command: ")
while line!='quit':
    words=line.lower().split()
    if line=="":
        print("Commands are: whois,add,search,delete or quit")
    elif words[0] =='whois':
        if words[1] in database:
            print(words[1],":",string.capwords(database[words[1]]))
        else:
            print("Not Found Try again!!")
    elif words[0]=='add':
        if words[1] in database:
            print("Already Present")
        else:
            database[words[1]]=" ".join(words[2:])
    elif words[0]=='search':
        for e in database:
            if database[e].find(words[1])!=-1:
                c=1
                print(e,":",string.capwords(database[e]))
                
        if c==0:
            print("Keyword Not found Try again!!")
    elif words[0]=="delete":
        if words[1] in database:del database[words[1]]
        else:print("Not Found Try again!!")
    else:
        print("Command Not Found Please Try again!!")
    line=input("Command: ")
database.close()

Output::

 

Wednesday, February 15, 2017

A Concordance Python

A concordance is an alphabetical listing of the words in a text String,along with the line numbers on which each word occurs.

Code::

def count(line,dct,linenumber):
    for word in line.split():
        lst=dct.get(word,[])
        lst.append(linenumber)
        dct[word]=lst

line=input("Enter text,quit to end: ")
linenumber=0
dct={}
while line!='quit':
    linenumber+=1
    count(line,dct,linenumber)
    line=input()
for el in sorted(dct.keys()):
    print(el,":",dct[el])

Output::

Tuesday, February 14, 2017

Tabulating Club Dues Python

Following Code will tabulate the dues of Members of Club :

Code::

def main():
    fre={}
    print("Enter Dues ,enter quit to end")
    while True:
        line=input()
        if(line=='quit'):break
        words=line.split(':')
        fre[words[0]]=fre.get(words[0],0)+int(words[1])
    for name in fre:
        print(name+" has Paid Rs",fre[name],"in dues")

main()

Output::

Word Frequency Python

Following code will give the frequency of occurrence of words in text:

Code::

def main():
    fre={}
    print("Enter Text ,enter quit to end the text")
    while True:
        line=input()
        if(line=='quit'):break
        for el in line.split():
            fre[el]=fre.get(el,0)+1
    print (fre)

main()

    
Output::

Monday, February 13, 2017

Eliza Python

ELIZA is an early natural language processing computer program created from 1964 to 1966[1] at the MIT Artificial Intelligence Laboratory by Joseph Weizenbaum.[2] Created to demonstrate the superficiality of communication between man and machine.

Code::         #a short example u can increase the conditions 

def reply(line,words):
    if len(words)==0: return "You have to talk to me "
    elif "mother"in words:return "Tell me more about your mother "
    elif "father" in words :return "Tell me more about your father "
    elif words[0]=='i' and words[1]=='feel': return "Why do you feel that way? "
    elif words[-1]=='?': return "What do u want to know? "
    elif words[0]=='i' and words[1]=='think': return "Do you really think so? "
    elif "question" in words:return "What's the question? "
    elif "love" in words:return "I must tell you,Love is Very Complicated Agree? "
    elif "agree" in words:return "Do you agree always with this much ease? "
    elif "no" in words: return"Nice!!,Tell me more "
    else: return "Tell me more "

name=input("Hello, Welcome to NT Therapy Center,What's Your name? ")
print("Type quit to exit")
line=input("Hello "+name+" How can we help you? ")
while line!='quit':
    line=line.lower()
    words=line.split()
    replied=reply(line,words)

    line=input(replied)

Output::

Message Encryption and Decryption Python

Suppose you want to send a message to a friend,but you don't want other people to be able to read it
To do this you must disguise the text in some fashion.This Process is called Encryption.The reverse process is called Decryption.

Code::

def encrypt(msg):
    result=''
    for c in msg:
        result=result+" "+str(ord(c))
    return result

def decrypt(msg):
    result=''
    for c in msg.split():
        result=result+chr(int(c))
    return result


msg=input("Enter Secret Message: ")
emsg=encrypt(msg)
print("Encrypted Message: "+emsg)

print("Decrypted Message: "+decrypt(emsg))

Output::


Another method is using rotate 13 i.e convert the entered character to character 13 place ahead that character.


Code::                                                                                                                                       

def char13(c):
    alph="abcdefghijklmnopqrstuvwxyz"
    index=alph.find(c)
    index=(index+13)%26
    return alph[index]

def encrypt(text):
    result=''
    for c in text.lower():
        if 'a'<=c<='z':result=result+char13(c)
        else: result=result+c
    return result

msg=input("Enter Secret Message: ")
emsg=encrypt(msg)
print("Encrypted Message: "+emsg)
print("Decrypted Message: "+encrypt(emsg))


Output::