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

From PrattWiki
Jump to navigation Jump to search
(Lecture 4 - 9/8 - Built-in functions, formatted printing)
Line 55: Line 55:
 
** You can enter numbers in scientific notation with a number followed by the letter <code>e</code> and then a number or negative number for the power of 10; for example, <code>x = 6.02e23</code> or <code>e = -1.6e-19</code>
 
** You can enter numbers in scientific notation with a number followed by the letter <code>e</code> and then a number or negative number for the power of 10; for example, <code>x = 6.02e23</code> or <code>e = -1.6e-19</code>
 
** float can convert scientific notation as well: <code>float("1e-5")</code>
 
** float can convert scientific notation as well: <code>float("1e-5")</code>
 +
 +
== Lecture 5 - 9/11 - Intro to lists, tuples, and strings; Intro to graphs ==
 +
* 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 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>.
 +
* Trinket
 +
<html>
 +
<iframe src="https://trinket.io/embed/python3/b1113c2184" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 +
</html>
 +
* To read more:
 +
** Note!  Many of the tutorials below use Python 2 so instead of <code>print(thing)</code> it shows <code>print thing</code>
 +
** [https://www.tutorialspoint.com/python/python_lists.htm Lists] at tutorialspoint
 +
** [https://www.tutorialspoint.com/python/python_tuples.htm Tuples] at tutorialspoint
 +
* Plotting
 +
** Need to import matplotlib.pyplot as plt
 +
** Create a figure, cerate a set of axes within the figure, plot using that set of axes
 +
** Plots can have different colors, line styles, and/or point styles
 +
** Labels and title are added with the set command
 +
** See [[Python:Plotting]] for more info
 +
<!--
 +
* 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
 +
** 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 <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 apply either ord or chr to multiple items, use a <code>map</code>; to see the results, make a <code>list</code> out of the map
 +
-->

Revision as of 15:47, 11 September 2023

Lecture 1 - 8/28 - Course introduction

Lecture 2 - 9/1 - Introduction to programming

  • Seven steps of programming The Seven Steps Poster
  • Almost all languages have input, output, math, conditional execution (decisions), and repetition (loops)
  • Problem: Consider how to decide if a number is a prime number
    • Some "shortcuts" for specific factors (2, 3, and 5, for example) 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?
    • We can use output to get the computer to ask for a number and we can use input to allow the computer to receive that number
    • We can use math and the mod operator (%) to see if one number is evenly divisible by another, a loop to go through all possible relevant divisors, and a decision structure to choose what to do if we determine that a number is not prime.
  • Very quick tour of Python with Spyder
    • Console (with history tab), info box (with variable explorer, files, and other tabs), and editing window
    • Pushing "play" button or hitting F5 will save the script, change the working directory, and run the script
    • Quick introduction to variable types: int, float, str
    • Quick introduction to indexing: Python is "0" indexed, meaning if there is a collection of items called x, x[0] will be the "first" item in the collection and x[N-1] where N is the total number of items will be the last item. Also, reverse indexing, where x[-1] is the last item and x[-N] is the first item.

Lecture 3 - 9/4 - Introduction to Python

  • No lecture due to Labor Day
  • See User:DukeEgr93/ld for a summary of work to do instead!

Lecture 4 - 9/8 - Built-in functions, formatted printing

  • Python doesn't know everything to start with; may need to import things
    • import MODULE means using MODULE.function() to run - brings in everything in the module under the module's name
    • import MODULE as NAME means using NAME.function() to run - this is the most common one for us
    • from MODULE import FUNCTION1, FUNCTION2, ... means using FUNCTION1(), FUNCTION2() as function calls - be careful not to override things
    • from MODULE import * means importing every function and constant from a module into their own name - very dangerous!
  • Arrays
    • Must import numpy for arrays
    • import numpy as np will be a very common part of code for EGR 103
    • 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 arrays works the way you expect
      • ** * / // % + -
        • With arrays, * and / work element by element; *matrix* multiplication is a different character (specifically, @)
  • Creating formatted strings using {} and .format() (format strings, 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.
    • Using {} by themselves will substitute items in order from the format() function into the string that gets created
    • Putting a number in the {} will tell format which thing to get
    • Format specification comes after a : in the {}; if you do not specify a location index, you still have to put a colon in the {}
      • {:s} means string and {:Xs} where X is an integer means reserve at least that much space for a left-formatted string; {:>s} or {:>Xs} where X is a number will right-justify the string
      • {:f} means floating point (default 6 digits after decimal point) and {:X.Yf} reserves at least X spaces (including + or - and the . if it is there) with Y digits after the decimal point for t right-justified number
      • {:e} means floating point (default 6 digits after decimal point) and {:X.Ye} reserves at least X spaces (including + or - and the . if it is there and the letter e and the + or - after the e and the two or three digit number after that) with Y digits after the decimal point for t right-justified number
    • Aside - Format Specification Mini-Language has all the possibilities; we will cover some but not all of these in later classes
    • You can enter numbers in scientific notation with a number followed by the letter e and then a number or negative number for the power of 10; for example, x = 6.02e23 or e = -1.6e-19
    • float can convert scientific notation as well: float("1e-5")

Lecture 5 - 9/11 - Intro to lists, tuples, and strings; Intro to graphs

  • 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 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.
  • 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
  • Plotting
    • Need to import matplotlib.pyplot as plt
    • Create a figure, cerate a set of axes within the figure, plot using that set of axes
    • Plots can have different colors, line styles, and/or point styles
    • Labels and title are added with the set command
    • See Python:Plotting for more info