Exhibit: Session 2 of 10, a Python training
# -*- coding: utf-8 -*- """ Created on Thurs June 22, 2017 Course: PYT-PR (Saisoft.net) Instructor: Kirby Urner kirby.urner@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))
On Fri, Jun 23, 2017 at 3:12 AM, kirby urner <kirby.urner@gmail.com> wrote:
Looping with for more string formatting
Kerby, I am curious about the pedagogy of introducing two distinct concepts at the same time like that. Care to elaborate? I'm trying not to be critical, I am not qualified ;)
On Fri, Jun 23, 2017 at 6:32 PM, Carl Karsten <carl@nextdayvideo.com> wrote:
On Fri, Jun 23, 2017 at 3:12 AM, kirby urner <kirby.urner@gmail.com> wrote:
Looping with for more string formatting
Kerby, I am curious about the pedagogy of introducing two distinct concepts at the same time like that. Care to elaborate?
I introduce string formatting with {placeholders} early in the training, and then keep using that. I also use the keywords list (keyword.kwlist) for raw material, when introducing for loops. So something like this: from keyword import kwlist for word in kwlist: if word.islower(): # showing how conditionals work print("Lowercase keyword: | {:20} |".format(word)) The format method is a good stand-in for callables in general in that we can start looking at positional versus named arguments, * and ** as argument exploders etc. So yeah, my technique is to keep introducing new concepts (e.g. for loop) while continuing to use those looked at so far (e.g. print formatting). Kind of like a juggling act, where one keeps introducing more balls. Kirby Kirby
Um....
So yeah, my technique is to keep introducing new concepts (e.g. for loop) while continuing to use those looked at so far (e.g. print formatting).
That I get.
Looping with for more string formatting
That looks like deliberately introducing two new concepts at the same time (for and *more* string formatting) Are you trying to introduce two more balls? I have always made a point of one at a time, but maybe that is boring and the student tunes out. On Sat, Jun 24, 2017 at 11:04 AM, kirby urner <kirby.urner@gmail.com> wrote:
On Fri, Jun 23, 2017 at 6:32 PM, Carl Karsten <carl@nextdayvideo.com> wrote:
On Fri, Jun 23, 2017 at 3:12 AM, kirby urner <kirby.urner@gmail.com> wrote:
Looping with for more string formatting
Kerby, I am curious about the pedagogy of introducing two distinct concepts at the same time like that. Care to elaborate?
I introduce string formatting with {placeholders} early in the training, and then keep using that. I also use the keywords list (keyword.kwlist) for raw material, when introducing for loops. So something like this:
from keyword import kwlist
for word in kwlist: if word.islower(): # showing how conditionals work print("Lowercase keyword: | {:20} |".format(word))
The format method is a good stand-in for callables in general in that we can start looking at positional versus named arguments, * and ** as argument exploders etc.
So yeah, my technique is to keep introducing new concepts (e.g. for loop) while continuing to use those looked at so far (e.g. print formatting).
Kind of like a juggling act, where one keeps introducing more balls.
Kirby
Kirby
participants (2)
-
Carl Karsten
-
kirby urner