EGR 103/Concept List/F23

From PrattWiki
Revision as of 22:26, 10 September 2023 by DukeEgr93 (talk | contribs)
Jump to navigation Jump to search

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 3 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")