Python Program to Get Electronic Configuration by Atomic Number or Symbol

Python Program to Get Electronic Configuration by Atomic Number or Symbol

# Import sleep function
from time import sleep

# Do in 'try' and except not to get error
try:
    # Print heading
    print(" --- :Chemistry with Python = :) :--")
    
    # Global indentifiers used in program
    AtNo = ""
    atno = ""

    # define function to get data from user
    def get_input_at_no():
        global AtNo, atno
        atno = int(
            input("\n \t  Enter Atomic number of element..\n \t  ==================================\n\n\t>>>   "))
        print('')
        AtNo = atno
        sleep(5)


    # define function to get data from user


    def get_input_at_sym():
        global AtNo, atno
        symb = ['H', "He", 'Li', "Be", "B", "C", "N", "O", "F", 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K',
                'Ca', 'Sc', 'Ti', 'V',
                'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr',
                'Nb', 'Mo', 'Tc', 'Ru',
                'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm',
                'Sm', 'Eu', 'Gd', 'Tb',
                'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb',
                'Bi', 'Po', 'At', 'Rn',
                'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr',
                'Rf', 'Ha', 'Unh', 'Ns',
                'Hs', 'Mt', 'Uun', 'Uuu', 'Uub', 'Uut', 'Uuq', 'Uup', 'UUh', 'Uus', "Uuo"]
        symbo = input("\n \t  Enter Atomic symbol of element..\n \t  =================================\n\n\t>>>   ")
        print('')
        if len(symbo) == 1:
            symbo = symbo.capitalize()
        elif len(symbo) == 2:
            symbo = symbo.__getitem__(0).capitalize() + symbo.__getitem__(1).lower()
        elif len(symbo) == 3:
            symbo = symbo.__getitem__(0).capitalize() + symbo.__getitem__(1).lower() + symbo.__getitem__(2).lower()

        AtNo = int(symb.index(symbo)) + 1
        atno = AtNo
        sleep(5)


    # define function for 3d electronic config


    def ele_con():
        global AtNo
        orb = ['1s', '2s', '2p', '3s', '3p', '4s', '3d', '4p', '5s', '4d', '5p', '6s', '4f', '5d', '6p', '7s', '5f',
               '6d', '7p']
        if AtNo == 24:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 5', '4s 1', sep=' , ')
        elif AtNo == 29:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 1', sep=' , ')
        elif AtNo == 41:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6', '4d 4', '5s 1', sep=' , ')
        elif AtNo == 42:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6',
                  '4d 5', '5s 1', sep=' , ')
        elif AtNo == 44:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6',
                  '4d 7', '5s 1', sep=' , ')
        elif AtNo == 45:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6',
                  '4d 8', '5s 1', sep=' , ')
        elif AtNo == 46:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6',
                  '4d 10', '5s 0', sep=' , ')
        elif AtNo == 47:
            print(' 1s 2\n', '2s 2\n', '2p 6\n', '3s 2\n', '3p 6\n', '3d 10\n', '4s 2\n', '4p 6\n',
                  '4d 10\n', '5s 1', sep=' , ')
        elif AtNo == 57:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6', '4d 10', '5s 2',
                  '5p 6',
                  '5d 1', '6s 2', sep=' , ')
        elif AtNo == 58:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6', '4d 10', '5s 2',
                  '5p 6',
                  '4f 1', '5d 1', '6s 2', sep=' , ')
        elif AtNo == 64:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6', '4d 10', '5s 2',
                  '5p 6',
                  '4f 7', '5d 1', '6s 2', sep=' , ')
        elif AtNo == 78:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6', '4d 10', '5s 2',
                  '5p 6',
                  '4f 14', '5d 9', '6s 1', sep=' , ')
        elif AtNo == 79:
            print(' 1s 2', '2s 2', '2p 6', '3s 2', '3p 6', '3d 10', '4s 2', '4p 6', '4d 10', '5s 2',
                  '5p 6',
                  '4f 14', '5d 10', '6s 1', sep=' , ')
        else:
            for i in orb:
                if i.__getitem__(1) == 's':
                    if AtNo <= 2:
                        print(i, AtNo)
                        break
                    else:
                        print(i, 2, end=' , ')
                        AtNo = AtNo - 2
                        if AtNo <= 0:
                            break

                elif i.__getitem__(1) == 'p':
                    if AtNo <= 6:
                        print(i, AtNo)
                        break
                    else:
                        print(i, 6, end=' , ')
                        AtNo = AtNo - 6
                        if AtNo <= 0:
                            break
                elif i.__getitem__(1) == 'd':
                    if AtNo <= 10:
                        print(i, AtNo)
                        break
                    else:
                        print(i, 10, end=' , ')
                        AtNo = AtNo - 10
                        if AtNo <= 0:
                            break
                elif i.__getitem__(1) == 'f':
                    if AtNo <= 14:
                        print(i, AtNo)
                        break
                    else:
                        print(i, 14, end=' , ')
                        AtNo = AtNo - 14
                        if AtNo <= 0:
                            break


    # define function for 2d electronic config


    def elecon():
        global AtNo
        if AtNo <= 2:
            if AtNo == 2:
                print("Noble gas Electronic Configurations: ", AtNo)
            else:
                print("Electronic Configurations: ", AtNo)
        elif 2 < AtNo <= 10:
            if AtNo == 10:
                print("Noble gas Electronic Configurations:  2 ,", (AtNo - 2))
            else:
                print("Electronic Configurations: 2 ,", (AtNo - 2))
        elif 10 < AtNo <= 18:
            if AtNo == 18:
                print("Noble gas Electronic Configurations:  2 , 8 ,", (AtNo - 2 - 8))
            else:
                print("Electronic Configurations: 2 , 8 ,", (AtNo - 2 - 8))
        elif 18 < AtNo <= 36:
            if AtNo == 36:
                print("Noble gas Electronic Configurations:  2 , 8 , 18 ,", (AtNo - 2 - 8 - 18))
            elif 21 <= AtNo <= 30:
                print("Electronic Configurations : Unavailable")
            elif AtNo == 19 or 20:
                print("Electronic Configurations: 2 , 8 , 8 ,", (AtNo - 18))
            else:
                print("Electronic Configurations : 2 , 8 , 18 ,", (AtNo - 2 - 18 - 8))
        elif 36 < AtNo <= 54:
            if AtNo == 54:
                print("Noble gas Electronic Configurations:  2 , 8 , 18 , 18 ,", (AtNo - 2 - 8 - 18 - 18))
            elif 39 <= AtNo <= 48:
                print("Electronic Configurations : Unavailable")
            elif AtNo == 37 or 38:
                print("Electronic Configurations: 2 , 8 , 18 , 18 ,", (AtNo - 36))
            else:
                print("Electronic Configurations: 2 , 8 , 18 , 18 ,", (AtNo - 2 - 8 - 18 - 18))
        elif 54 < AtNo <= 86:
            if AtNo == 86:
                print("Noble gas Electronic Configurations:  2 , 8 , 18 , 32 , 18 ,", (AtNo - 2 - 8 - 18 - 32 - 18))
            elif 57 <= AtNo <= 80:
                print("Electronic Configurations : Unavailable")
            elif AtNo == 55 or 56:
                print("Electronic Configurations: 2 , 8 , 18 ,  32 , 18 ,", (AtNo - 54))

            else:
                print("Electronic Configurations: 2 , 8 , 18 ,  32 , 18 ,", (AtNo - 2 - 8 - 18 - 32 - 18))

        elif AtNo > 87:
            print("Electronic Configurations : Unavailable")


    # define function for symbols of element


    def symbol():
        global AtNo
        symb = ['H', "He", 'Li', "Be", "B", "C", "N", "O", "F", 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K',
                'Ca', 'Sc', 'Ti', 'V',
                'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr',
                'Nb', 'Mo', 'Tc', 'Ru',
                'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm',
                'Sm', 'Eu', 'Gd', 'Tb',
                'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb',
                'Bi', 'Po', 'At', 'Rn',
                'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr',
                'Rf', 'Ha', 'Unh', 'Ns',
                'Hs', 'Mt', 'Uun', 'Uuu', 'Uub', 'Uut', 'Uuq', 'Uup', 'UUh', 'Uus', "Uuo"]
        print('\n Atomic Symbol :', symb.__getitem__(AtNo - 1), "\n\n Electronic Configurations : --")


    # define function for run all code


    def give_the_final_result():
        get_input = int(input("\n\t1. Get Electronic Configuration by Atomic No.\
        \n\n\t2. Get Electronic Configuration by Atomic Symbol.\n\n(1/2)>>>  "))
        if get_input == 1:
            get_input_at_no()
            if 0 >= AtNo > 118:
                print("Invalid Atomic No")

            else:

                elecon()
                symbol()
                ele_con()
                print('\nAtomic No =', atno)
                sleep(5)
        elif get_input == 2:
            get_input_at_sym()
            elecon()
            symbol()
            ele_con()
            print('\nAtomic No =', atno)
            sleep(5)
        else:
            sleep(5)
            print("\n Enter 1 or 2 not else..")

            give_the_final_result()


    def try_again_or_not(name):
        inn = input(f'\n Try {name} \n (y/n)>>>  ')
        if inn.lower() == 'y':
            give_the_final_result()
        elif inn.lower() == 'n':
            sleep(1)
        else:
            try_again_or_not('another')


    give_the_final_result()
    try_again_or_not('another')
except:
    print("Invalid Input")
    try_again_or_not('again')
    sleep(5)

 

Output:

--- :Chemistry with Python = :) :--

1. Get Electronic Configuration by Atomic No.

2. Get Electronic Configuration by Atomic Symbol.

(1/2)>>> 1

Enter Atomic number of element..
==================================

>>> 13

Electronic Configurations: 2 , 8 , 3

Atomic Symbol : Al

Electronic Configurations : --
1s 2 , 2s 2 , 2p 6 , 3s 2 , 3p 1

Atomic No = 13

Try another 
(y/n)>>> y

1. Get Electronic Configuration by Atomic No.

2. Get Electronic Configuration by Atomic Symbol.

(1/2)>>> 2

Enter Atomic symbol of element..
=================================

>>> FE

Electronic Configurations : Unavailable

Atomic Symbol : Fe

Electronic Configurations : --
1s 2 , 2s 2 , 2p 6 , 3s 2 , 3p 6 , 4s 2 , 3d 6

Atomic No = 26
>>>


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *