DEV Community

Cover image for Periodic Table
Scott Gordon
Scott Gordon

Posted on

Periodic Table

Console Output

# periodic_table.py
#   This program display a periodic chart and allows you to pick an element
#   for a full description of the element chosen.
# by: Scott Gordon

import csv, sys, re

# Read data from periodictable.csv
elements_file = open("periodictable.csv", encoding="utf-8")
elements_csv_reader = csv.reader(elements_file)
elements = list(elements_csv_reader)
elements_file.close()

COLUMNS = [
    "Atomic Number",
    "Symbol",
    "Element",
    "Origin of name",
    "Group",
    "Period",
    "Atomic weight",
    "Density",
    "Melting point",
    "Boiling point",
    "Specific heat capacity",
    "Electronegativity",
    "Abundance in earth's crust",
]

# Find longest string in each column in order to justify text
LONGEST_COLUMN = 0
for key in COLUMNS:
    if len(key) > LONGEST_COLUMN:
        LONGEST_COLUMN = len(key)

# Add elements into dictionary
ELEMENTS = {}
for line in elements:
    element = {
        "Atomic Number": line[0],
        "Symbol": line[1],
        "Element": line[2],
        "Origin of name": line[3],
        "Group": line[4],
        "Period": line[5],
        "Atomic weight": line[6] + " u",
        "Density": line[7] + " g/cm^3",
        "Melting point": line[8] + " K",
        "Boiling point": line[9] + " K",
        "Specific heat capacity": line[10] + " J/(g*K)",
        "Electronegativity": line[11],
        "Abundance in earth's crust": line[12] + " mg/kg",
    }

    # Remove bracketed text from csv that was imported from Wikipedia
    for key, value in element.items():
        # Remove [roman numeral] text
        element[key] = re.sub(r"\[(I|V|X)+\]", "", value)

    ELEMENTS[line[0]] = element  # Map atomic number to element
    ELEMENTS[line[1]] = element  # Map symbol to element

print("Periodic Table of Elements")
print()

while True:
    print(
        """ Periodic Table of Elements
      1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18
    1 H                                                  He
    2 Li Be                               B  C  N  O  F  Ne
    3 Na Mg                               Al Si P  S  Cl Ar
    4 K  Ca Sc Ti V  Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr
    5 Rb Sr Y  Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I  Xe
    6 Cs Ba La Hf Ta W  Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn
    7 Fr Ra Ac Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og

            Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu
            Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr"""
    )
    response = input("> ").title()

    if response == "Quit":
        sys.exit()

    # Display element's data
    if response in ELEMENTS:
        for key in COLUMNS:
            key_justified = key.rjust(LONGEST_COLUMN)
            print(f"{key_justified}: {ELEMENTS[response][key]}")
        input("Press Enter to continue...")

Enter fullscreen mode Exit fullscreen mode

This is a copy of the .csv file used in this program.

