[Edu-sig] Exhibit: Session 2 of 10, a Python training

kirby urner kirby.urner at gmail.com
Fri Jun 23 04:12:46 EDT 2017


# -*- coding: utf-8 -*-
"""
Created on Thurs June 22, 2017

Course: PYT-PR (Saisoft.net)

Instructor:
Kirby Urner
kirby.urner at gmail.com

Audio Check (6:15 PM PDT)

Introduction
   What's an Object?

Types we've seen briefly:
int float string datetime list...
... lets keep adding to that
python_types.py

Mapping versus Sequence:
   dict & set

Looping with for
   more string formatting

Lab 1:  In file elements.py, create
named tuples for the first 10 elements
of Periodic Table.  Need at least:
protons, abbreviation, long name.
Print all elements in a for loop.

New types:
range
enumerate
collections.namedtuple
collections.Counter

Lab 2: create a dict with abbreviations
as keys, for your elements, in elements.py

Worth watching (some other time):
https://youtu.be/lyDLAutA88s
David Beazley | Keynote: Built in Super Heroes

Objects:
  What is an API?
  Internal state
  Dot Notation (going within)

Review / new:  sys module and sys.path
os module.  elements.py is a module too

Immutable versus Mutable:
  tuple versus list, what's the difference?

Callable verus not Callable
  does it need "a mouth" (with or without arguments?)

Lab3: create more formatted printing
for elements.py (elements_lab3.py)

Summary of Session 02
"""

>From the shared Google drive lesson folder:

# -*- coding: utf-8 -*-
"""
Created on Thu Jul  7 19:36:12 2016

@author: kurner

LAB:

Create a namedtuple to represent an element.
Use the Periodic Table to define 10 namedtuples
corresponding to the first 10 elements. Such
as Hydrogen, Helium and so on.

Need at least:
protons, abbreviation (1 or 2 letters), long name

Example:
Employee = namedtuple("Job Holder", "name age title")

Hint:
"{:10} | {:10} | {:10}".format(4.5, 5.8, 9.0)
"""

from collections import namedtuple

Element = namedtuple("Atom", "protons abbrev long_name mass")

# FIRST 10 ELEMENTS FROM PERIODIC TABLE
all_elements = dict()
all_elements["H"]  = Element(1, "H", "Hydrogen", 1.00794)
all_elements["He"] = Element(2, "He", "Helium", 4.002602)
all_elements["Li"] = Element(3, "Li", "Lithium", 6.941)
all_elements["Be"] = Element(4, "Be", "Beryllium", 9.012182)
all_elements["B"]  = Element(5, "B", "Boron", 10.811)
all_elements["C"]  = Element(6, "C", "Carbon", 12.0107)
all_elements["N"]  = Element(7, "N", "Nitrogen", 14.0067)
all_elements["O"]  = Element(8, "O", "Oxygen", 15.9994)
all_elements["F"]  = Element(9, "F", "Fluorine", 18.998403)
all_elements["Ne"] = Element(10, "Ne", "Neon", 20.1797)
all_elements["Na"] = Element(11, "Na", "Sodium", 22.98976928)
all_elements["Mg"] = Element(12, "Mg", "Magnesium", 24.4050)
all_elements["Al"] = Element(13, "Al", "Aluminum", 26.8815386)
all_elements["Si"] = Element(14, "Si", "Silicon", 28.0855)
all_elements["P"]  = Element(15, "P", "Phosphorous", 30.973762)
all_elements["S"]  = Element(16, "S", "Sulfur", 32.065)
all_elements["Cl"] = Element(17, "Cl", "Chlorine", 35.453)
all_elements["Ar"] = Element(18, "Ar", "Argon", 39.948)
all_elements["K"]  = Element(19, "K", "Potassium", 39.0983)
all_elements["Ca"] = Element(20, "Ca", "Calcium", 40.078)
all_elements["Sc"] = Element(21, "Sc", "Scandium", 44.955912)

ordered_elements = \
["H", "He", "Li", "Be",
"B", "C", "N",
"O", "F", "Ne", "Na",
"Mg", "Al", "Si", "P",
"S", "Cl", "Ar", "K", "Ca", "Sc"]

print("          PERIODIC TABLE")
print()
print(" Protons       Abbrev          Mass")
print("-" * 50)
for atom in ordered_elements:
    the_atom = all_elements[atom]
    print("{0:>10} | {1:^10} | {2:12.8} |".format(the_atom.protons,
                                  the_atom.abbrev, the_atom.mass))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20170623/499e039a/attachment.html>


More information about the Edu-sig mailing list