Python Reading

Key Ideas

Conda with IDLE:

TERMINAL
Mac/Linux: Use Terminal app or Linux Shell
Windows: Anaconda Prompt (use that instead of cmd Command Prompt; find it from the Search menu)

% conda --version. # conda 26.1.1
% conda env list # see conda environments (* means active)
% conda activate base  # use default conda environment for python (base)


% which python # Mac
> where python # Windows  
# GOOD: /opt/anaconda3/bin/python (you want to see anaconda3 in the path)
# OTHERWISE: The Fix...


# THE FIX IF IT IS STILL WRONG:
% unalias python # Without this, conda activation was being ignored
% hash -r # clears the cache so it re-checks where python lives.
% python -m idlelib # Run IDLE using the exact Python interpreter currently active


IDLE
>>> import sys
>>> print(sys.executable)
/opt/anaconda3/bin/python   # means it's working

Now some things to be aware of...

The way you start IDLE matters here. NOT by double-clicking on the desktop icon (that would launch IDLE with the path you originally set up when you installed it.) Instead, type the command

% python -m idlelib 

from the command line (Mac: Terminal; Windows Anaconda Prompt). If you want to make sure it will work, first verify that the active environment is the anaconda3. Type the command

% conda env list 

and look for "anaconda3" in the path. That tells you that the anaconda3 environment (the one with all the 3rd party data science modules like Numpy) is the current environment.

In-Class Activity

  1. Create a one-dimensional array called v with 10 integers. Each integer should be a random number between 1 and 100.
  2. Create a new array which consists of the odd indices of previously created array v.
  3. Create a new array in backwards ordering from v.
  4. What will be the output of the following code:
    a = np.array([1, 2, 3, 4, 5])
    b = a[1:4]
    b[0] = 200
    print(a[1])
    
  5. Create a two-dimensional array called m with 25 integers in a 5 by 5 matrix. Each integer should be a random number between 1 and 100.
  6. Create a new array from m, in which the elements of each row are in reverse order.
  7. Create another array from m, where the rows are in reverse order.
  8. Create another array from m, where columns and rows are in reverse order.
  9. Create another array from m, where the first and last row and the first and last column are cut off.