28 Aug

Python Basics

  1. Python as a calculator. Apart from addition, subtraction, multiplication and division, there is also support for 
    1. Exponentiation: **
    2. Modulo:
  2. Variables and Types. It is really important to know that different types of variables implie different behavior even when same operators are used.
    1. Variable Assignment. = in Python means assignment, it doesn't test equality! (x=5)
    2. Variable types.
      1. int, or integer: a number without a fractional part. 
      2. float, or floating point: a number that has both an integer and fractional part, separated by a point.
      3. str, or string: a type to represent text.
      4. bool, or boolean: a type to represent logical values. (True or False)
    3. Inner Functions.
      1. To determine the type of a variable x: type(x)
    4. Type conversion. You can convert Python values into any types using str(), int(), bool(), etc.

Python Lists

A list is a compound data type; you can group values together. A list can contain any Python type. 

  1. Create a list.  my_list=["my","list",a,b] where a,b are variables, fr example.
  2. Subsetting lists
    1. One-element. You select the index of your list. my_list[0] and my_list[-1] return the first and the last element, respectively, form your list.
    2. Slicing. My_list[3:5] returns (including) index 3 to (excluding) index 5. My_list[:4] everything up to (excluding) index 4. My_list[5:] returns from (including) index 5 up to the end
    3. Lists within lists. The same rules apply, when an element is a list you can subset it. My_list[1][3]
  3. Changing list elements. My_list[i]=new_value changes the i-th element to new_value. My_list[i:k]=[n_1,...,n_(k-i)] changes the i:k elements 
  4. Adding and Removing elements. 
    1. Adding. My_list_ext=My_list+New_list adds New_list to My_list.
    2. Removing. del(My_list[i]) cuts the i-th element. Be careful! when you remove elements, your list re-index itself.
  5. Behind the scenes. 
    1. When we create a list x=["A","B","C"] we are referencing x to ["A","B","C"], so when writing y=x and changing for example y[1]="Z" we are changing index 1 in ["A","B","C"]  to "Z", so both x and y are now referencing to ["A","Z","C"]
    2. To avoid the latter problem we have various alternatives. Both of which if we change elements to one doesn't affect the other.
      1. y=list(x)
      2. y=x[:]

Python Lists.pdf

Functions

Functions are a piece of reusable code to solve a particular task. You can open up the documentation for a specified function using help(function's_name). It is important that you find functions you think already exists in internet, so that you save time in coding. You can call functions and save your result in a variable. The len() function is extremely useful; it also works on strings to count the number of characters!

  1. Methods. Methods are functions that belong to objects (int, str, list, ...), depending on the type of the object, there are different methods associated to them.  
    1. Examples: if the variables sister, height and fam are str, float and list objects, respectively, then we could use:
      1. sister.capitalize() and sister.replace("z","sa")
      2. height.bit_length() and heigth.conjugate()
      3. fam.index("mom") and fam.count(1.73) (Others include append(), remove(), reverse())
    2. Observations: It is important to notice that some functions alter the value of the object, while others don't.
  2. Packages. It would be a huge mess to have all code in Python distribution, this is where packages enter the game. These are a directory of Python Scripts, where each script have functions, methods and type to solve specific tasks.
    1. Install package. Go to h!p://pip.readthedocs.org/en/stable/installing/ , then download get-pip.py, and in the terminal: python3 get-pip.py ; pip3 install numpy.
    2. Import package. Once you have downloaded the package you have to call it on your script to use it.
      1. import numpy. Then you call functions like this: numpy.array([1,2,3])
      2. import numpy as np. Then you call functions like this: np.array([1,2,3])
      3. from numpy import array. Then you call the function like this: array([1,2,3])
      4. Example: If you want to use the function inv(), which is in the linalg subpackage of the scipy package. You want to be able to use this as my_inv then you write: from scipy.linalg import inv as my_inv

ch3_Functions.pdf

NumPy package

When working with data we always want to perform vast calculations fast, and is in this way that lists are not useful since, for example, you cannot perform element wise calculations. NumPy stands for "Numeric Python"

  1. 1D NumPy Arrays. These are another kind of object, the alternative for python lists; you can perform element wise calculations, easy and fast. Its argument is a list object. An example (where height is a list) :  import numpy as np; np_height=np.array(height); bmi=np_weight/np_height**2
    1. Remarks: NumPy arrays contain only one type of elements (while lists not necessarily). Remember, different types: different behavior.
    2. Subsetting: We can subset just as we did with lists, but also we can subset with booleans just like this:[In] bmi=np.array([ 21.852, 20.975, 21.75 , 24.747, 21.441]) ; [In] bmi>23 [Out] array([False, False, False, True, False], dtype=bool) ; [In] bmi[bmi > 23] [Out] array([ 24.747])
  2. 2D NumPy Arrays. We can construct n-dimensional arrays, for that we just need a list containing lists, on which each list corresponds to a row. The method ".shape" gives us the array structure.
    1. Subsetting. It works when we use the usual list subsetting, with one bracket to access the first dimension and another to intersect it wit the second dimension. We also can use the [A,B] way, where A corresponds to the elements in the 1st dim and B to the 2nd dim (A and B can be of the form ":", "x:y", etc)
    2. Example: (Subsetting arrays can be realy easy!) Have a look at the code below where the elements "a" and "c" are extracted from a list of lists.
      # regular list of lists x = [["a", "b"], ["c", "d"]] [x[0][0], x[1][0]] 
      # numpy import numpy as np np_x = np.array(x) np_x[:,0]

      For regular Python lists, this is a real pain. For 2D numpy arrays, however, it's pretty intuitive!

    3. Aritmetic.

      1. np_mat = np.array([[1, 2],[3, 4],[5, 6]]) ;np_mat * 2; np_mat + np.array([10, 10]) ;np_mat + np_mat
        1. when multiplied by 2, all its elements are multiplied by 2, when summed with a 2 by 1 array all its rows are wise-summed, when summed with itself is the same as multiplying by 2.
  3. NumPy: Basic Statistics. When we have little data, it is simple to deduce some characteristics, however it is not that simple when having huge data sets. we need to summarize them using, for example,  statistics.
    1. Basic functions. np.mean(), np.median(), np.corrcoef(), np.std(). 
    2. Generate data. Example.
      1. height = np.round(np.random.normal(1.75, 0.20, 5000), 2) ;weight = np.round(np.random.normal(60.32, 15, 5000), 2) (Arguments:  mean, standard dev, number of samples) ;np_city = np.column_stack((height, weight))
    3. Observation. NumPy has sum(), sort(),... which Python has by default, the difference is that NumPy restricted to single data type is faster to do calculations!

ch4_NumPy.pdf





Comments
* The email will not be published on the website.
I BUILT MY SITE FOR FREE USING