1,H,Hydrogen,"Greek elements hydro- and -gen, meaning 'water-forming'",1,1,1.008[III][IV][V][VI],0.00008988,14.01,20.28,14.304,2.2,1400
2,He,Helium,"Greek hḗlios, 'sun'",18,1,4.002602(2)[III][V],0.0001785,—[VII],4.22,5.193,–,0.008
3,Li,Lithium,"Greek líthos, 'stone'",1,2,6.94[III][IV][V][VIII][VI],0.534,453.69,1560,3.582,0.98,20
4,Be,Beryllium,"beryl, a mineral (ultimately from the name of Belur in southern India)",2,2,9.0121831(5),1.85,1560,2742,1.825,1.57,2.8
5,B,Boron,"borax, a mineral (from Arabic bawraq)",13,2,10.81[III][IV][V][VI],2.34,2349,4200,1.026,2.04,10
6,C,Carbon,"Latin carbo, 'coal'",14,2,12.011[III][V][VI],2.267,3800,4300,0.709,2.55,200
7,N,Nitrogen,"Greek nítron and -gen, meaning 'niter-forming'",15,2,14.007[III][V][VI],0.0012506,63.15,77.36,1.04,3.04,19
8,O,Oxygen,"Greek oxy- and -gen, meaning 'acid-forming'",16,2,15.999[III][V][VI],0.001429,54.36,90.2,0.918,3.44,461000
9,F,Fluorine,"Latin fluere, 'to flow'",17,2,18.998403163(6),0.001696,53.53,85.03,0.824,3.98,585
10,Ne,Neon,"Greek néon, 'new'",18,2,20.1797(6)[III][IV],0.0008999,24.56,27.07,1.03,–,0.005
11,Na,Sodium,"English soda (the symbol Na is derived from New Latin natrium, coined from German Natron, 'natron')",1,3,22.98976928(2),0.971,370.87,1156,1.228,0.93,23600
12,Mg,Magnesium,"Magnesia, a district of Eastern Thessaly in Greece",2,3,24.305[VI],1.738,923,1363,1.023,1.31,23300
13,Al,Aluminium,"alumina, from Latin alumen (gen. aluminis), 'bitter salt, alum'",13,3,26.9815384(3),2.698,933.47,2792,0.897,1.61,82300
14,Si,Silicon,"Latin silex, 'flint' (originally silicium)",14,3,28.085[V][VI],2.3296,1687,3538,0.705,1.9,282000
15,P,Phosphorus,"Greek phōsphóros, 'light-bearing'",15,3,30.973761998(5),1.82,317.3,550,0.769,2.19,1050
16,S,Sulfur,"Latin sulphur, 'brimstone'",16,3,32.06[III][V][VI],2.067,388.36,717.87,0.71,2.58,350
17,Cl,Chlorine,"Greek chlōrós, 'greenish yellow'",17,3,35.45[III][IV][V][VI],0.003214,171.6,239.11,0.479,3.16,145
18,Ar,Argon,"Greek argós, 'idle' (because of its inertness)",18,3,39.948[III][V][VI],0.0017837,83.8,87.3,0.52,–,3.5
19,K,Potassium,"New Latin potassa, 'potash' (the symbol K is derived from Latin kalium)",1,4,39.0983(1),0.862,336.53,1032,0.757,0.82,20900
20,Ca,Calcium,"Latin calx, 'lime'",2,4,40.078(4)[III],1.54,1115,1757,0.647,1,41500
21,Sc,Scandium,"Latin Scandia, 'Scandinavia'",3,4,44.955908(5),2.989,1814,3109,0.568,1.36,22
22,Ti,Titanium,"Titans, the sons of the Earth goddess of Greek mythology",4,4,47.867(1),4.54,1941,3560,0.523,1.54,5650
23,V,Vanadium,"Vanadis, an Old Norse name for the Scandinavian goddess Freyja",5,4,50.9415(1),6.11,2183,3680,0.489,1.63,120
24,Cr,Chromium,"Greek chróma, 'colour'",6,4,51.9961(6),7.15,2180,2944,0.449,1.66,102
25,Mn,Manganese,corrupted from magnesia negra; see Magnesium,7,4,54.938043(2),7.44,1519,2334,0.479,1.55,950
26,Fe,Iron,English word (the symbol Fe is derived from Latin ferrum),8,4,55.845(2),7.874,1811,3134,0.449,1.83,56300
27,Co,Cobalt,"German Kobold, 'goblin'",9,4,58.933194(3),8.86,1768,3200,0.421,1.88,25
28,Ni,Nickel,"Nickel, a mischievous sprite of German miner mythology",10,4,58.6934(4),8.912,1728,3186,0.444,1.91,84
29,Cu,Copper,"English word, from Latin cuprum, from Ancient Greek Kýpros 'Cyprus'",11,4,63.546(3)[V],8.96,1357.77,2835,0.385,1.9,60
30,Zn,Zinc,"Most likely from German Zinke, 'prong' or 'tooth', though some suggest Persian sang, 'stone'",12,4,65.38(2),7.134,692.88,1180,0.388,1.65,70
31,Ga,Gallium,"Latin Gallia, 'France'",13,4,69.723(1),5.907,302.9146,2673,0.371,1.81,19
32,Ge,Germanium,"Latin Germania, 'Germany'",14,4,72.630(8),5.323,1211.4,3106,0.32,2.01,1.5
33,As,Arsenic,"French arsenic, from Greek arsenikón 'yellow arsenic' (influenced by arsenikós, 'masculine' or 'virile'), from a West Asian wanderword ultimately from Old Iranian *zarniya-ka, 'golden'",15,4,74.921595(6),5.776,1090 [IX],887,0.329,2.18,1.8
34,Se,Selenium,"Greek selḗnē, 'moon'",16,4,78.971(8)[V],4.809,453,958,0.321,2.55,0.05
35,Br,Bromine,"Greek brômos, 'stench'",17,4,79.904[VI],3.122,265.8,332,0.474,2.96,2.4
36,Kr,Krypton,"Greek kryptós, 'hidden'",18,4,83.798(2)[III][IV],0.003733,115.79,119.93,0.248,3,1×10−4
37,Rb,Rubidium,"Latin rubidus, 'deep red'",1,5,85.4678(3)[III],1.532,312.46,961,0.363,0.82,90
38,Sr,Strontium,"Strontian, a village in Scotland",2,5,87.62(1)[III][V],2.64,1050,1655,0.301,0.95,370
39,Y,Yttrium,"Ytterby, a village in Sweden",3,5,88.90584(1),4.469,1799,3609,0.298,1.22,33
40,Zr,Zirconium,"zircon, a mineral",4,5,91.224(2)[III],6.506,2128,4682,0.278,1.33,165
41,Nb,Niobium,"Niobe, daughter of king Tantalus from Greek mythology",5,5,92.90637(1),8.57,2750,5017,0.265,1.6,20
42,Mo,Molybdenum,"Greek molýbdaina, 'piece of lead', from mólybdos, 'lead'",6,5,95.95(1)[III],10.22,2896,4912,0.251,2.16,1.2
43,Tc,Technetium,"Greek tekhnētós, 'artificial'",7,5,[98][X],11.5,2430,4538,–,1.9,~ 3×10−9[XI]
44,Ru,Ruthenium,"New Latin Ruthenia, 'Russia'",8,5,101.07(2)[III],12.37,2607,4423,0.238,2.2,0.001
45,Rh,Rhodium,"Greek rhodóeis, 'rose-coloured', from rhódon, 'rose'",9,5,102.90549(2),12.41,2237,3968,0.243,2.28,0.001
46,Pd,Palladium,"the asteroid Pallas, considered a planet at the time",10,5,106.42(1)[III],12.02,1828.05,3236,0.244,2.2,0.015
47,Ag,Silver,English word (The symbol derives from Latin argentum),11,5,107.8682(2)[III],10.501,1234.93,2435,0.235,1.93,0.075
48,Cd,Cadmium,"New Latin cadmia, from King Kadmos",12,5,112.414(4)[III],8.69,594.22,1040,0.232,1.69,0.159
49,In,Indium,"Latin indicum, 'indigo' (colour found in its spectrum)",13,5,114.818(1),7.31,429.75,2345,0.233,1.78,0.25
50,Sn,Tin,English word (The symbol derives from Latin stannum),14,5,118.710(7)[III],7.287,505.08,2875,0.228,1.96,2.3
51,Sb,Antimony,"Latin antimonium, the origin of which is uncertain: folk etymologies suggest it is derived from Greek antí ('against') + mónos ('alone'), or Old French anti-moine, 'Monk's bane', but it could plausibly be from or related to Arabic ʾiṯmid, 'antimony', reformatted as a Latin word. (The symbol derives from Latin stibium 'stibnite'.)",15,5,121.760(1)[III],6.685,903.78,1860,0.207,2.05,0.2
52,Te,Tellurium,"Latin tellus, 'the ground, earth'",16,5,127.60(3)[III],6.232,722.66,1261,0.202,2.1,0.001
53,I,Iodine,"French iode, from Greek ioeidḗs, 'violet')",17,5,126.90447(3),4.93,386.85,457.4,0.214,2.66,0.45
54,Xe,Xenon,"Greek xénon, neuter form of xénos 'strange'",18,5,131.293(6)[III][IV],0.005887,161.4,165.03,0.158,2.6,3×10−5
55,Cs,Caesium,"Latin caesius, 'sky-blue'",1,6,132.90545196(6),1.873,301.59,944,0.242,0.79,3
56,Ba,Barium,"Greek barýs, 'heavy'",2,6,137.327(7),3.594,1000,2170,0.204,0.89,425
57,La,Lanthanum,"Greek lanthánein, 'to lie hidden'",3,6,138.90547(7)[III],6.145,1193,3737,0.195,1.1,39
58,Ce,Cerium,"the dwarf planet Ceres, considered a planet at the time",,6,140.116(1)[III],6.77,1068,3716,0.192,1.12,66.5
59,Pr,Praseodymium,"Greek prásios dídymos, 'green twin'",,6,140.90766(1),6.773,1208,3793,0.193,1.13,9.2
60,Nd,Neodymium,"Greek néos dídymos, 'new twin'",,6,144.242(3)[III],7.007,1297,3347,0.19,1.14,41.5
61,Pm,Promethium,Prometheus of Greek mythology,,6,[145][X],7.26,1315,3273,–,1.13,2×10−19[XI]
62,Sm,Samarium,"samarskite, a mineral named after Colonel Vasili Samarsky-Bykhovets, Russian mine official",,6,150.36(2)[III],7.52,1345,2067,0.197,1.17,7.05
63,Eu,Europium,Europe,,6,151.964(1)[III],5.243,1099,1802,0.182,1.2,2
64,Gd,Gadolinium,"gadolinite, a mineral named after Johan Gadolin, Finnish chemist, physicist and mineralogist",,6,157.25(3)[III],7.895,1585,3546,0.236,1.2,6.2
65,Tb,Terbium,"Ytterby, a village in Sweden",,6,158.925354(8),8.229,1629,3503,0.182,1.2,1.2
66,Dy,Dysprosium,"Greek dysprósitos, 'hard to get'",,6,162.500(1)[III],8.55,1680,2840,0.17,1.22,5.2
67,Ho,Holmium,"New Latin Holmia, 'Stockholm'",,6,164.930328(7),8.795,1734,2993,0.165,1.23,1.3
68,Er,Erbium,"Ytterby, a village in Sweden",,6,167.259(3)[III],9.066,1802,3141,0.168,1.24,3.5
69,Tm,Thulium,"Thule, the ancient name for an unclear northern location",,6,168.934218(6),9.321,1818,2223,0.16,1.25,0.52
70,Yb,Ytterbium,"Ytterby, a village in Sweden",,6,173.045(10)[III],6.965,1097,1469,0.155,1.1,3.2
71,Lu,Lutetium,"Latin Lutetia, 'Paris'",,6,174.9668(1)[III],9.84,1925,3675,0.154,1.27,0.8
72,Hf,Hafnium,"New Latin Hafnia, 'Copenhagen' (from Danish havn)",4,6,178.49(2),13.31,2506,4876,0.144,1.3,3
73,Ta,Tantalum,"King Tantalus, father of Niobe from Greek mythology",5,6,180.94788(2),16.654,3290,5731,0.14,1.5,2
74,W,Tungsten,"Swedish tung sten, 'heavy stone' (The symbol is from wolfram, the old name of the tungsten mineral wolframite)",6,6,183.84(1),19.25,3695,5828,0.132,2.36,1.3
75,Re,Rhenium,"Latin Rhenus, 'the Rhine'",7,6,186.207(1),21.02,3459,5869,0.137,1.9,7×10−4
76,Os,Osmium,"Greek osmḗ, 'smell'",8,6,190.23(3)[III],22.61,3306,5285,0.13,2.2,0.002
77,Ir,Iridium,"Iris, the Greek goddess of the rainbow",9,6,192.217(2),22.56,2719,4701,0.131,2.2,0.001
78,Pt,Platinum,"Spanish platina, 'little silver', from plata 'silver'",10,6,195.084(9),21.46,2041.4,4098,0.133,2.28,0.005
79,Au,Gold,English word (The symbol derives from Latin aurum),11,6,196.966570(4),19.282,1337.33,3129,0.129,2.54,0.004
80,Hg,Mercury,"Mercury, Roman god of commerce, communication, and luck, known for his speed and mobility (The symbol is from the element's Latin name hydrargyrum, derived from Greek hydrárgyros, 'water-silver')",12,6,200.592(3),13.5336,234.43,629.88,0.14,2,0.085
81,Tl,Thallium,"Greek thallós, 'green shoot or twig'",13,6,204.38[VI],11.85,577,1746,0.129,1.62,0.85
82,Pb,Lead,English word (The symbol derives from Latin plumbum),14,6,207.2(1)[III][V],11.342,600.61,2022,0.129,1.87,14
83,Bi,Bismuth,"German Wismut, from weiß Masse 'white mass', unless from Arabic",15,6,208.98040(1)[X],9.807,544.7,1837,0.122,2.02,0.009
84,Po,Polonium,"Latin Polonia, 'Poland' (the home country of Marie Curie)",16,6,[209][X],9.32,527,1235,–,2,2×10−10[XI]
85,At,Astatine,"Greek ástatos, 'unstable'",17,6,[210][X],7,575,610,–,2.2,3×10−20[XI]
86,Rn,Radon,radium,18,6,[222][X],0.00973,202,211.3,0.094,2.2,4×10−13[XI]
87,Fr,Francium,France,1,7,[223][X],1.87,300,950,–,0.7,~ 1×10−18[XI]
88,Ra,Radium,"French radium, from Latin radius, 'ray'",2,7,[226][X],5.5,973,2010,0.094,0.9,9×10−7[XI]
89,Ac,Actinium,"Greek aktís, 'ray'",3,7,[227][X],10.07,1323,3471,0.12,1.1,5.5×10−10[XI]
90,Th,Thorium,"Thor, the Scandinavian god of thunder",,7,232.0377(4)[X][III],11.72,2115,5061,0.113,1.3,9.6
91,Pa,Protactinium,"proto- (from Greek prôtos, 'first, before') + actinium, which is produced through the radioactive decay of protactinium",,7,231.03588(1)[X],15.37,1841,4300,–,1.5,1.4×10−6[XI]
92,U,Uranium,"Uranus, the seventh planet in the Solar System",,7,238.02891(3)[X],18.95,1405.3,4404,0.116,1.38,2.7
93,Np,Neptunium,"Neptune, the eighth planet in the Solar System",,7,[237][X],20.45,917,4273,–,1.36,≤ 3×10−12[XI]
94,Pu,Plutonium,"the dwarf planet Pluto, considered the ninth planet in the Solar System at the time",,7,[244][X],19.84,912.5,3501,–,1.28,≤ 3×10−11[XI]
95,Am,Americium,"The Americas, as the element was first synthesised on the continent, by analogy with europium",,7,[243][X],13.69,1449,2880,–,1.13,0[XII]
96,Cm,Curium,"Pierre Curie and Marie Curie, French physicists and chemists",,7,[247][X],13.51,1613,3383,–,1.28,0[XII]
97,Bk,Berkelium,"Berkeley, California, where the element was first synthesised, by analogy with terbium",,7,[247][X],14.79,1259,2900,–,1.3,0[XII]
98,Cf,Californium,"California, where the element was first synthesised",,7,[251][X],15.1,1173,(1743)[XIII],–,1.3,0[XII]
99,Es,Einsteinium,"Albert Einstein, German physicist",,7,[252][X],8.84,1133,(1269)[XIII],–,1.3,0[XII]
100,Fm,Fermium,"Enrico Fermi, Italian physicist",,7,[257][X],(9.7)[XIII],(1125)[XIII],–,–,1.3,0[XII]
101,Md,Mendelevium,"Dmitri Mendeleev, Russian chemist and inventor who proposed the periodic table",,7,[258][X],(10.3)[XIII],(1100)[XIII],–,–,1.3,0[XII]
102,No,Nobelium,"Alfred Nobel, Swedish chemist and engineer",,7,[259][X],(9.9)[XIII],(1100)[XIII],–,–,1.3,0[XII]
103,Lr,Lawrencium,"Ernest O. Lawrence, American physicist",,7,[266][X],(15.6)[XIII],(1900)[XIII],–,–,1.3,0[XII]
104,Rf,Rutherfordium,"Ernest Rutherford, chemist and physicist from New Zealand",4,7,[267][X],(23.2)[XIII],(2400)[XIII],(5800)[XIII],–,–,0[XII]
105,Db,Dubnium,"Dubna, Russia, where the Joint Institute for Nuclear Research is located",5,7,[268][X],(29.3)[XIII],–,–,–,–,0[XII]
106,Sg,Seaborgium,"Glenn T. Seaborg, American chemist",6,7,[269][X],(35.0)[XIII],–,–,–,–,0[XII]
107,Bh,Bohrium,"Niels Bohr, Danish physicist",7,7,[270][X],(37.1)[XIII],–,–,–,–,0[XII]
108,Hs,Hassium,"New Latin Hassia, 'Hesse' (a state in Germany)",8,7,[270][X],(40.7)[XIII],–,–,–,–,0[XII]
109,Mt,Meitnerium,"Lise Meitner, Austrian physicist",9,7,[278][X],(37.4)[XIII],–,–,–,–,0[XII]
110,Ds,Darmstadtium,"Darmstadt, Germany, where the element was first synthesised",10,7,[281][X],(34.8)[XIII],–,–,–,–,0[XII]
111,Rg,Roentgenium,"Wilhelm Conrad Röntgen, German physicist",11,7,[282][X],(28.7)[XIII],–,–,–,–,0[XII]
112,Cn,Copernicium,"Nicolaus Copernicus, Polish astronomer",12,7,[285][X],(23.7)[XIII],–,~357[XIV],–,–,0[XII]
113,Nh,Nihonium,"Japanese Nihon, 'Japan' (where the element was first synthesised)",13,7,[286][X],(16)[XIII],(700)[XIII],(1400)[XIII],–,–,0[XII]
114,Fl,Flerovium,"Flerov Laboratory of Nuclear Reactions, part of JINR, where the element was synthesised; itself named after Georgy Flyorov, Russian physicist",14,7,[289][X],(14)[XIII],–,~210,–,–,0[XII]
115,Mc,Moscovium,"Moscow Oblast, Russia, where the element was first synthesised",15,7,[290][X],(13.5)[XIII],(700)[XIII],(1400)[XIII],–,–,0[XII]
116,Lv,Livermorium,"Lawrence Livermore National Laboratory in Livermore, California, which collaborated with JINR on its synthesis",16,7,[293][X],(12.9)[XIII],(709)[XIII],(1085)[XIII],–,–,0[XII]
117,Ts,Tennessine,"Tennessee, United States",17,7,[294][X],(7.2)[XIII],(723)[XIII],(883)[XIII],–,–,0[XII]
118,Og,Oganesson,"Yuri Oganessian, Russian physicist",18,7,[294][X],(5.0)[XIII][XV],–,(350)[XIII],–,–,0[XII]
Enter fullscreen mode Exit fullscreen mode

Photo by Vedrana Filipović on Unsplash

Latest comments (2)

Collapse
 
otumianempire profile image
Michael Otu

only few may understand even though they know
only few may appreciate even though they understand

Collapse
 
diosamuel profile image
diosamuel

cool!