Difference between revisions of "EGR 103/Concept List/F22"

From PrattWiki
Jump to navigation Jump to search
Line 23: Line 23:
 
** If there are <code>"""</code> or <code>'''</code>, Python ignores everything until the closing <code>"""</code> or <code>'''</code>
 
** If there are <code>"""</code> or <code>'''</code>, Python ignores everything until the closing <code>"""</code> or <code>'''</code>
 
** If you use <code># %%</code> in Spyder, the editing window will set up a '''cell''' and light up the cell your cursor is in.  Cells have no impact on how the code runs, just how the code appears in the window  
 
** If you use <code># %%</code> in Spyder, the editing window will set up a '''cell''' and light up the cell your cursor is in.  Cells have no impact on how the code runs, just how the code appears in the window  
<!--
+
 
* To play with Python:
+
== Lecture 3 - 9/5 - "Number" Types ==
** Install it on your machine or a public machine: [https://www.anaconda.com/download/ Download]
+
* Python is a "typed" language
** + - * // (rounded division) and % (remainder / modula) produce in if both sides are an int, float if either or both are floats
+
** Focus of the day: int, float, and array
 +
*** int: integers; Python 3 can store these perfectly
 +
*** float: floating point numbers - "numbers with decimal points" - Python sometimes has problems storing floating point items exactly
 +
** Focus a little later: string, list, tuple
 +
** Focus later: dictionary, set
 +
** Focus way later: map, filter, zip
 +
* Basic operations and types
 +
** + - * // (rounded division) and % (remainder / modulo) produce int if both sides are an int, float if either or both are floats
 
** / (regular division) and // (rounded division) produces float with ints or floats
 
** / (regular division) and // (rounded division) produces float with ints or floats
 
** ** to do powers
 
** ** to do powers
 +
** <code>VAR = input("prompt: ")</code> will ask the user for a value and stores whatever they type as a string (broken in some versions of Spyder!)
 +
** <code>NUM = int(VAR)</code>
 +
*** If VAR is an int or a float, it will return an int rounded towards 0
 +
*** If VAR is a string, it will return an int only if the string looks exactly like an integer
 +
** <code>NUM = float(VAR)</code>
 +
*** If VAR is an int or a float, it will return a float with the same value
 +
*** If VAR is a string, it will return a float if the string looks like a float, including scientific notation such as <code>float("1.23e4")</code>
 +
* Arrays
 
** Python doesn't know everything to start with; may need to import things
 
** Python doesn't know everything to start with; may need to import things
 
*** <code>import MODULE</code> means using <code>MODULE.function()</code> to run
 
*** <code>import MODULE</code> means using <code>MODULE.function()</code> to run
 
*** <code>import MODULE as NAME</code> means using <code>NAME.function()</code> to run
 
*** <code>import MODULE as NAME</code> means using <code>NAME.function()</code> to run
** <code>VAR = input("prompt: ")</code> will ask the user for a value and stores whatever they type as a string
 
** <code>NUM = int(VAR)</code> will convert the item in VAR to an integer if it looks like an integer; error otherwise
 
== Lecture 3 - 8/30 - "Number" Types ==
 
* Finished prime number checker - code is available in the Box drive for the class under Lectures / Lec03
 
** Looked at for loops for running code multiple times
 
** Looked at if...else trees for making decisions
 
** Created a variable to track whether we thought the number was prime
 
* Python is a "typed" language - variables have types.  We will use several types:
 
** Focus of the day: int, float, and array
 
** Focus a little later: string, list, tuple
 
** Focus later: dictionary, set
 
** Focus way later: map, filter, zip
 
* int: integers; Python 3 can store these perfectly
 
* float: floating point numbers - "numbers with decimal points" - Python sometimes has problems storing floating point items exactly
 
* array
 
** Requires numpy, usually with <code>import numpy as np</code>
 
 
** Organizational unit for storing rectangular arrays of numbers
 
** Organizational unit for storing rectangular arrays of numbers
 
** Generally create with np.array(LIST) where depth of nested LIST is dimensionality of array
 
** Generally create with np.array(LIST) where depth of nested LIST is dimensionality of array
Line 72: Line 71:
 
** Only works for arrays!
 
** Only works for arrays!
  
== Lecture 4 - 9/3 - Other Types ==
+
 
* 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 and mutable items within lists can be changed.  Lists can be "grown" by using += with the list.
+
 
 +
<!--
 +
* To play with Python:
 +
** Install it on your machine or a public machine: [https://www.anaconda.com/download/ Download]
 +
 
 +
* Finished prime number checker - code is available in the Box drive for the class under Lectures / Lec03
 +
** Looked at for loops for running code multiple times
 +
** Looked at if...else trees for making decisions
 +
** Created a variable to track whether we thought the number was prime
 +
* Python is a "typed" language - variables have types.  We will use several types:
 +
-->
 +
 
 +
== Lecture 4 - 9/9 - Other Types ==
 +
* 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 and mutable items within lists can be changed.  Lists can be "grown" by using += with the list or l.append().
 
* 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); tuple items cannot be changed but mutable items within tuples can be
 
* 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); tuple items cannot be changed but mutable items within tuples can be
 
* Strings are set off with " " or ' ' and contain characters; string items cannot be changed
 
* Strings are set off with " " or ' ' and contain characters; string items cannot be changed
 
* For lists, tuples, and strings:
 
* For lists, tuples, and strings:
 
** Using + concatenates the two collections
 
** Using + concatenates the two collections
** Using * with them makes creates a collection with the orignal repeated that many times
+
** Using * with them makes creates a collection with the original repeated that many times
 
** Using += will create a new item with something appended to the old item; the "something" needs to be the same type (list, tuple, or string); this may seem to break the "can't be changed" rule but really <code>a += b</code> is <code>a = a + b</code> which creates a new <code>a</code>.
 
** Using += will create a new item with something appended to the old item; the "something" needs to be the same type (list, tuple, or string); this may seem to break the "can't be changed" rule but really <code>a += b</code> is <code>a = a + b</code> which creates a new <code>a</code>.
 
* Characters in strings have "numerical" values based on the ASCII table ([https://www.asciitable.com/ https://www.asciitable.com/])
 
* Characters in strings have "numerical" values based on the ASCII table ([https://www.asciitable.com/ https://www.asciitable.com/])
 
** Numbers are earlier than lower case letters; lower case letters are earlier than upper case letters
 
** Numbers are earlier than lower case letters; lower case letters are earlier than upper case letters
 
** Strings are sorted character by character; if one string is shorter than another, it is considered less  
 
** Strings are sorted character by character; if one string is shorter than another, it is considered less  
*** " Hello" < "Hi" since the "e" comes before the "i"
+
*** " Hello" < "Hi" is True since the "e" comes before the "i"
*** "Zebra" < "apple" since the upper case "Z" is before the lower case "a"
+
*** "Zebra" < "apple" is True since the upper case "Z" is before the lower case "a"
*** "go" < "gone" since the first two characters match and then the word is done
+
*** "go" < "gone" is True since the first two characters match and then the word is done
 
* To get the numerical value of a single character, use <code>ord("A")</code> or replace the A with the character you want
 
* To get the numerical value of a single character, use <code>ord("A")</code> or replace the A with the character you want
 
* To get the character a number represents, use <code>chr(NUM)</code>
 
* To get the character a number represents, use <code>chr(NUM)</code>
Line 98: Line 110:
 
** [https://www.tutorialspoint.com/python/python_tuples.htm Tuples] at tutorialspoint
 
** [https://www.tutorialspoint.com/python/python_tuples.htm Tuples] at tutorialspoint
  
 +
<!--
 
== Lecture 5 - 9/6 - Format and Functions ==
 
== Lecture 5 - 9/6 - Format and Functions ==
 
* Creating formatted strings using {} and .format() ([https://www.python.org/dev/peps/pep-3101/#format-strings format strings], [https://www.python.org/dev/peps/pep-3101/#standard-format-specifiers standard format specifiers]) -- focus was on using s for string and e or f for numerical types, minimumwidth.precision, and possibly a + in front to force printing + for positive numbers.
 
* Creating formatted strings using {} and .format() ([https://www.python.org/dev/peps/pep-3101/#format-strings format strings], [https://www.python.org/dev/peps/pep-3101/#standard-format-specifiers standard format specifiers]) -- focus was on using s for string and e or f for numerical types, minimumwidth.precision, and possibly a + in front to force printing + for positive numbers.

Revision as of 17:05, 10 September 2022

Lecture 1 - 8/29 - Course Introduction

  • Main class page: EGR 103L
    • Includes links to Sakai, Pundit, and Ed pages
  • Sakai page: Sakai 103L page; grades, surveys and tests, some assignment submissions; first day slideshow in Resources section

Lecture 2 - 8/27 - Programs and Programming

  • Almost all languages have input, output, math, conditional execution (decisions), and repetition (loops)
  • Seven steps of programming The Seven Steps Poster. Also, for Monday's class:
  • Problem: Consider how to decide if a number is a prime number
    • Some "shortcuts" for specific factors but need to have a generalized approach
    • See if number is evenly divisible by any integer between 2 and the square root of the number - but how do we ask the computer to do that?
  • Quick tour of Python
    • Console (with history tab), variable explorer (with other tabs), and editing window
    • Main numerical types: whole numbers (int) and numbers with decimals (float)
    • Can use % (called "mod") to get "remainder"
      • If both items are integers, result is an integer; if either is a float, result is a float
    • Relational operators: < <= == >= > !=
      • Result is is either True or False
  • Comments in code:
    • If there is a #, Python ignores everything remaining in that line after the #
    • If there are """ or , Python ignores everything until the closing """ or
    • If you use # %% in Spyder, the editing window will set up a cell and light up the cell your cursor is in. Cells have no impact on how the code runs, just how the code appears in the window

Lecture 3 - 9/5 - "Number" Types

  • Python is a "typed" language
    • Focus of the day: int, float, and array
      • int: integers; Python 3 can store these perfectly
      • float: floating point numbers - "numbers with decimal points" - Python sometimes has problems storing floating point items exactly
    • Focus a little later: string, list, tuple
    • Focus later: dictionary, set
    • Focus way later: map, filter, zip
  • Basic operations and types
    • + - * // (rounded division) and % (remainder / modulo) produce int if both sides are an int, float if either or both are floats
    • / (regular division) and // (rounded division) produces float with ints or floats
    • ** to do powers
    • VAR = input("prompt: ") will ask the user for a value and stores whatever they type as a string (broken in some versions of Spyder!)
    • NUM = int(VAR)
      • If VAR is an int or a float, it will return an int rounded towards 0
      • If VAR is a string, it will return an int only if the string looks exactly like an integer
    • NUM = float(VAR)
      • If VAR is an int or a float, it will return a float with the same value
      • If VAR is a string, it will return a float if the string looks like a float, including scientific notation such as float("1.23e4")
  • Arrays
    • Python doesn't know everything to start with; may need to import things
      • import MODULE means using MODULE.function() to run
      • import MODULE as NAME means using NAME.function() to run
    • Organizational unit for storing rectangular arrays of numbers
    • Generally create with np.array(LIST) where depth of nested LIST is dimensionality of array
      • np.array([1, 2, 3]) is a 1-dimensional array with 3 elements
      • np.array([[1, 2, 3], [4, 5, 6]]) is a 2-dimension array with 2 rows and 3 columns
  • Math with "Number" types works the way you expect
    • ** * / // % + -
    • With arrays, * and / work element by element; *matrix* multiplication is a different character (specifically, @)
  • 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 a collection or change information in mutable collections
  • a[0] is the element in a at the start
  • a[3] is the element in a three away from the start
  • a[-1] is the last element of a
  • a[-2] is the second-to-last element of a
  • 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 a 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 - 9/9 - Other Types

  • 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 and mutable items within lists can be changed. Lists can be "grown" by using += with the list or l.append().
  • 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); tuple items cannot be changed but mutable items within tuples can be
  • Strings are set off with " " or ' ' and contain characters; string items cannot be changed
  • For lists, tuples, and strings:
    • Using + concatenates the two collections
    • Using * with them makes creates a collection with the original repeated that many times
    • Using += will create a new item with something appended to the old item; the "something" needs to be the same type (list, tuple, or string); this may seem to break the "can't be changed" rule but really a += b is a = a + b which creates a new a.
  • Characters in strings have "numerical" values based on the ASCII table (https://www.asciitable.com/)
    • Numbers are earlier than lower case letters; lower case letters are earlier than upper case letters
    • Strings are sorted character by character; if one string is shorter than another, it is considered less
      • " Hello" < "Hi" is True since the "e" comes before the "i"
      • "Zebra" < "apple" is True since the upper case "Z" is before the lower case "a"
      • "go" < "gone" is True since the first two characters match and then the word is done
  • To get the numerical value of a single character, use ord("A") or replace the A with the character you want
  • To get the character a number represents, use chr(NUM)
  • To apply either ord or chr to multiple items, use a map; to see the results, make a list out of the map
  • Trinket

  • 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