Interactive Periodic Table Lookup Tool in Python
Guide to Building a Periodic Table Lookup Tool in Python
Hey there, Python beginners! Want to create a cool program that lets you look up details about chemical elements, like Hydrogen or Gold, just by typing their name, symbol, or atomic number? In this post, weâll walk through a simple Python script that does exactly that. Itâs like having your own digital periodic table! Weâll break it down step by step in a way thatâs easy to understand, even if youâre new to coding. Letâs dive in!
What Does This Program Do?
This Python program lets you:
Enter an elementâs name (e.g., “Hydrogen”), symbol (e.g., “H”), or atomic number (e.g., “1”).
Get back the elementâs full name, symbol, atomic number, and atomic mass.
Keep looking up elements until you type “exit” to quit.
Itâs interactive, beginner-friendly, and a great way to learn about dictionaries, loops, and error handling in Python. Hereâs how it works!
Code
# Full periodic table data
elements_data = {
1: ("H", "Hydrogen", 1.008), 2: ("He", "Helium", 4.0026), 3: ("Li", "Lithium", 6.94),
4: ("Be", "Beryllium", 9.0122), 5: ("B", "Boron", 10.81), 6: ("C", "Carbon", 12.011),
7: ("N", "Nitrogen", 14.007), 8: ("O", "Oxygen", 15.999), 9: ("F", "Fluorine", 18.998),
10: ("Ne", "Neon", 20.180), 11: ("Na", "Sodium", 22.990), 12: ("Mg", "Magnesium", 24.305),
13: ("Al", "Aluminium", 26.982), 14: ("Si", "Silicon", 28.085), 15: ("P", "Phosphorus", 30.974),
16: ("S", "Sulfur", 32.06), 17: ("Cl", "Chlorine", 35.45), 18: ("Ar", "Argon", 39.948),
19: ("K", "Potassium", 39.098), 20: ("Ca", "Calcium", 40.078), 21: ("Sc", "Scandium", 44.956),
22: ("Ti", "Titanium", 47.867), 23: ("V", "Vanadium", 50.942), 24: ("Cr", "Chromium", 51.996),
25: ("Mn", "Manganese", 54.938), 26: ("Fe", "Iron", 55.845), 27: ("Co", "Cobalt", 58.933),
28: ("Ni", "Nickel", 58.693), 29: ("Cu", "Copper", 63.546), 30: ("Zn", "Zinc", 65.38),
31: ("Ga", "Gallium", 69.723), 32: ("Ge", "Germanium", 72.63), 33: ("As", "Arsenic", 74.922),
34: ("Se", "Selenium", 78.971), 35: ("Br", "Bromine", 79.904), 36: ("Kr", "Krypton", 83.798),
37: ("Rb", "Rubidium", 85.468), 38: ("Sr", "Strontium", 87.62), 39: ("Y", "Yttrium", 88.906),
40: ("Zr", "Zirconium", 91.224), 41: ("Nb", "Niobium", 92.906), 42: ("Mo", "Molybdenum", 95.95),
43: ("Tc", "Technetium", 98), 44: ("Ru", "Ruthenium", 101.07), 45: ("Rh", "Rhodium", 102.91),
46: ("Pd", "Palladium", 106.42), 47: ("Ag", "Silver", 107.87), 48: ("Cd", "Cadmium", 112.41),
49: ("In", "Indium", 114.82), 50: ("Sn", "Tin", 118.71), 51: ("Sb", "Antimony", 121.76),
52: ("Te", "Tellurium", 127.60), 53: ("I", "Iodine", 126.90), 54: ("Xe", "Xenon", 131.29),
55: ("Cs", "Cesium", 132.91), 56: ("Ba", "Barium", 137.33), 57: ("La", "Lanthanum", 138.91),
58: ("Ce", "Cerium", 140.12), 59: ("Pr", "Praseodymium", 140.91), 60: ("Nd", "Neodymium", 144.24),
61: ("Pm", "Promethium", 145), 62: ("Sm", "Samarium", 150.36), 63: ("Eu", "Europium", 151.96),
64: ("Gd", "Gadolinium", 157.25), 65: ("Tb", "Terbium", 158.93), 66: ("Dy", "Dysprosium", 162.50),
67: ("Ho", "Holmium", 164.93), 68: ("Er", "Erbium", 167.26), 69: ("Tm", "Thulium", 168.93),
70: ("Yb", "Ytterbium", 173.05), 71: ("Lu", "Lutetium", 174.97), 72: ("Hf", "Hafnium", 178.49),
73: ("Ta", "Tantalum", 180.95), 74: ("W", "Tungsten", 183.84), 75: ("Re", "Rhenium", 186.21),
76: ("Os", "Osmium", 190.23), 77: ("Ir", "Iridium", 192.22), 78: ("Pt", "Platinum", 195.08),
79: ("Au", "Gold", 196.97), 80: ("Hg", "Mercury", 200.59), 81: ("Tl", "Thallium", 204.38),
82: ("Pb", "Lead", 207.2), 83: ("Bi", "Bismuth", 208.98), 84: ("Po", "Polonium", 209),
85: ("At", "Astatine", 210), 86: ("Rn", "Radon", 222), 87: ("Fr", "Francium", 223),
88: ("Ra", "Radium", 226), 89: ("Ac", "Actinium", 227), 90: ("Th", "Thorium", 232.04),
91: ("Pa", "Protactinium", 231.04), 92: ("U", "Uranium", 238.03), 93: ("Np", "Neptunium", 237),
94: ("Pu", "Plutonium", 244), 95: ("Am", "Americium", 243), 96: ("Cm", "Curium", 247),
97: ("Bk", "Berkelium", 247), 98: ("Cf", "Californium", 251), 99: ("Es", "Einsteinium", 252),
100: ("Fm", "Fermium", 257), 101: ("Md", "Mendelevium", 258), 102: ("No", "Nobelium", 259),
103: ("Lr", "Lawrencium", 266), 104: ("Rf", "Rutherfordium", 267), 105: ("Db", "Dubnium", 268),
106: ("Sg", "Seaborgium", 269), 107: ("Bh", "Bohrium", 270), 108: ("Hs", "Hassium", 277),
109: ("Mt", "Meitnerium", 278), 110: ("Ds", "Darmstadtium", 281), 111: ("Rg", "Roentgenium", 282),
112: ("Cn", "Copernicium", 285), 113: ("Nh", "Nihonium", 286), 114: ("Fl", "Flerovium", 289),
115: ("Mc", "Moscovium", 290), 116: ("Lv", "Livermorium", 293), 117: ("Ts", "Tennessine", 294),
118: ("Og", "Oganesson", 294),
}
# Create lookup dictionaries
symbol_lookup = {v[0].lower(): k for k, v in elements_data.items()}
name_lookup = {v[1].lower(): k for k, v in elements_data.items()}
# Main interactive loop
print("PERIODIC TABLE ATOMIC MASS LOOKUP")
print("You can enter the element's name, symbol, or atomic number.\n")
while True:
user_input = input("Enter element name/symbol/atomic number (or 'exit' to quit): ").strip().lower()
if user_input == 'exit':
break
try:
if user_input.isdigit():
atomic_number = int(user_input)
elif user_input in name_lookup:
atomic_number = name_lookup[user_input]
elif user_input in symbol_lookup:
atomic_number = symbol_lookup[user_input]
else:
print("Invalid input. Please try again.\n")
continue
symbol, name, mass = elements_data[atomic_number]
print(f"Element: {name} ({symbol})\nAtomic Number: {atomic_number}\nAtomic Mass: {mass}\n")
except KeyError:
print("Element not found in periodic table. Try again.\n")
How It Works: Step-by-Step Breakdown
Letâs go through the code like weâre building it from scratch. Each step is like a building block, and weâll explain it in a way thatâs super clear for beginners.
Step 1: Storing the Periodic Table
The program starts with a dictionary called elements_data. A dictionary in Python is like a phonebook: it pairs a key (like a personâs name) with a value (their phone number). Here:
The keys are atomic numbers (1, 2, 3, …, up to 118).
The values are tuples (like small, unchangeable lists) with three pieces of info:
The elementâs symbol (e.g., “H” for Hydrogen).
The elementâs name (e.g., “Hydrogen”).
The elementâs atomic mass (e.g., 1.008 for Hydrogen).
Example:
elements_data = {
1: ("H", "Hydrogen", 1.008),
2: ("He", "Helium", 4.0026),
# ... and so on
}
If we ask for elements_data[1], we get (“H”, “Hydrogen”, 1.008).
Think of it like: A big filing cabinet where each drawer (the atomic number) holds a card with the symbol, name, and mass of an element.
Step 2: Making Search Easier with Lookup Dictionaries
To make searching faster, the program creates two more dictionaries:
symbol_lookup: Maps element symbols (like “h” or “au”) to their atomic numbers.
name_lookup: Maps element names (like “hydrogen” or “gold”) to their atomic numbers.
Code:
symbol_lookup = {v[0].lower(): k for k, v in elements_data.items()}
name_lookup = {v[1].lower(): k for k, v in elements_data.items()}
v[0] grabs the symbol from the tuple (e.g., “H” from (“H”, “Hydrogen”, 1.008)).
v[1] grabs the name (e.g., “Hydrogen”).
.lower() makes everything lowercase, so “H” becomes “h” and “Hydrogen” becomes “hydrogen”. This means users can type “H” or “h” and it still works.
k is the atomic number (the key from elements_data).
Example:
symbol_lookup[“h”] gives 1 (Hydrogenâs atomic number).
name_lookup[“hydrogen”] also gives 1.
Think of it like: Creating quick-reference index cards so you can find an elementâs atomic number by its symbol or name without flipping through the whole filing cabinet.
Step 3: Welcoming the User
The program prints a friendly message to tell users what they can do:
print("PERIODIC TABLE ATOMIC MASS LOOKUP")
print("You can enter the element's name, symbol, or atomic number.\n")
This is like a sign saying, âHey, type an elementâs name, symbol, or number, and Iâll tell you about it!â
Step 4: The Main Loop
The program uses a while True loop to keep asking for input until the user wants to stop:
while True:
user_input = input("Enter element name/symbol/atomic number (or 'exit' to quit): ").strip().lower()
input(…) asks the user to type something and stores it in user_input.
.strip() removes extra spaces (e.g., ” H ” becomes “H”).
.lower() makes the input lowercase (e.g., “HYDROGEN” becomes “hydrogen”).
Think of it like: A helpful assistant who keeps asking, âWhat element do you want to know about?â until youâre done.
Step 5: Checking for Exit
The program checks if the user wants to quit:
if user_input == 'exit':
break
If the user types “exit”, the break command stops the loop, and the program ends.
Think of it like: Telling the assistant, âIâm done,â and they close the book.
Step 6: Figuring Out What the User Typed
Now the program checks what the user entered (a number, name, or symbol):
try:
if user_input.isdigit():
atomic_number = int(user_input)
elif user_input in name_lookup:
atomic_number = name_lookup[user_input]
elif user_input in symbol_lookup:
atomic_number = symbol_lookup[user_input]
else:
print("Invalid input. Please try again.\n")
continue
Is it a number? user_input.isdigit() checks if the input is all numbers (like “1” or “79”). If so, it converts it to an integer with int(user_input) to use as the atomic number.
Is it a name? If itâs not a number, it checks if the input is in name_lookup (e.g., “hydrogen”). If it is, it gets the atomic number.
Is it a symbol? If itâs not a name, it checks symbol_lookup (e.g., “au”). If it matches, it gets the atomic number.
Is it invalid? If none of these work (e.g., “xyz”), it prints âInvalid inputâ and uses continue to go back and ask again.
Think of it like: The assistant checking if you gave a page number, a book title, or an authorâs name. If you say âpizza,â they say, âNope, try again!â
Step 7: Showing the Elementâs Info
Once the program has the atomic number, it grabs and displays the elementâs details:
symbol, name, mass = elements_data[atomic_number]
print(f"Element: {name} ({symbol})\nAtomic Number: {atomic_number}\nAtomic Mass: {mass}\n")
elements_data[atomic_number] gets the tuple (e.g., (“H”, “Hydrogen”, 1.008) for atomic number 1).
symbol, name, mass = … splits the tuple into three variables.
The print statement shows the info in a neat format, like:
Output:
Element: Hydrogen (H)
Atomic Number: 1
Atomic Mass: 1.008
Step 8: Handling Mistakes
The try-except block catches errors if the atomic number doesnât exist:
except KeyError:
print("Element not found in periodic table. Try again.\n")
If the user enters a number like 119 (which isnât in the dictionary), Python raises a KeyError. The except block catches this and says, âElement not found.â
Think of it like: The assistant saying, âSorry, thatâs not in our records,â if you ask for something that doesnât exist.
What Happens When You Run It?
Hereâs an example of using the program:
PERIODIC TABLE ATOMIC MASS LOOKUP
You can enter the element’s name, symbol, or atomic number.
Enter element name/symbol/atomic number (or ‘exit’ to quit): Hydrogen
Element: Hydrogen (H)
Atomic Number: 1
Atomic Mass: 1.008
Enter element name/symbol/atomic number (or ‘exit’ to quit): Au
Element: Gold (Au)
Atomic Number: 79
Atomic Mass: 196.97
Enter element name/symbol/atomic number (or ‘exit’ to quit): 8
Element: Oxygen (O)
Atomic Number: 8
Atomic Mass: 15.999
Enter element name/symbol/atomic number (or ‘exit’ to quit): xyz
Invalid input. Please try again.
Enter element name/symbol/atomic number (or ‘exit’ to quit): exit
How It All Comes Together
The program:
- Stores all element data in a dictionary.
- Creates quick lookup tables for symbols and names.
- Keeps asking the user for input.
- Checks if the input is a number, symbol, or name.
- Finds the matching element and shows its details.
- Handles errors if the input is wrong.
- Stops when the user types “exit”.
Why This Code Is Great for Beginners
- Simple Logic: It uses basic Python concepts like dictionaries, loops, and conditionals.
- Real-World Use: Itâs a practical tool for looking up real scientific data.
- Error Handling: It shows how to handle mistakes without crashing.
- Interactive: Itâs fun to use because it responds to user input.

