Build a Contact App using Python – Python Projects

Contact app using Python

Hello Pythonists.! Welcome to another great tutorial, How to build a contact app using python project. I hope you definitely like the project. Let’s get started..!

It is like our phone contact app on our mobile, but I didn’t design the interface to it, but you will get the entire functionality of it.

Note: I wrote the entire code as a beginner perspective, you can reduce the no.of lines in it, and also you can optimize it.

What we do in this Python project..?

We do in this python project some of the common operations like below, believe me, it’s not that simple to do it as we talk. They are

  1. Creating Contacts
  2. Updating Contacts
  3. Deleting Contacts
  4. Searching Contacts
  5. Listing Contacts

What is the plan of this python project

Here i gave my plan in a simple manner, that wouldn’t be great but it’s OK to me and you to understand.

# Contact App'
# It is like our mobile Contact APP

''
- create (name,number,email)
     
- update (name or number)

- delete (name or number)

- search (name or number)

- view (name or number) 

'''

What you need to complete this Python Project

You must know at least these things in python to build it.

  • Python Lists
  • Python Classes
  • Functions

If you don’t know classes, here is my extraordinary tutorial on classes in Python.

Classes in Python – A Quick guide with examples

I failed in the contact app project

I am serious, so serious guys and its True. Let me explain how i failed in this python project.

I know python classes, so I implemented this project using python dictionaries. I went a good time with it, but I noticed that, there is a problem with dictionaries if we do this project.

Problem: When you update contacts, it changes the entire list as a single entity. Let me explain this…[ Note: If you do like below]

#
d = {'jp':['jp','jp@gmail.com','9204932000003'], 'dad': ['jp','dad@gmail.com','7384050302']} 

# you can add as many as you want


# I want to update the name of jp

d['jp'] = 'Devpyjp'

# Now it becomes
d = {'jp':'Devpyjp', 'dad': ['jp','dad@gmail.com','7384050302']} 

I realized: Yes, it is a problem, but somehow we can avoid it. we can take that entire value of that key and we can pass the updated data through the list index.

But, I decided to avoid python dictionaries in this scenario and move to Lists , “The Super Hero“.

How to Build a Contact App using Python

Don’t worry guys, you never miss a single line of it. I will give the entire code of this project here, so lets get started.

1. Creating Contacts

To create contacts, first, we need to create a class for it and then we will use a method called to create that takes parameters as

create (name,number,email) # You can add more
class Contact:
    
    def __init__(self):
        self.book = []
        self.names =[]
        self.numbers =[]

    def create(self,name,number,email):
        
        self.book.append([name,number,email])
        self.names.append(name)
        self.numbers.append(number)      
        print(name,'Contact created successfully..!')

Do you surprised why I took another 2 lists names and numbers, they are used for searching, updating, and deleting based on numbers and names.

Note: It allows duplicates also as our mobile contact app, if you have a doubt, please check on your device. If you don’t want to allow them then you can restrict.

2. Updating Contacts

I know it is a long code, as I mentioned in the starting it is for beginners. we need to update a contact based on either name or number as in our mobile, so I implemented both but you can reduce it.

update (name or number)
def update(self, name=False,number=False):
        
        # -------------------------------- Update Using Names ----------------------#    
        if name:
            if name in self.names:
               
                inames = [i for i, x in enumerate(self.names) if x == name]
                   
                print('No.of contacts found:',len(inames),inames)
                c = 1
                for iname in inames:
                    print(c,'name:',self.names[iname],'details:',self.book[iname],'\n')
                    c+=1
                d = 'y'
                while d:
                    inp = input("Press 'y' to continue or exit to 'n':")
                    if inp =='y':
                        nm_i = int(input('Enter index of listed contact above:'))

                        try:
                            data = inames[nm_i-1]
                            print('\nDetails:',self.book[data])
                            print(1,'- Name: ',self.book[data][0])
                            print(2,'- Number: ',self.book[data][1])
                            print(3,'- Email: ',self.book[data][2])
                            print(0,'- Exit')
                            
                            v = 'y'
                            while v:
                                ch = int(input('Enter index of listed contact above:'))
                                if ch == 1:
                                    ch_name = input('Enter name to update:')
                                    self.book[data][0] = ch_name
                                    self.names[data] = ch_name
                                
                                elif ch == 2:
                                    ch_number = input('Enter Number to update:')
                                    self.book[data][1] = ch_number
                                    self.numbers[data] = ch_number
                                elif ch == 3:
                                    ch_email = input('Enter Email to update:')
                                    self.book[data][2] = ch_email
                                elif ch==0:
                                    v = False
                                else:
                                    v= False
                            
                            print('#---------------- Updated contact -------------#')
                            print(self.book[data])
                            print(self.names[data])
                            print(self.numbers[data])
                            
                        except:
                            print('Index not found..!')
                        
                    else:
                        break
            else:
                print('No contact found..!')
                    
    # -------------------------------- Update Using Numbers ----------------------#        
        elif number:
            if number in self.numbers:
                   
                inumbers = [i for i, x in enumerate(self.numbers) if x == number]  
                print('No.of contacts found:',len(inumbers))
                c = 1
                for inumber in inumbers:
                    print(c,'Number:',self.numbers[inumber],'details:',self.book[inumber],'\n') 
                    c+=1
            
            
                d = 'y'
                while d:
                    inp = input("Press 'y' to continue or exit to 'n':")
                    if inp =='y':
                        nm_i = int(input('Enter index of listed contact above:'))

                        try:
                            data = inumbers[nm_i-1]
                            print('\nDetails:',self.book[data])
                            print(1,'- Name: ',self.book[data][0])
                            print(2,'- Number: ',self.book[data][1])
                            print(3,'- Email: ',self.book[data][2])
                            print(0,'- Exit')
                            
                            v = 'y'
                            while v:
                                ch = int(input('Enter index of listed contact above:'))
                                if ch == 1:
                                    ch_name = input('Enter name to update:')
                                    self.book[data][0] = ch_name
                                    self.names[data] = ch_name
                                
                                elif ch == 2:
                                    ch_number = input('Enter Number to update:')
                                    self.book[data][1] = ch_number
                                    self.numbers[data] = ch_number
                                elif ch == 3:
                                    ch_email = input('Enter Email to update:')
                                    self.book[data][2] = ch_email
                                elif ch==0:
                                    v = False
                                else:
                                    v= False
                            
                            print('#---------------- Updated contact -------------#')
                            print(self.book[data])
                            #print(self.names[data])
                            #print(self.numbers[data])
                            
                        except:
                            print('Index not found..!')
                        
                    else:
                        break
            else:
                print('No contact found..!')
        else:
            print('No conatact Found..!') 

These both a almost same, but careful with me before you modify them.

3. Deleting Contacts

Once you understand the above lines, it could be easy for you. Same as above we use both number and name to delete contacts.

delete (name or number)
 #------------------------------ DELETE Contacts --------------------------#
    def delete(self,name=False,number=False):
        
        if name:
            if name in self.names:      
                d = 'y'
                while d:
                    inames = [i for i, x in enumerate(self.names) if x == name]
                    if len(inames) >0:     
                        print('No.of contacts found:',len(inames),inames)
                        c = 1
                        for iname in inames:
                            print(c,'name:',self.names[iname],'details:',self.book[iname],'\n')
                            c+=1       
                    
                        inp = input("Press 'y' to continue or exit to 'n':")
                            
                        if inp =='y':
                            nm_i = int(input('Enter index of listed CONTACT above:'))

                            try:
                                data = inames[nm_i-1]
                                print('\nDetails:',self.book[data])
                                    
                                f = input('Are you sure want to DELETE..? (y/n):')
                                if f =='y':
                                    del self.book[data]
                                    del self.numbers[data]
                                    del self.names[data]
                                    
                                print('#---------------- Updated contact Book -------------#') 
                                print('\nDetails:',self.book)
                                print('\nName Entries:',self.names)
                                print('\nNumber Entries:',self.numbers)
                            except:
                                print('\nIndex not found..!')
                        else:
                            break
                    else:
                        print('0 contacts found..!')  
                        break 
            else:
                print('Contact not found..!')        
                    
                
        elif number:
            if number in self.numbers:  
                #inumbers = [i for i, x in enumerate(self.numbers) if x == number]
                    
                d = 'y'
                while d:
                    
                    inumbers = [i for i, x in enumerate(self.numbers) if x == number]
                    if len(inumbers) >0:  
                        c = 1
                        for inumber in inumbers:
                            print(c,'number:',self.numbers[inumber],'details:',self.book[inumber],'\n')
                            c+=1
                            
                        inp = input("Press 'y' to continue or exit to 'n':")
                        if inp =='y':
                            nm_i = int(input('Enter index of listed CONTACT above:'))

                            try:
                                data = inumbers[nm_i-1]
                                print('\nDetails:',self.book[data])
                                    
                                f = input('Are you sure want to DELETE..? (y/n):')
                                if f =='y':
                                    del self.book[data]
                                    del self.numbers[data]
                                    del self.names[data]
                                    
                                print('#---------------- Updated contact Book -------------#') 
                                print('\nDetails:',self.book)
                                print('\nName Entries:',self.names)
                                print('\nNumber Entries:',self.numbers)
                            except:
                                print('\nIndex not found..!')
                        else:
                            break  
                    else:
                        print('0 contacts found..!')  
                        break  
            
            else:
                print('No contact Found..!')
                        
        
        else:
            print('No conatact Found..!')  
        

4. Listing Contacts

It is very simple task, we will show the entire Phone book using the below method.

view (name or number) 
 #------------------------- PhoneBook ---------------------------#
    def phonebook(self):
        print('\nTotal Contacts found:',len(self.book))
        for contact in self.book:
            print('\nNAME:',contact[0])
            print('Phone Number:',contact[1])
            print('Email:',contact[2])

It will show the entire phone book what we created in the last method.

5. Search contacts

This is one of the most important tasks in our Contact app, here we need to search for contacts based on name and number.

search (name or number)
#-------------------------------------- Search Contact -----------------------------# 
    def search(self,number=False,name=False):
        
        if name:
            if name in self.names:
                inames = [i for i, x in enumerate(self.names) if x == name]
            
                c = 1
                for iname in inames:
                    print(c,'name:',self.names[iname],'details:',self.book[iname],'\n')
                    c+=1
            else:
                print('No contact found..!')
        
        elif number:
            if number in self.numbers:
                inumbers = [i for i, x in enumerate(self.numbers) if x == number]
                c = 1
                for inumber in inumbers:
                    print(c,'number:',self.numbers[inumber],'details:',self.book[inumber],'\n')
                    c+=1

            else:
                print('No contact found..!')
        else:
                print('No contact found..!')

Ohhh! we finally completed our all tasks successfully.. .! I was really HAPPY to do this and share with you, before closing it, I will give you another great code along with entire project code. Don’t scare to see this, you can optimize it.


class Contact:
    
    def __init__(self):
        self.book = []
        self.names =[]
        self.numbers =[]

    def create(self,name,number,email):
        
        self.book.append([name,number,email])
        self.names.append(name)
        self.numbers.append(number)      
        print(name,'Contact created successfully..!')
        
    def update(self, name=False,number=False):
        
        # -------------------------------- Update Using Names ----------------------#    
        if name:
            if name in self.names:
               
                inames = [i for i, x in enumerate(self.names) if x == name]
                   
                print('No.of contacts found:',len(inames),inames)
                c = 1
                for iname in inames:
                    print(c,'name:',self.names[iname],'details:',self.book[iname],'\n')
                    c+=1
                d = 'y'
                while d:
                    inp = input("Press 'y' to continue or exit to 'n':")
                    if inp =='y':
                        nm_i = int(input('Enter index of listed contact above:'))

                        try:
                            data = inames[nm_i-1]
                            print('\nDetails:',self.book[data])
                            print(1,'- Name: ',self.book[data][0])
                            print(2,'- Number: ',self.book[data][1])
                            print(3,'- Email: ',self.book[data][2])
                            print(0,'- Exit')
                            
                            v = 'y'
                            while v:
                                ch = int(input('Enter index of listed contact above:'))
                                if ch == 1:
                                    ch_name = input('Enter name to update:')
                                    self.book[data][0] = ch_name
                                    self.names[data] = ch_name
                                
                                elif ch == 2:
                                    ch_number = input('Enter Number to update:')
                                    self.book[data][1] = ch_number
                                    self.numbers[data] = ch_number
                                elif ch == 3:
                                    ch_email = input('Enter Email to update:')
                                    self.book[data][2] = ch_email
                                elif ch==0:
                                    v = False
                                else:
                                    v= False
                            
                            print('#---------------- Updated contact -------------#')
                            print(self.book[data])
                            print(self.names[data])
                            print(self.numbers[data])
                            
                        except:
                            print('Index not found..!')
                        
                    else:
                        break
            else:
                print('No contact found..!')
                    
    # -------------------------------- Update Using Numbers ----------------------#        
        elif number:
            if number in self.numbers:
                   
                inumbers = [i for i, x in enumerate(self.numbers) if x == number]  
                print('No.of contacts found:',len(inumbers))
                c = 1
                for inumber in inumbers:
                    print(c,'Number:',self.numbers[inumber],'details:',self.book[inumber],'\n') 
                    c+=1
            
            
                d = 'y'
                while d:
                    inp = input("Press 'y' to continue or exit to 'n':")
                    if inp =='y':
                        nm_i = int(input('Enter index of listed contact above:'))

                        try:
                            data = inumbers[nm_i-1]
                            print('\nDetails:',self.book[data])
                            print(1,'- Name: ',self.book[data][0])
                            print(2,'- Number: ',self.book[data][1])
                            print(3,'- Email: ',self.book[data][2])
                            print(0,'- Exit')
                            
                            v = 'y'
                            while v:
                                ch = int(input('Enter index of listed contact above:'))
                                if ch == 1:
                                    ch_name = input('Enter name to update:')
                                    self.book[data][0] = ch_name
                                    self.names[data] = ch_name
                                
                                elif ch == 2:
                                    ch_number = input('Enter Number to update:')
                                    self.book[data][1] = ch_number
                                    self.numbers[data] = ch_number
                                elif ch == 3:
                                    ch_email = input('Enter Email to update:')
                                    self.book[data][2] = ch_email
                                elif ch==0:
                                    v = False
                                else:
                                    v= False
                            
                            print('#---------------- Updated contact -------------#')
                            print(self.book[data])
                            print(self.names[data])
                            print(self.numbers[data])
                            
                        except:
                            print('Index not found..!')
                        
                    else:
                        break
            else:
                print('No contact found..!')
        else:
            print('No conatact Found..!') 


    #------------------------------ DELETE Contacts --------------------------#
    def delete(self,name=False,number=False):
        
        if name:
            if name in self.names:      
                d = 'y'
                while d:
                    inames = [i for i, x in enumerate(self.names) if x == name]
                    if len(inames) >0:     
                        print('No.of contacts found:',len(inames),inames)
                        c = 1
                        for iname in inames:
                            print(c,'name:',self.names[iname],'details:',self.book[iname],'\n')
                            c+=1       
                    
                        inp = input("Press 'y' to continue or exit to 'n':")
                            
                        if inp =='y':
                            nm_i = int(input('Enter index of listed CONTACT above:'))

                            try:
                                data = inames[nm_i-1]
                                print('\nDetails:',self.book[data])
                                    
                                f = input('Are you sure want to DELETE..? (y/n):')
                                if f =='y':
                                    del self.book[data]
                                    del self.numbers[data]
                                    del self.names[data]
                                    
                                print('#---------------- Updated contact Book -------------#') 
                                print('\nDetails:',self.book)
                                print('\nName Entries:',self.names)
                                print('\nNumber Entries:',self.numbers)
                            except:
                                print('\nIndex not found..!')
                        else:
                            break
                    else:
                        print('0 contacts found..!')  
                        break 
            else:
                print('Contact not found..!')        
                    
                
        elif number:
            if number in self.numbers:  
                #inumbers = [i for i, x in enumerate(self.numbers) if x == number]
                    
                d = 'y'
                while d:
                    
                    inumbers = [i for i, x in enumerate(self.numbers) if x == number]
                    if len(inumbers) >0:  
                        c = 1
                        for inumber in inumbers:
                            print(c,'number:',self.numbers[inumber],'details:',self.book[inumber],'\n')
                            c+=1
                            
                        inp = input("Press 'y' to continue or exit to 'n':")
                        if inp =='y':
                            nm_i = int(input('Enter index of listed CONTACT above:'))

                            try:
                                data = inumbers[nm_i-1]
                                print('\nDetails:',self.book[data])
                                    
                                f = input('Are you sure want to DELETE..? (y/n):')
                                if f =='y':
                                    del self.book[data]
                                    del self.numbers[data]
                                    del self.names[data]
                                    
                                print('#---------------- Updated contact Book -------------#') 
                                print('\nDetails:',self.book)
                                print('\nName Entries:',self.names)
                                print('\nNumber Entries:',self.numbers)
                            except:
                                print('\nIndex not found..!')
                        else:
                            break  
                    else:
                        print('0 contacts found..!')  
                        break  
            
            else:
                print('No contact Found..!')
                        
        
        else:
            print('No conatact Found..!')  
        
    #------------------------- PhoneBook ---------------------------#
    def phonebook(self):
        print('\nTotal Contacts found:',len(self.book))
        for contact in self.book:
            print('\nNAME:',contact[0])
            print('Phone Number:',contact[1])
            print('Email:',contact[2])
    
    #-------------------------------------- Search Contact -----------------------------# 
    def search(self,number=False,name=False):
        
        if name:
            if name in self.names:
                inames = [i for i, x in enumerate(self.names) if x == name]
            
                c = 1
                for iname in inames:
                    print(c,'name:',self.names[iname],'details:',self.book[iname],'\n')
                    c+=1
            else:
                print('No contact found..!')
        
        elif number:
            if number in self.numbers:
                inumbers = [i for i, x in enumerate(self.numbers) if x == number]
                c = 1
                for inumber in inumbers:
                    print(c,'number:',self.numbers[inumber],'details:',self.book[inumber],'\n')
                    c+=1

            else:
                print('No contact found..!')
        else:
                print('No contact found..!')


    
#c = Contact()
#c.create('jp','91245635','jp@gmail.com')  
#c.create('sam','06570354647','jp@gmail.com') 
#c.create('jp','94567896','jp@gmail.com') 

#c.phonebook()

#c.delete('jp') 
#c.delete(number='94567896') 

#c.update(name='jp')    
#c.update(number='94567896')     

menu ='''
          1.Create
          2.Update
          3.ListContacts
          4.Search
          5.Delete
          6.Exit
'''

if __name__ == "__main__":
    
    c = Contact()
    
    a = True
    while a:
        print('\n',menu,'\n')
        choice = int(input('\nEnter your Choice:'))
        if choice ==1:
            name = input('Enter Name:')
            phone = input('Enter phone number:')
            email = input('Enter your email:')
            c.create(name,phone,email)
        elif choice == 2:
            k =True
            while k:
                print(
                    '''
                    1. Update through name
                    2. Update through number
                    3. Exit
                    '''
                )
                inp = int(input('Enter input:'))
                
                if inp ==1:
                    name = input('Enter name:')
                    c.update(name=name)
                elif inp ==2:
                    number = input('Enter Number:')
                    c.update(number=number)
                else:
                    k = False
        elif choice == 3:
            c.phonebook()
            
        elif choice == 4:
            k =True
            while k:
                print(
                    '''
                    1. Search through name
                    2. Search through number
                    3. Exit
                    '''
                )
                inp = int(input('Enter input:'))
                
                if inp ==1:
                    name = input('Enter name:')
                    c.search(name=name)
                elif inp ==2:
                    number = input('Enter Number:')
                    c.search(number=number)
                else:
                    k = False
        
        elif choice == 5:
            k =True
            while k:
                print(
                    '''
                    1. Delete through name
                    2. Delete through number
                    3. Exit
                    '''
                )
                inp = int(input('Enter input:'))
                
                if inp ==1:
                    name = input('Enter name:')
                    c.delete(name=name)
                elif inp ==2:
                    number = input('Enter Number:')
                    c.delete(number=number)
                else:
                    k = False
        elif choice == 6:
            a = False
        else:
            break
                       
                    
            
       

I hope it will give a good intuition about python classes and also it helps a lot in your python practice. To get more like this please subscribe to our newsletter now. Thank you.

1 thought on “Build a Contact App using Python – Python Projects”

Leave a Reply