User:DukeEgr93/ld

From PrattWiki
Jump to navigation Jump to search

This page assumes you have already installed Anaconda and Box - if you have not, please do the following:

  • Read Box and follow the instructions to get Box Drive installed on your computer and create a folder inside your Box folder for EGR103F23
  • Read Installing_Anaconda_and_Spyder and follow the instructions to install the Anaconda distribution of Python on your computer if you do not already have it or make sure you have Anaconda and Spyder 5.4.3 if you have previously installed Anaconda

Chapra 2.1

Open Spyder from Anaconda (Windows people can open it from the Start->Anaconda3 folder and macOS people will need to open Anaconda Navigator and then launch Spyder). Carefully go through Chapra 2.1 with Spyder open on your computer. Note that the Spyder version pictured is a little older than the one you are using - the tabs for the Explorer window will be in a slightly different place, for example, and there will be four of them (Help, Variable Explorer, Plots, and Files). Furthermore, the icons along the top have changed a little bit. The most important ones at the moment - new file, open file, save, save all, and save & run - all look the same in both versions.

Chapra 2.2.1

Carefully go through Chapra 2.2.1 to learn more about assigning values to variables, importing and using the math module, and the basics of the format command. We will go into much greater detail on the format command later! For now, note the following:

  • The number in front of the : tells format which item to get from its arguments. You can have multiple items to print at once. Here is an example of grabbing the items in a different order from how they are presented to the command:
    In [1]: '{1} {3} {2} {0}'.format(1, 2.0, "three", (4,))
    Out[1]: '2.0 (4,) three 1'
    
  • In the absence of the index numbers, the format will grab items in the order presented:
    In [1]: '{} {} {} {}'.format(1, 2.0, "three", (4,))
    Out[1]: '1 2.0 three (4,)'
    
  • If you want to format the item, the formatting information will come after the index and after a : and you must include the : even if you use the default ordering (i.e. you do not put in an index:
    In [1]: '{:8.1e}'.format(3.5678)
    Out[1]: ' 3.6e+00'
    
    In this case, there is no index so format will just take the next argument it has (in this case, the only argument it has!). The number to the left of the . specifies how much space to reserve; if printing the number correctly takes up less space than that value, format will pad the left side with spaces. In this case, printing the number using the format specified only took 7 sports, so you can see an extra space at the beginning. The number after the . specifies how many digits to put after the decimal point. The format command will round automatically if need be. Finally, the e at the end means to print this as a number in scientific notation. For Python, that means using e$$\pm$$p to represent the $$\times 10^{\pm p}$$ part. 3.6e+00 is the same thing as $$3.6\times 10^{0}$$

Chapra 2.2.2

Carefully go through Chapra 2.2.2 to learn a little more about five different types of variables: list, tuple, set, dictionary, and array. We will look at the first four of these in a later lecture - focus on array for now. Note that array is not a built-in variable type but must be brought in with the numpy module. From the examples in the book, you can see that you create an array using the np.array() function with a function argument that is organized using square brackets. You can make multidimensional arrays! Quick note: we will not generally be using np.matrix; we'll stick with np.array.

Chapra 2.2.3

Carefully go through Chapra 2.2.2 to learn more about indexing. Specifically, note that the "first" item is indexed as 0 and that if you specify an "ending" index, the data you will get will *not* include the value with the ending index. There are many commands in Python that seem to "end before the end" and this is one of them. Also, you can give an optional third value to the index to indicate spacing between indices. Negative spacings mean to go backwards.

  • If the spacing is left out, the spacing will be 1
  • If the spacing is positive, the slice will start at the first index and go until the index is the largest value it can be while being smaller than the end
  • If the spacing is positive, the default for the start is 0 and the default for the end is to include the end; note that a[0:-1] will not include the end but a[0:] will.
  • If the spacing is negative, the slice will start at the first index and go until the index is the smallest value it can be while being larger than the end
  • If the spacing is negative, the default for the start is -1 and the default for the end is to include the 0 index; note that a[-1:0:-1] will not include the 0 index term but a[-1::-1] will.
  • If the starting value is already too large for positive spacing / too small for negative spacing based on the ending, a blank is returned. a[3:1] will return a blank

Check out the following examples and make sure you understand how they work:

In [1]: word = "Duke University"

In [2]: word[0]          # pick out the item in index 0
Out[2]: 'D'

In [3]: word[3:6]        # pick out the items in indices 3, 4, and 5 (stop before 6)
Out[3]: 'e U'

In [4]: word[2:9:2]      # pick out every second index from 2 to 8 (so indices 2, 4, 6, 8)
Out[4]: 'k nv'

In [5]: word[-1:0:-2]    # pick out every second index from the end to 1 going backwards (so indices 14, 12, 10, 8, 6, 4, 2; but NOT 0)
Out[5]: 'yirvn k'

In [6]: word[-1::-2]     # pick out every second index from the end to the beginning inclusive going backwards (so indices 14, 12, 10, 8, 6, 4, 2, 0)
Out[6]: 'yirvn kD'

Chapra 2.2.4

Carefully go through Chapra 2.2.4 to learn more about some useful commands for generating an array of values. Note the different syntax:

  • np.arange(start, finish, delta) will generate an array of numbers starting at start, separated by delta, and finishing before finish; the sign on delta needs to match the direction going from start to finish or you will get an empty array.
  • np.linspace(start, finish, num points) will generate an array of num points values starting at start and ending at finish; if finish is closer to $$-\infty$$ than start, linspace will calculate the negative spacing automatically.
  • np.logspace(start power, finish power, num points) will generate an array of num points values starting at 10**(start power) and ending at 10**(finish power). The big difference here is that you are giving the powers of 10, not the values!

Chapra 2.2.5

Carefully go through Chapra 2.2.5 to learn more about character strings and how to build larger strings. Also look at Table 2.2. Note that the top four functions are functions you call on a string (len(s)) whereas the bottom seven are methods you call using the string. Methods are functions that are built into the definition of the object. len() can be applied to a variety of variable types; endswith() only makes sense with a string. Note with the methods that even if they do not need an argument, you must include the () at the end. You can chain indexing and methods together:

In [1]: word[-1::-1].upper().index("REV")
Out[1]: 4

This is very similar to the German Language! And apparently Swedish! And Danish!

Chapra 2.3

Carefully go through Chapra 2.3 to learn more about mathematical operations. At the part when the book starts with "We can also carry out calculations with matric objects." on Page 30, you can skip to just before the start of Page 32 and "There are two features of the IPython Console we should highlight." We will discuss matrix multiplication in several weeks.