Periodic Table of the Elements
The periodic table of the elements organizes all known chemical elements into a single table. This program presents this table and lets the player access additional information about each element, such as its atomic number, symbol, melting point, and so on. I compiled this information from Wikipedia and stored it in a file called periodictable.csv that you can download from https://inventwithpython.com/periodictable.csv.
periodic_table_of_the_elements.py
1"""Periodic Table of Elements, by Al Sweigart al@inventwithpython.com
2Displays atomic information for all the elements.
3This code is available at https://nostarch.com/big-book-small-python-programming
4Tags: short, science"""
5
6# Data from https://en.wikipedia.org/wiki/List_of_chemical_elements
7# Highlight the table, copy it, then paste it into a spreadsheet program
8# like Excel or Google Sheets like in https://invpy.com/elements
9# Then save this file as periodictable.csv.
10# Or download this csv file from https://invpy.com/periodictable.csv
11
12import csv, sys, re
13
14# Read in all the data from periodictable.csv.
15elementsFile = open('periodictable.csv', encoding='utf-8')
16elementsCsvReader = csv.reader(elementsFile)
17elements = list(elementsCsvReader)
18elementsFile.close()
19
20ALL_COLUMNS = ['Atomic Number', 'Symbol', 'Element', 'Origin of name',
21 'Group', 'Period', 'Atomic weight', 'Density',
22 'Melting point', 'Boiling point',
23 'Specific heat capacity', 'Electronegativity',
24 'Abundance in earth\'s crust']
25
26# To justify the text, we need to find the longest string in ALL_COLUMNS.
27LONGEST_COLUMN = 0
28for key in ALL_COLUMNS:
29 if len(key) > LONGEST_COLUMN:
30 LONGEST_COLUMN = len(key)
31
32# Put all the elements data into a data structure:
33ELEMENTS = {} # The data structure that stores all the element data.
34for line in elements:
35 element = {'Atomic Number': line[0],
36 'Symbol': line[1],
37 'Element': line[2],
38 'Origin of name': line[3],
39 'Group': line[4],
40 'Period': line[5],
41 'Atomic weight': line[6] + ' u', # atomic mass unit
42 'Density': line[7] + ' g/cm^3', # grams/cubic cm
43 'Melting point': line[8] + ' K', # kelvin
44 'Boiling point': line[9] + ' K', # kelvin
45 'Specific heat capacity': line[10] + ' J/(g*K)',
46 'Electronegativity': line[11],
47 'Abundance in earth\'s crust': line[12] + ' mg/kg'}
48
49 # Some of the data has bracketed text from Wikipedia that we want to
50 # remove, such as the atomic weight of Boron:
51 # "10.81[III][IV][V][VI]" should be "10.81"
52
53 for key, value in element.items():
54 # Remove the [roman numeral] text:
55 element[key] = re.sub(r'\[(I|V|X)+\]', '', value)
56
57 ELEMENTS[line[0]] = element # Map the atomic number to the element.
58 ELEMENTS[line[1]] = element # Map the symbol to the element.
59
60print('Periodic Table of Elements')
61print('By Al Sweigart al@inventwithpython.com')
62print()
63
64while True: # Main program loop.
65 # Show table and let the user select an element:
66 print(''' Periodic Table of Elements
67 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
68 1 H He
69 2 Li Be B C N O F Ne
70 3 Na Mg Al Si P S Cl Ar
71 4 K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr
72 5 Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe
73 6 Cs Ba La Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn
74 7 Fr Ra Ac Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og
75
76 Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu
77 Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr''')
78 print('Enter a symbol or atomic number to examine, or QUIT to quit.')
79 response = input('> ').title()
80
81 if response == 'Quit':
82 sys.exit()
83
84 # Display the selected element's data:
85 if response in ELEMENTS:
86 for key in ALL_COLUMNS:
87 keyJustified = key.rjust(LONGEST_COLUMN)
88 print(keyJustified + ': ' + ELEMENTS[response][key])
89 input('Press Enter to continue...')