Thursday, November 2, 2017

Adventures in Python: Using a Dictionary to Print a Table (OT: HP Prime new keyboard colors)

Adventures in Python:  Using a Dictionary to Print a Table
(OT: HP Prime new keyboard colors)

Here is a short program where a dictionary and the format command are used to print a nice looking table.

In Python, a dictionary is defined as list of two-element entries, in the format { x1:y1, x2:y2, x3:y3, … }.  Where x1, y1, x2, y2, and so on are strings or numbers. 

We can also designate the format of data within a print statement.  The general syntax:

print( a string that contains {n:ABC}.format(n0, n1, n2, …))

You can as many print formats as you want.  The format inside of {n:ABC}:
n = the nth argument, starting with 0
A = 0 for padded zeros, < align left, > align right, ^ align center
B = length of a field in the form of L.N  (L = length of the field, N = number of decimal places, note that L is the minimum filed length, not maximum)
C = d or i = integer, f - floating, r or s = string

Example: 
>>> import math

Print π with 10 decimal places:
>>> print('{0:1.10f}'.format(math.pi))
3.1415926536

Print π with 8 decimal places:
>>> print('{0:1.8f}'.format(math.pi))
3.14159265

Print the first five letters of the alphabet from a string of the entire alphabet:
>>> print('{0:10.5s}'.format('abcdefghijklmnopqrstuvwxyz'))
abcde

Sample script:

# Program 011:  Using format in print

# General syntax:

# print(' ... {n:ABC} '.format(x0, x1))
# n = the nth argument, starting with 0
# A = 0 for padded zeros, < align left, > align right, ^ align center
# B = length of a field
# C = d or i = integer, f - floating, r or s = string

# x0, x1 are items

# If a table is used, use a for loop with tablename.items
# Table sytnax =  { item0 : item1, [start a new row]}
# for x0, x1 in table.items():
#  print('...   '.format(x0,x1))
# For this, table can only have 2 times per row?


# table of the famous stars
table = {'Antares':'Scorpius', 'Regulus':'Leo',
         'Aldebaran':'Taurus', 'Sadalmelik':'Aquarius',
         'Siruis':'Canis Major', 'Vega':'Lyra',
         'Polaris':'Ursa Minor', 'Deneb':'Cygnus',
         'Alpha Centauri':'Centaurus', 'Altair':'Aquila',
         'Castor':'Gemini', 'Betelgeuse':'Orion',
         'Fomalhaut':'Piscis Austrinus', 'Spica':'Virgo'}

print('Astronomy\'s Famous Stars')
# need \' for the apostrophe
# for our example let x0 = star, x1 = constellation
for star, constellation in table.items():
    # 0 = star, 1 = constellation
    # <15s = left aligned, 15 spaces, string
    print('Star:  {0:<15s} ==> Constellation: {1:<15s}'
          .format(star,constellation))

Result:

Astronomy's Famous Stars
Star:  Antares         ==> Constellation: Scorpius      
Star:  Regulus         ==> Constellation: Leo           
Star:  Aldebaran       ==> Constellation: Taurus        
Star:  Sadalmelik      ==> Constellation: Aquarius      
Star:  Siruis          ==> Constellation: Canis Major   
Star:  Vega            ==> Constellation: Lyra          
Star:  Polaris         ==> Constellation: Ursa Minor    
Star:  Deneb           ==> Constellation: Cygnus        
Star:  Alpha Centauri  ==> Constellation: Centaurus     
Star:  Altair          ==> Constellation: Aquila        
Star:  Castor          ==> Constellation: Gemini        
Star:  Betelgeuse      ==> Constellation: Orion         
Star:  Fomalhaut       ==> Constellation: Piscis Austrinus
Star:  Spica           ==> Constellation: Virgo

Overtime

I finally got a new HP Prime with the new color contrast (darker blue shift text, slightly darker orange shift text, keys are a lighter color, the number keys are white), and yes, I do like the new design. (model number G8X92AA, hardware version C, 2016 edition)




Eddie


This blog is property of Edward Shore, 2017.

4 comments:

  1. The program and moreover idea is really cool. Can you please upload how does it work in real time? Would like to see it :)
    -seth at merehead

    ReplyDelete


  2. Thanks for providing good information,Thanks for your sharing python Online Course

    ReplyDelete
  3. Do you type this code in the regular program editor (Shift One), or do I have to start a Python editor elsewhere?

    If you have some pages that go into starting out with micropython on HP Prime, I'd love to be pointed to them. I was disappointed, when getting only a bit into it so far on Casio fx-cg50, that their micropython doesn't even support arrays, at least not that I've figured out so far.

    ReplyDelete
    Replies
    1. John,

      On the HP Prime, it's through the regular program editor (Shift One). If I recall correctly, it's a #cas type of program.

      I have not seen any support on arrays or graphics on the fx-CG 50's Micropython. Hopefully next update.

      Eddie

      Delete

Casio fx-CG50: Animation of a Graph

Casio fx-CG50:  Animation of a Graph Introduction You can easily animate a graph with the Dynamic Graphing mode.   While I'm descri...