Open In App

Get the details of an element by Atomic Number using Python

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to get the details of the elements by inputting the atomic number using the Python module. Chemistry’s foundational tool, the periodic table of elements provides a structured and ordered perspective of the constituent parts of matter. Each of the elements in this table has distinct qualities and traits that have captivated scientists for ages.

Get the details of an element by Atomic Number

Requirements:

We’ll explore the “periodictable” module in this tutorial by delving into the chemical and programming worlds of Python. This module offers a simple way to obtain important information on chemical elements, including atomic numbers and symbols, atomic masses, and more, even though it is not a part of the standard Python library.

Periodictable:

The ‘periodictable’ module is a Python package designed to make it easier to retrieve important chemical element data. It provides a method that is both structured and simple to use for retrieving data on all known chemical elements, including their atomic weights, densities, names, symbols, and numbers. This package also offers a periodic table of the elements together with support for mass and x-ray/neutron scattering data.

To install the package run the below command in the terminal window:

pip install periodictable

Code:

Python3




# Import the 'periodictable' module, which provides information about chemical elements.
import periodictable
 
# Use a try-except block to handle potential errors during user input.
try:
    # Prompt the user to enter the Atomic Number of the Element and convert it to an integer.
    Atomic_no = int(input("Enter the Atomic Number of the Element: "))
     
    # Retrieve information about the element with the given Atomic Number.
    element = periodictable.elements[Atomic_no]
     
    # Print various properties of the element.
    print("Atomic Number:", element.number)
    print("Symbol:", element.symbol)
    print("Name:", element.name)
    print("Atomic Mass:", element.mass)
    print("Density:", element.density)
     
# Handle the ValueError exception if the user doesn't enter a valid integer.
except ValueError:
    print("Please enter a valid atomic number as an integer.")
     
# Handle the KeyError exception if the entered atomic number doesn't exist in the periodic table.
except KeyError:
    print("The entered atomic number does not exist in the periodic table.")
 
#This code is Contributed by PL VISHNUPPRIYAN


Working:

1) Imports the ‘periodictable’ module, which provides data about chemical elements.

2) Uses a try-except block to handles the errors during user input and prompts the user to input the atomic number of an element.

3) Converts the user’s input into an integer, representing the atomic number and retrieves information about the element with the specified atomic number using the module’s dictionary.

4) Prints various properties of the element, including its atomic number, symbol, name, atomic mass, and density.

5) Handles the errors, if the user doesn’t enter a valid integer, it prints a message asking for a valid atomic number or if the entered atomic number doesn’t correspond to any known element, it prints a message indicating that the element doesn’t exist in the periodic table.

Output:

Example 1: Atomic Number-11 (Sodium)

Periodicvalue5

Output: Sodium

Example 2: Atomic Numebr -17(Chlorine)

Periodicvalue3

Output: Chlorine

Example 3: Atomic Number -13(Aluminium)

Periodicvalue2

Output: Aluminium



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads