Difference between revisions of "EGR 103/Concept List Fall 2019"

From PrattWiki
Jump to navigation Jump to search
(Lecture 7 - Applications)
(Lecture 7 - Applications)
Line 143: Line 143:
 
         print('LOOOOOOOOOOOOOOOSER')
 
         print('LOOOOOOOOOOOOOOOSER')
 
      
 
      
 +
</source>
 +
</div>
 +
</div>
 +
 +
* NATO Phonetic Translator - [https://en.wikipedia.org/wiki/NATO_phonetic_alphabet NATO phonetic alphabet]
 +
<div class="mw-collapsible mw-collapsed">
 +
<source lang=python>
 +
# nato_trans.py from class:
 +
</source>
 +
<div class="mw-collapsible-content">
 +
<source lang=python>
 +
fread = open('NATO.dat', 'r')
 +
 +
d = {}
 +
 +
for puppies in fread:
 +
    #print(puppies) $ if you want to see the whole line
 +
   
 +
    #key = puppies[0]
 +
    #value = puppies[:-1]
 +
    #d[key] = value
 +
   
 +
    d[puppies[0]] = puppies[:-1]
 +
 +
fread.close()
 +
 +
hamster = input('Word: ').upper()
 +
 +
for kittens in hamster:
 +
    #print(d[letter], end=' ')
 +
    print(d.get(kittens, 'XXX'), end=' ')
 +
   
 +
'''
 +
In class - one question was "in cases where there is not a code, can it
 +
return the original value instead of XXX" -- yes:
 +
    print(d.get(kittens, kittens))
 +
'''
 +
</source>
 +
</div>
 +
</div>
 +
:* Data file we used:
 +
<div class="mw-collapsible mw-collapsed">
 +
<source lang=python>
 +
# NATO.dat from class:
 +
</source>
 +
<div class="mw-collapsible-content">
 +
<source lang=python>
 +
Alfa
 +
Bravo
 +
Charlie
 +
Delta
 +
Echo
 +
Foxtrot
 +
Golf
 +
Hotel
 +
India
 +
Juliett
 +
Kilo
 +
Lima
 +
Mike
 +
November
 +
Oscar
 +
Papa
 +
Quebec
 +
Romeo
 +
Sierra
 +
Tango
 +
Uniform
 +
Victor
 +
Whiskey
 +
X-ray
 +
Yankee
 +
Zulu
 +
 
</source>
 
</source>
 
</div>
 
</div>

Revision as of 02:39, 17 September 2019

This page will be used to keep track of the commands and major concepts for each lecture in EGR 103.

Lecture 1 - Introduction

  • Class web page: EGR 103L; assignments, contact info, readings, etc - see slides on Errata/Notes page
  • Sakai page: Sakai 103L page; grades, surveys and tests, some assignment submissions
  • CampusWire page: CampusWire 103L page; message board for questions - you need to be in the class and have the access code to subscribe.

Lecture 2 - Programs and Programming

Lecture 3 - "Number" Types

  • Python is a "typed" language - variables have types
  • We will use eight types:
    • Focus of the day: int, float, and array
    • Focus a little later: string, list, tuple
    • Focus later: dictionary, set
  • int: integers; Python can store these perfectly
  • float: floating point numbers - "numbers with decimal points" - Python sometimes has problems
  • array
    • Requires numpy, usually with import numpy as np
    • Organizational unit for storing rectangular arrays of numbers
  • Math with "Number" types works the way you expect
    • ** * / // % + -
  • Relational operators can compare "Number" Types and work the way you expect with True or False as an answer
    • < <= == >= > !=
    • With arrays, either same size or one is a single value; result will be an array of True and False the same size as the array
  • Slices allow us to extract information from an array or put information into an array
  • a[0] is the element in a at the start
  • a[3] is the element in a three away from the start
  • a[:] is all the elements in a because what is really happening is:
    • a[start:until] where start is the first index and until is just *past* the last index;
    • a[3:7] will return a[3] through a[6] in 4-element array
    • a[start:until:increment] will skip indices by increment instead of 1
    • To go backwards, a[start:until:-increment] will start at an index and then go backwards until getting at or just past until.
  • For 2-D arrays, you can index items with either separate row and column indices or indices separated by commas:
    • a[2][3] is the same as a[2, 3]
    • Only works for arrays!

Lecture 4 - Other Types and Functions

  • Lists are set off with [ ] and entries can be any valid type (including other lists!); entries can be of different types from other entries
  • List items can be changed
  • Tuples are indicated by commas without square brackets (and are usually shown with parentheses - which are required if trying to make a tuple an entry in a tuple or a list)
  • Dictionaries are collections of key : value pairs set off with { }; keys can be any immutable type (int, float, string, tuple) and must be unique; values can be any type and do not need to be unique
  • To read more:
    • Note! Many of the tutorials below use Python 2 so instead of print(thing) it shows print thing
    • Lists at tutorialspoint
    • Tuples at tutorialspoint
    • Dictionary at tutorialspoint
  • Defined functions can be multiple lines of code and have multiple outputs.
    • Four different types of input parameters:
      • Required (listed first)
      • Named with defaults (second)
      • Additional positional arguments ("*args") (third)
        • Function will create a tuple containing these items in order
      • Additional keyword arguments ("**kwargs") (last)
        • Function will create a dictionary of keyword and value pairs
    • Function ends when indentation stops or when the function hits a return statement
    • Return returns single item as an item of that type; if there are multiple items returned, they are stored in a tuple
    • If there is a left side to the function call, it either needs to be a single variable name or a tuple with as many entries as the number of items returned

Lecture 5 - Format, Logic, Decisions, and Loops

Lecture 6 - String Things and Loops

  • ord to get numerical value of each character
  • chr to get character based on integer
  • map(fun, sequence) to apply a function to each item in a sequence
  • Basics of while loops
  • Basics of for loops
  • List comprehensions
    • [FUNCTION for VAR in SEQUENCE if LOGIC]
      • The FUNCTION should return a single thing (though that thing can be a list, tuple, etc)
      • The "if LOGIC" part is optional
      • [k for k in range(3)] creates [0, 1, 2]
      • [k**2 for k in range (5, 8)] creates [25, 36, 49]
      • [k for k in 'hello' if k<'i'] creates ['h', 'e']
      • [(k,k**2) for k in range(11) if k%3==2] creates [(2, 4), (5, 25), (8, 64)]
    • Wait - that's the simplified version...here:
  • Want to see Amharic?
list(map(chr, range(4608, 4992)))
  • Want to see the Greek alphabet?
for k in range(913,913+25):
    print(chr(k), chr(k+32))

Lecture 7 - Applications

# tpir.py from class:
import numpy as np
import time

def create_price(low=100, high=1500):
    return np.random.randint(low, high+1)
    
def get_guess():
    guess = int(input('Guess: '))
    return guess
    
def check_guess(actual, guess):
    if actual > guess:
        print('Higher!')
    elif actual < guess:
        print('Lower!')

    
if __name__ == '__main__':
    #print(create_price(0, 100))
    the_price = create_price()
    the_guess = get_guess()
    start_time = time.clock()
    #print(the_guess)
    while the_price != the_guess and (time.clock() < start_time+30):
        check_guess(the_price, the_guess)
        the_guess = get_guess()
    
    if the_price==the_guess:    
        print('You win!!!!!!!')
    else:
        print('LOOOOOOOOOOOOOOOSER')
# nato_trans.py from class:
fread = open('NATO.dat', 'r')

d = {}

for puppies in fread:
    #print(puppies) $ if you want to see the whole line
    
    #key = puppies[0]
    #value = puppies[:-1]
    #d[key] = value
    
    d[puppies[0]] = puppies[:-1]

fread.close()

hamster = input('Word: ').upper()

for kittens in hamster:
    #print(d[letter], end=' ')
    print(d.get(kittens, 'XXX'), end=' ')
    
'''
In class - one question was "in cases where there is not a code, can it
return the original value instead of XXX" -- yes:
    print(d.get(kittens, kittens))
'''
  • Data file we used:
# NATO.dat from class:
Alfa
Bravo
Charlie
Delta
Echo
Foxtrot
Golf
Hotel
India
Juliett
Kilo
Lima
Mike
November
Oscar
Papa
Quebec
Romeo
Sierra
Tango
Uniform
Victor
Whiskey
X-ray
Yankee
Zulu