From 0a8bbab108b24b3e3e37a58b622660da75d367e0 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Thu, 4 Mar 2021 13:11:36 -0700 Subject: [PATCH 01/12] initial adding of tutorial python basics content --- foundations/basic-python.md | 556 ++++++++++++++++++++++++++++++++++++ 1 file changed, 556 insertions(+) diff --git a/foundations/basic-python.md b/foundations/basic-python.md index d9809057d..0fcfd486c 100644 --- a/foundations/basic-python.md +++ b/foundations/basic-python.md @@ -11,3 +11,559 @@ We can use [Jupyter](jupyter) notebooks for much of the content, which will be r Here's a minimal example: - [Fun with Python](Hello) + +----------- +Why Python? +----------- + +You're already here because you want to learn to use Python for your data +analysis and visualizations. Python can be compared to other high-level, +interpreted, object-oriented languages, but is especially great because it is +free and open source! + +High level languages: + Other high level languages include MatLab, IDL, and NCL. The advantage of + high level languages is that they provide functions, data structures, and + other utilities that are commonly used, which means it takes less code to + get real work done. The disadvantage of high level languages is that they + tend to obscure the low level aspects of the machine such as: memory use, + how many floating point operations are happening, and other information + related to performance. C and C++ are all examples of lower level + languages. The "higher" the level of language, the more computing + fundamentals are abstracted. + +Interpreted languages: + Most of your work is probably already in interpreted languages if you've + ever used IDL, NCL, or MatLab (interpreted languages are typically also + high level). So you are already familiar with the advantages of this: you + don't have to worry about compiling or machine compatability (it is + portable). And you are probably familiar with their deficiencies: sometimes + they can be slower than compiled languages and potentially more memory + intensive. + +Object Oriented languages: + Objects are custom datatypes. For every custom datatype, you usually have + a set of operations you might want to conduct. For example, if you have an + object that is a list of numbers you might want to apply a mathematical + operation, such as sum, onto this list object in bulk. Not every function + can be applied to every datatype; it wouldn't make sense to apply a + logarithm to a string of letters or to capitalize a list of numbers. Data + and the operations applied to them are grouped together into one object. + +Open source: + Python as a language is open source which means that there is a community + of developers behind its codebase. Anyone can join the developer community + and contribute to deciding the future of the language. When someone + identifies gaps to Python's abilities, they can write up the code to fill + these gaps. The open source nature of Python means that Python as a + language is very adaptable to shifting needs of the user community. + +Python is a language designed for rapid prototyping and efficient programming. +It is easy to write new code quickly with less typing. + +------------------------- +First Python Script +------------------------- + +This section of the tutorial will focus on teaching you Python through the +creation of your first script. You will learn about syntax and the reasoning +behind why things are done the way they are along the way. We will also +incorporate lessons on the use of Git because we highly recommend you version +controling your work. + +We are assuming you are familiar with bash and terminal commands. If not +`here is a cheat sheet `_. + +~~~~~~~~~~~~~~~~~~~ +Reading a .txt File +~~~~~~~~~~~~~~~~~~~ + +In building your first Python script we will set up our workspace, read a +:code:`.txt` file, and learn Git fundamentals. + +Here is a video recording of the live tutorial covering "Reading a .txt File": + +.. youtube:: Jog7ybd6amw + :height: 315 + :width: 560 + :align: center + +.. + +.. seealso:: + + `Questions and Answers from the live "Reading a .txt File" tutorial `_. + +.. + +Let's begin. + +1. Open a terminal. + + .. note:: + + On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **Terminal**. + + .. + +2. Create a directory: + + .. code-block:: bash + + $ mkdir python_tutorial + + .. + + The first thing we have to do is create a directory to store our work. + Let's call it :code:`python_tutorial`. + +3. Go into the directory: + + .. code-block:: bash + + $ cd python_tutorial + +4. Create a virtual environment for this project: + + .. code-block:: bash + + $ conda create --name python_tutorial python + + .. + + A conda environment is a directory that contains a collection of packages + or libraries that you would like installed and accessible for this workflow. + Type :bash:`conda create --name` and the name of your project, here that is + :code:`python_tutorial`, and then specify that you would like to install Python + in the virtual environment for this project. + + It is a good idea to create new environments for different projects because + since Python is open source, new versions of the tools you use may become + available. This is a way of guaranteeing that your script will use the same + versions of packages and libraries and should run the same as you expect it + to. + + .. seealso:: + + `More information on Conda environments `_ + + .. + +5. And activate your Conda environment: + + .. code-block:: bash + + $ conda activate python_tutorial + + .. + +6. Make the directory a Git repository: + + .. code-block:: bash + + $ git init . + + .. + + A Git repository tracks changes made to files within your project. It looks + like a :code:`.git/` folder inside that project. + + This command adds version control to this new python_tutorial directory + and all of its contents. + + .. seealso:: + + `More information on Git repositories `_ + + .. + +7. Create a data directory: + + .. code-block:: bash + + $ mkdir data + + .. + + And we'll make a directory for our data. + +8. Go into the data directory: + + .. code-block:: bash + + $ cd data + +9. Download sample data from the CU Boulder weather station: + + .. code-block:: bash + + $ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt + + .. + + This weather station is a Davis Instruments wireless Vantage Pro2 located on + the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft + elevation). The station is monitored by the Atmospheric and Oceanic Sciences + (ATOC) department and is part of the larger University of Colorado ATOC + Weather Network. + +10. Check the status of your repository: + + .. code-block:: bash + + $ git status + + .. + + You will see the newly created :bash:`data` directory (which is listed as + :bash:`./`, since you are currently *in* that directory) is listed as + "untracked," which means all of the files you added to that directory are + *also* untracked by Git. The :bash:`git status` command will tell you what + to do with untracked files. Those instructions mirror the next 2 steps: + +11. Add the file to the Git staging area: + + .. code-block:: bash + + $ git add wxobs20170821.txt + + .. + + By adding this datafile to your directory, you have made a change that is + not yet reflected in our Git repository. Every file in your working directory is classified + by git as "untracked", "unmodified", "modified", or "staged." + Type :bash:`git add` and then the name of the altered file to stage your change, + i.e. moving a file that is either untracked or modified to the staged category so they can be committed. + + .. seealso:: + + `More information on git add `_ + + .. + +12. Check your git status once again: + + .. code-block:: bash + + $ git status + + .. + + Now this file is listed as a "change to be commited," i.e. staged. Staged + changes can now be commited to your repository history. + +13. Commit the file to the Git repository: + + .. code-block:: bash + + $ git commit -m "Adding sample data file" + + .. + + With :bash:`git commit`, you've updated your repository with all the changes + you staged, in this case just one file. + + .. note:: + + On a Windows machine you may see the following: :bash:`warning: LF will be replaced by CRLF.` The file will have its original line endings in your working directory. + Do not worry too much about this warning. CR refers to "Carriage Return Line Feed" and LF refers to "Line Feed." Both are used to indicate line termination. + In Windows both a Carriage Return and Line Feed are required to note the end of a line, but in Linux/UNIX only a Line Feed is required. Most text editors can account for line ending differences between opperating systems, but sometimes a conversion is necessary. + To silence this warning you can type :bash:`git config --global core.autocrlf false` in the terminal. + + .. + +14. Look at the Git logs: + + .. code-block:: bash + + $ git log + + .. + + If you type :bash:`git log` you will show a log of all the commits, or changes + made to your repository. + +15. Go back to the top-level directory: + + .. code-block:: bash + + $ cd .. + + .. + +16. And now that you've set up our workspace, create a blank Python script, + called :code:`mysci.py`: + + .. code-block:: bash + + $ touch mysci.py + + .. + + .. note:: + + If you are working on a Windows machine it is possible that :bash:`touch` will not be + recognized as an internal or external command. If this is the case, run + :bash:`conda install m2-base` to enable unix commands such as :bash:`touch`. + + .. + +17. Edit the :code:`mysci.py` file using nano, vim, or your favorite text editor: + + .. code-block:: python + :linenos: + + print("Hello, world!") + + .. + + Your classic first command will be to print :python:`Hello, world!`. + + .. note:: + + On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor. + + .. + +18. Try testing the script by typing :bash:`python` and then the name of your script: + + .. code-block:: bash + + $ python mysci.py + + .. + + **Yay!** You've just created your first Python script. + + +19. You probably won't need to run your Hello World script again, so delete the + :python:`print("Hello, world!")` line and start over with something more useful - + we'll read the first 4 lines from our datafile. + + Change the :code:`mysci.py` script to read: + + .. code-block:: python + :linenos: + + # Read the data file + filename = "data/wxobs20170821.txt" + datafile = open(filename, 'r') + + print(datafile.readline()) + print(datafile.readline()) + print(datafile.readline()) + print(datafile.readline()) + + datafile.close() + + .. + + First create a variable for your datafile name, which is a string - this + can be in single or double quotes. + + Then create a variable associated with the opened file, here it is called + :python:`datafile`. + + The :python:`'r'` argument in the open command indicates that we are opening + the file for reading capabilities. Other input arguments for open include + :python:`'w'`, for example, if you wanted to write to the file. + + The readline command moves through the open file, always reading the next + line. + + And remember to close your datafile. + + Comments in Python are indicated with a hash, as you can see in the first + line :python:`# Read the data file`. Comments are ignored by the interpreter. + + .. seealso:: + + `More information on the open() function `_ + + .. + +20. And test your script again by typing: + + .. code-block:: bash + + $ python mysci.py + + .. + + Testing of your script with :bash:`python mysci.py` should be done every time + you wish to execute the script. This will no longer be specified as a + unique step in between every change to our script. + +21. Change the :code:`mysci.py` script to read your whole data file: + + .. code-block:: python + :linenos: + + # Read the data file + filename = "data/wxobs20170821.txt" + datafile = open(filename, 'r') + + data = datafile.read() + + datafile.close() + + # DEBUG + print(data) + print('data') + + .. + + Our code is similar as before, but now we've read the entire file. To + test that this worked. We'll :python:`print(data)`. Print statements in python + require parenthesis around the object you wish to print, in this scenario the data object. + + Try :python:`print('data')` as well. Now Python will print the string + :code:`data`, as it did for the hello world function, instead of the + information stored in the variable data. + + Don't forget to execute with :bash:`python mysci.py`. + +22. Change the :code:`mysci.py` script to read your whole data file using a context + manager with: + + .. code-block:: python + :linenos: + + # Read the data file + filename = "data/wxobs20170821.txt" + with open(filename, 'r') as datafile: + data = datafile.read() + + # DEBUG + print(data) + + .. + + Again this is a similar method of opening the datafile, but we now use :python:`with open`. + The :python:`with` statement is a context manager that provides clean-up and + assures that the file is automatically closed after you've read it. + + The indendation of the line :python:`data = datafile.read()` is very important. + Python is sensitive to white space and will not work if you mix spaces and + tabs (Python does not know your tab width). It is best practice to use + spaces as opposed to tabs (tab width is not consistent between editors). + + Combined these two lines mean: with the datafile opened, I'd like to read + it. + + And execute with :bash:`python mysci.py`. + + .. seealso:: + + `More information on context managers `_ + + .. + +23. What did we just see? What is the data object? What type is data? How do we + find out? + + Change the DEBUG section of our script to: + + .. code-block:: python + :lineno-start: 6 + + # DEBUG + print(type(data)) + + .. + + And execute with :bash:`python mysci.py` + + Object types refer to :python:`float`, :python:`integer`, :python:`string` + or other types that you can create. + + Python is a dynamically typed language, which means you don't have to + explicitly specify the datatype when you name a variable, Python will + automatically figure it out by the nature of the data. + +24. Now, clean up the script by removing the DEBUG section, before we commit + this to Git. + +25. Let's check the status of our Git repository + + .. code-block:: bash + + $ git status + + .. + + .. note:: + + Take a look at which files have been changed in the repository! + + .. + +26. Stage these changes: + + .. code-block:: bash + + $ git add mysci.py + + .. + +27. Let's check the status of our Git repository,again. What's different from + the last time we checked the status? + + .. code-block:: bash + + $ git status + + .. + +28. Commit these changes: + + .. code-block:: bash + + $ git commit -m "Adding script file" + + .. + + Here a good commit message :code:`-m` for our changes would be + :code:`"Adding script file"` + +29. Let's check the status of our Git repository, now. It should tell you that + there are no changes made to your repository (i.e., your repository is + up-to-date with the state of the code in your directory). + + .. code-block:: bash + + $ git status + + .. + +30. Look at the Git logs, again: + + .. code-block:: bash + + $ git log + + .. + + You can also print simplified logs with the :code:`--oneline` option. + +----- + +That concludes the first lesson of this virtual tutorial. + +In this section you set up a workspace by creating your directory, conda +environment, and git repository. You downloaded a .txt file and read it using +the Python commands of :python:`open()`, :python:`readline()`, :python:`read()`, +:python:`close()`, and :python:`print()`, as well as the context manager +:python:`with`. You should be familiar with the :python:`str` datatype. You +also used fundamental git commands such as :bash:`git init`, :bash:`git status`, +:bash:`git add`, :bash:`git commit`, and :bash:`git log`. + + +.. seealso:: + + - `Conda environments `_ + - `Git repositories `_ + - `The open() function `_ + - `Context managers `_ + +.. From 4d88e685029b3ead1bf30c8249fcbf2569eb5cee Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Thu, 18 Mar 2021 10:46:37 -0600 Subject: [PATCH 02/12] add rough content --- foundations/basic-python.md | 607 +++++++++--------------------------- 1 file changed, 153 insertions(+), 454 deletions(-) diff --git a/foundations/basic-python.md b/foundations/basic-python.md index 0fcfd486c..9ce02e337 100644 --- a/foundations/basic-python.md +++ b/foundations/basic-python.md @@ -12,558 +12,257 @@ Here's a minimal example: - [Fun with Python](Hello) ------------ -Why Python? ------------ - -You're already here because you want to learn to use Python for your data -analysis and visualizations. Python can be compared to other high-level, -interpreted, object-oriented languages, but is especially great because it is -free and open source! - -High level languages: - Other high level languages include MatLab, IDL, and NCL. The advantage of - high level languages is that they provide functions, data structures, and - other utilities that are commonly used, which means it takes less code to - get real work done. The disadvantage of high level languages is that they - tend to obscure the low level aspects of the machine such as: memory use, - how many floating point operations are happening, and other information - related to performance. C and C++ are all examples of lower level - languages. The "higher" the level of language, the more computing - fundamentals are abstracted. - -Interpreted languages: - Most of your work is probably already in interpreted languages if you've - ever used IDL, NCL, or MatLab (interpreted languages are typically also - high level). So you are already familiar with the advantages of this: you - don't have to worry about compiling or machine compatability (it is - portable). And you are probably familiar with their deficiencies: sometimes - they can be slower than compiled languages and potentially more memory - intensive. - -Object Oriented languages: - Objects are custom datatypes. For every custom datatype, you usually have - a set of operations you might want to conduct. For example, if you have an - object that is a list of numbers you might want to apply a mathematical - operation, such as sum, onto this list object in bulk. Not every function - can be applied to every datatype; it wouldn't make sense to apply a - logarithm to a string of letters or to capitalize a list of numbers. Data - and the operations applied to them are grouped together into one object. - -Open source: - Python as a language is open source which means that there is a community - of developers behind its codebase. Anyone can join the developer community - and contribute to deciding the future of the language. When someone - identifies gaps to Python's abilities, they can write up the code to fill - these gaps. The open source nature of Python means that Python as a - language is very adaptable to shifting needs of the user community. - -Python is a language designed for rapid prototyping and efficient programming. -It is easy to write new code quickly with less typing. - -------------------------- -First Python Script -------------------------- - -This section of the tutorial will focus on teaching you Python through the -creation of your first script. You will learn about syntax and the reasoning -behind why things are done the way they are along the way. We will also -incorporate lessons on the use of Git because we highly recommend you version -controling your work. - -We are assuming you are familiar with bash and terminal commands. If not -`here is a cheat sheet `_. - -~~~~~~~~~~~~~~~~~~~ -Reading a .txt File -~~~~~~~~~~~~~~~~~~~ - -In building your first Python script we will set up our workspace, read a -:code:`.txt` file, and learn Git fundamentals. - -Here is a video recording of the live tutorial covering "Reading a .txt File": - -.. youtube:: Jog7ybd6amw - :height: 315 - :width: 560 - :align: center - -.. - -.. seealso:: - - `Questions and Answers from the live "Reading a .txt File" tutorial `_. - -.. - -Let's begin. +## The Road to Python -1. Open a terminal. - - .. note:: - - On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **Terminal**. - - .. - -2. Create a directory: - - .. code-block:: bash - - $ mkdir python_tutorial - - .. - - The first thing we have to do is create a directory to store our work. - Let's call it :code:`python_tutorial`. - -3. Go into the directory: - - .. code-block:: bash - - $ cd python_tutorial - -4. Create a virtual environment for this project: - - .. code-block:: bash - - $ conda create --name python_tutorial python - - .. - - A conda environment is a directory that contains a collection of packages - or libraries that you would like installed and accessible for this workflow. - Type :bash:`conda create --name` and the name of your project, here that is - :code:`python_tutorial`, and then specify that you would like to install Python - in the virtual environment for this project. - - It is a good idea to create new environments for different projects because - since Python is open source, new versions of the tools you use may become - available. This is a way of guaranteeing that your script will use the same - versions of packages and libraries and should run the same as you expect it - to. - - .. seealso:: - - `More information on Conda environments `_ - - .. - -5. And activate your Conda environment: - - .. code-block:: bash - - $ conda activate python_tutorial - - .. - -6. Make the directory a Git repository: - - .. code-block:: bash - - $ git init . +What brings you here? Are you a scientist who knows how to code, but not in Python? Are you new to coding entirely? Do you have a specific problem you are trying to solve? - .. +## Why Python? - A Git repository tracks changes made to files within your project. It looks - like a :code:`.git/` folder inside that project. +You're already here because you want to learn to use Python for your data analysis and visualizations. Python can be compared to other high-level, interpreted, object-oriented languages, but is especially great because it is free and open source! - This command adds version control to this new python_tutorial directory - and all of its contents. +**High level languages:** +Other high level languages include MatLab, IDL, and NCL. The advantage of high level languages is that the provide functions, data structures, and other utilities that are commonly used, which means it takes less code to get real work done. The disadvantage of high level languages is that they tend to obscure the low level aspects of the machine such as: memory use, how many floating point operations are happening, and other information related to performance. C and C++ are all examples of lower level languages. The "higher" the level of language, the more computing fundamentals are abstracted. - .. seealso:: +**Interpreted languages:** +Most of your work is probably already in interpreted languages if you've ever used IDL, NCL, or MatLab (interpreted languages are typically also high level). So you are already familiar with the advantages of this: you don't have to worry about compiling or machine compatability (it is portable). And you are probably familiar with their deficiencies: sometimes they can be slower than compiled languages and potentially more memory intensive. - `More information on Git repositories `_ +**Object Oriented languages:** +Objects are custom datatypes. For every custom datatype, you usually have a set of operations you might want to conduct. For example, if you have an object that is a list of numbers you might want to apply a mathematical operation, such as sum, onto this list object in bulk. Not every function can be applied to every datatype; it wouldn't make sense to apply a logarithm to a string of letters or to capitalize a list of numbers. Data and the operations applied to them are grouped together into one object. - .. +**Open source:** +Python as a language is open source which means that there is a community of developers behind its codebase. Anyone can join the developer community and contribute to deciding the future of the language. When someone identifies gaps to Python's abilities, they can write up the code to fill these gaps. The open source nature of Python means that Python as a language is very adaptable to shifting needs of the user community. -7. Create a data directory: +Python is a language designed for rapid prototyping and efficient programming. It is easy to write new code quickly with less typing. - .. code-block:: bash +## Chosing a Python Platform - $ mkdir data +Through is no single official platform for the Python language. You can run Python from the terminal command line, through a Jupyter session, in Spyder, and in your favorite IDE (integraged develpment platform). Here we will briefly review each interface and how to choose. In general, it is always best to test your programs in the same environment in which they will be run. The biggest factors to consider when chosing your platform are: - .. +1. What are you already comfortable with? +2. What are the people around you using (peers, coworkers, instructors, etc)? - And we'll make a directory for our data. +### Terminal -8. Go into the data directory: +There are a few reasons one might chose to run Python in the terminal. For learners who are familiar with basic [Linix commands](https://cheatography.com/davechild/cheat-sheets/linux-command-line/).and text editors, this is the quickest route to learn Python syntax without the having to cover the bells and whistles of your new platform. If you are runing Python on a super computer, through an HTTP request, or ssh tunneling you might want to consider learning in the terminal. - .. code-block:: bash +### Jupyter Session - $ cd data +Jupyter Notebooks are very popular among data scientists. It is a free, open-source, interactive tool that allows you to run Python code in "cells." This means that your workflow can alternate between code, output, and even Markdown-formatted explanatory sections that create an easy to follow analysis from start to finish. Jupyter notebooks are a great option for presentations or learning tools. For this reason many of the lessons in this book will be taught via Jupyter. -9. Download sample data from the CU Boulder weather station: +### Spyder - .. code-block:: bash +Spyder is a Python specific IDE that comes with the Anaconda download. It is perhaps the most familiar IDE if you are coming from languages such as Matlab that have a language specific platform. Many classes at universities are taught in the Spyder interface. - $ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt +### Other IDEs - .. +If you already code in other languages you might already have a favorite IDE, such as Visual Studio Code, that will work just as well in Python. PyCharm is an IDE that is very popular and Python-specific. - This weather station is a Davis Instruments wireless Vantage Pro2 located on - the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft - elevation). The station is monitored by the Atmospheric and Oceanic Sciences - (ATOC) department and is part of the larger University of Colorado ATOC - Weather Network. +## Installation -10. Check the status of your repository: +The installation may look slightly different depending on the Python platform you are using. Please follow the instructions underneath the platform you chosee. - .. code-block:: bash +### Terminal - $ git status +If running Python in the terminal it is best to install Miniconda. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). - .. +Then create a Conda environment with Python installed by typing the following into your terminal: +`conda create --name pythia-foundations python` - You will see the newly created :bash:`data` directory (which is listed as - :bash:`./`, since you are currently *in* that directory) is listed as - "untracked," which means all of the files you added to that directory are - *also* untracked by Git. The :bash:`git status` command will tell you what - to do with untracked files. Those instructions mirror the next 2 steps: +A conda environment is a directory that contains a collection of packages or libraries that you would like installed and accessible for this workflow. Type `conda create --name` and the name of your environment, and then specify that you would like to install Python in the virtual environment for this project. -11. Add the file to the Git staging area: +It is a good idea to create new environments for different projects because since Python is open source, new versions of the tools you use may become available. This is a way of guaranteeing that your script will use the same versions of packages and libraries and should run the same as you expect it to. - .. code-block:: bash +### Jupyter Session - $ git add wxobs20170821.txt +To run a Jupyter session you will need to install some necessary packages into your Conda environment. +You can install `miniconda`. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). - .. +Then create a Conda environment with Python installed. +`conda create --name pythia-foundations python jupyterlab nb_conda_kernels` - By adding this datafile to your directory, you have made a change that is - not yet reflected in our Git repository. Every file in your working directory is classified - by git as "untracked", "unmodified", "modified", or "staged." - Type :bash:`git add` and then the name of the altered file to stage your change, - i.e. moving a file that is either untracked or modified to the staged category so they can be committed. +Or you can install the full [Anaconda](https://www.anaconda.com/products/individual), and select Jupyter in the GUI. **have a screenshot of that here** - .. seealso:: +### Spyder - `More information on git add `_ +Install the full [Anaconda](https://www.anaconda.com/products/individual), and select Spyder in the GUI. **have a screenshot of that here** - .. +### Other IDEs -12. Check your git status once again: +**Add notes or links to documentation for each IDE.** - .. code-block:: bash +## First Python Script in the Terminal - $ git status +This section of the tutorial will focus on teaching you Python through the creation of your first script. I think I want to review how to execute Python in each cell, and then change this document to be platform agnostic. - .. +### Reading a .txt File - Now this file is listed as a "change to be commited," i.e. staged. Staged - changes can now be commited to your repository history. +In building your first Python script we will set up our workspace and read a `.txt` file. -13. Commit the file to the Git repository: - - .. code-block:: bash - - $ git commit -m "Adding sample data file" - - .. - - With :bash:`git commit`, you've updated your repository with all the changes - you staged, in this case just one file. - - .. note:: - - On a Windows machine you may see the following: :bash:`warning: LF will be replaced by CRLF.` The file will have its original line endings in your working directory. - Do not worry too much about this warning. CR refers to "Carriage Return Line Feed" and LF refers to "Line Feed." Both are used to indicate line termination. - In Windows both a Carriage Return and Line Feed are required to note the end of a line, but in Linux/UNIX only a Line Feed is required. Most text editors can account for line ending differences between opperating systems, but sometimes a conversion is necessary. - To silence this warning you can type :bash:`git config --global core.autocrlf false` in the terminal. - - .. - -14. Look at the Git logs: - - .. code-block:: bash - - $ git log - - .. - - If you type :bash:`git log` you will show a log of all the commits, or changes - made to your repository. - -15. Go back to the top-level directory: - - .. code-block:: bash - - $ cd .. - - .. - -16. And now that you've set up our workspace, create a blank Python script, - called :code:`mysci.py`: - - .. code-block:: bash - - $ touch mysci.py - - .. - - .. note:: +1. Open a terminal. - If you are working on a Windows machine it is possible that :bash:`touch` will not be - recognized as an internal or external command. If this is the case, run - :bash:`conda install m2-base` to enable unix commands such as :bash:`touch`. + On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **Terminal**. - .. +2. Activate your Conda environment: -17. Edit the :code:`mysci.py` file using nano, vim, or your favorite text editor: + ``` + $ conda activate pythia_foundations + ``` - .. code-block:: python - :linenos: +3. Create a directory to store our work. Let's call it `pythia_foundations`. - print("Hello, world!") + ``` + $ mkdir pythia_foundations + ``` - .. +4. Go into the directory: - Your classic first command will be to print :python:`Hello, world!`. + ``` + $ cd python_tutorial + ``` - .. note:: +5. And create a data directory: - On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor. +``` +mkdir data +``` - .. +6. Go into the data directory: + ` -18. Try testing the script by typing :bash:`python` and then the name of your script: +``` +$ cd data +``` - .. code-block:: bash +7. Download sample data from the CU Boulder weather station: - $ python mysci.py +``` +$ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt +``` - .. +This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network. - **Yay!** You've just created your first Python script. +7. Go back to the top-level directory: +``` +$ cd .. +``` -19. You probably won't need to run your Hello World script again, so delete the - :python:`print("Hello, world!")` line and start over with something more useful - - we'll read the first 4 lines from our datafile. +8. And now that you've set up our workspace, create a blank Python script, called `mysci.py`: - Change the :code:`mysci.py` script to read: +``` +$ touch mysci.py +``` - .. code-block:: python - :linenos: +If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`. - # Read the data file - filename = "data/wxobs20170821.txt" - datafile = open(filename, 'r') +9. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!". - print(datafile.readline()) - print(datafile.readline()) - print(datafile.readline()) - print(datafile.readline()) +```python +print("Hello, world!") +``` - datafile.close() +On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor. - .. +10. To run a Python script from the command line type, "python" and then the name of your script: - First create a variable for your datafile name, which is a string - this - can be in single or double quotes. +``` +$ python mysci.py +``` - Then create a variable associated with the opened file, here it is called - :python:`datafile`. +11. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile. - The :python:`'r'` argument in the open command indicates that we are opening - the file for reading capabilities. Other input arguments for open include - :python:`'w'`, for example, if you wanted to write to the file. +Change the `mysci.py` script to read: - The readline command moves through the open file, always reading the next - line. +```python +# Read the data file +filename = "data/wxobs20170821.txt" +datafile = open(filename, 'r') - And remember to close your datafile. +print(datafile.readline()) +print(datafile.readline()) +print(datafile.readline()) +print(datafile.readline()) - Comments in Python are indicated with a hash, as you can see in the first - line :python:`# Read the data file`. Comments are ignored by the interpreter. +datafile.close() - .. seealso:: +``` - `More information on the open() function `_ +First create a variable for your datafile name, which is a string - this can be in single or double quotes. - .. +Then create a variable associated with the opened file, here it is called `datafile`. -20. And test your script again by typing: +The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file. - .. code-block:: bash +The readline command moves through the open file, always reading the next line. - $ python mysci.py +And remember to close your datafile. - .. +Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter. - Testing of your script with :bash:`python mysci.py` should be done every time - you wish to execute the script. This will no longer be specified as a - unique step in between every change to our script. +12. Run your script by typing: -21. Change the :code:`mysci.py` script to read your whole data file: +``` +$ python mysci.py +``` - .. code-block:: python - :linenos: +Testing of your script with `python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script. - # Read the data file - filename = "data/wxobs20170821.txt" - datafile = open(filename, 'r') +13. Change the `mysci.py` script to read your whole data file: - data = datafile.read() +```python +# Read the data file +filename = "data/wxobs20170821.txt" +datafile = open(filename, 'r') - datafile.close() +data = datafile.read() - # DEBUG - print(data) - print('data') +datafile.close() - .. +# DEBUG +print(data) +print('data') +``` - Our code is similar as before, but now we've read the entire file. To - test that this worked. We'll :python:`print(data)`. Print statements in python - require parenthesis around the object you wish to print, in this scenario the data object. +Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object. - Try :python:`print('data')` as well. Now Python will print the string - :code:`data`, as it did for the hello world function, instead of the - information stored in the variable data. +Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data. - Don't forget to execute with :bash:`python mysci.py`. +Don't forget to execute with :bash:`python mysci.py`. -22. Change the :code:`mysci.py` script to read your whole data file using a context +14. Change the `mysci.py` script to read your whole data file using a context manager with: - .. code-block:: python - :linenos: - - # Read the data file - filename = "data/wxobs20170821.txt" - with open(filename, 'r') as datafile: - data = datafile.read() - - # DEBUG - print(data) - - .. - - Again this is a similar method of opening the datafile, but we now use :python:`with open`. - The :python:`with` statement is a context manager that provides clean-up and - assures that the file is automatically closed after you've read it. - - The indendation of the line :python:`data = datafile.read()` is very important. - Python is sensitive to white space and will not work if you mix spaces and - tabs (Python does not know your tab width). It is best practice to use - spaces as opposed to tabs (tab width is not consistent between editors). - - Combined these two lines mean: with the datafile opened, I'd like to read - it. - - And execute with :bash:`python mysci.py`. - - .. seealso:: - - `More information on context managers `_ - - .. - -23. What did we just see? What is the data object? What type is data? How do we - find out? - - Change the DEBUG section of our script to: - - .. code-block:: python - :lineno-start: 6 - - # DEBUG - print(type(data)) - - .. - - And execute with :bash:`python mysci.py` - - Object types refer to :python:`float`, :python:`integer`, :python:`string` - or other types that you can create. +```python +# Read the data file +filename = "data/wxobs20170821.txt" +with open(filename, 'r') as datafile: + data = datafile.read() - Python is a dynamically typed language, which means you don't have to - explicitly specify the datatype when you name a variable, Python will - automatically figure it out by the nature of the data. - -24. Now, clean up the script by removing the DEBUG section, before we commit - this to Git. - -25. Let's check the status of our Git repository - - .. code-block:: bash - - $ git status - - .. - - .. note:: - - Take a look at which files have been changed in the repository! - - .. - -26. Stage these changes: - - .. code-block:: bash - - $ git add mysci.py - - .. - -27. Let's check the status of our Git repository,again. What's different from - the last time we checked the status? - - .. code-block:: bash - - $ git status - - .. - -28. Commit these changes: - - .. code-block:: bash - - $ git commit -m "Adding script file" - - .. - - Here a good commit message :code:`-m` for our changes would be - :code:`"Adding script file"` - -29. Let's check the status of our Git repository, now. It should tell you that - there are no changes made to your repository (i.e., your repository is - up-to-date with the state of the code in your directory). - - .. code-block:: bash - - $ git status - - .. - -30. Look at the Git logs, again: +# DEBUG +print(data) +``` - .. code-block:: bash +Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it. - $ git log +The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors). - .. +Combined these two lines mean: with the datafile opened, I'd like to read it. - You can also print simplified logs with the :code:`--oneline` option. +And execute with `python mysci.py`. ------ +15. What did we just see? What is the data object? What type is data? How do we find out? -That concludes the first lesson of this virtual tutorial. +Change the DEBUG section of our script to: -In this section you set up a workspace by creating your directory, conda -environment, and git repository. You downloaded a .txt file and read it using -the Python commands of :python:`open()`, :python:`readline()`, :python:`read()`, -:python:`close()`, and :python:`print()`, as well as the context manager -:python:`with`. You should be familiar with the :python:`str` datatype. You -also used fundamental git commands such as :bash:`git init`, :bash:`git status`, -:bash:`git add`, :bash:`git commit`, and :bash:`git log`. +```python +# DEBUG +print(type(data)) +``` +And execute with `python mysci.py`. -.. seealso:: +Object types refer to `float`, `integer`, `string` or other types that you can create. - - `Conda environments `_ - - `Git repositories `_ - - `The open() function `_ - - `Context managers `_ +Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data. -.. +In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt file and read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and `print()`, as well as the context manager `with`. You should be familiar with the `str` datatype. From 2fd20055346dbfc80c94f0a849392741b8e97d53 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Thu, 18 Mar 2021 10:47:10 -0600 Subject: [PATCH 03/12] changes --- foundations/basic-python.md | 175 ++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 88 deletions(-) diff --git a/foundations/basic-python.md b/foundations/basic-python.md index 9ce02e337..4140649a2 100644 --- a/foundations/basic-python.md +++ b/foundations/basic-python.md @@ -122,147 +122,146 @@ In building your first Python script we will set up our workspace and read a `.t 5. And create a data directory: -``` -mkdir data -``` + ``` + mkdir data + ``` 6. Go into the data directory: - ` -``` -$ cd data -``` + ``` + $ cd data + ``` 7. Download sample data from the CU Boulder weather station: -``` -$ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt -``` + ``` + $ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt + ``` -This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network. + This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network. -7. Go back to the top-level directory: +8. Go back to the top-level directory: -``` -$ cd .. -``` + ``` + $ cd .. + ``` -8. And now that you've set up our workspace, create a blank Python script, called `mysci.py`: +9. And now that you've set up our workspace, create a blank Python script, called `mysci.py`: -``` -$ touch mysci.py -``` + ``` + $ touch mysci.py + ``` -If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`. + If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`. -9. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!". +10. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!". -```python -print("Hello, world!") -``` + ```python + print("Hello, world!") + ``` -On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor. + On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor. -10. To run a Python script from the command line type, "python" and then the name of your script: +11. To run a Python script from the command line type, "python" and then the name of your script: -``` -$ python mysci.py -``` + ``` + $ python mysci.py + ``` -11. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile. +12. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile. -Change the `mysci.py` script to read: + Change the `mysci.py` script to read: -```python -# Read the data file -filename = "data/wxobs20170821.txt" -datafile = open(filename, 'r') + ```python + # Read the data file + filename = "data/wxobs20170821.txt" + datafile = open(filename, 'r') -print(datafile.readline()) -print(datafile.readline()) -print(datafile.readline()) -print(datafile.readline()) + print(datafile.readline()) + print(datafile.readline()) + print(datafile.readline()) + print(datafile.readline()) -datafile.close() + datafile.close() -``` + ``` -First create a variable for your datafile name, which is a string - this can be in single or double quotes. + First create a variable for your datafile name, which is a string - this can be in single or double quotes. -Then create a variable associated with the opened file, here it is called `datafile`. + Then create a variable associated with the opened file, here it is called `datafile`. -The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file. + The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file. -The readline command moves through the open file, always reading the next line. + The readline command moves through the open file, always reading the next line. -And remember to close your datafile. + And remember to close your datafile. -Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter. + Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter. -12. Run your script by typing: +13. Run your script by typing: -``` -$ python mysci.py -``` + ``` + $ python mysci.py + ``` -Testing of your script with `python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script. + Testing of your script with `python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script. -13. Change the `mysci.py` script to read your whole data file: +14. Change the `mysci.py` script to read your whole data file: -```python -# Read the data file -filename = "data/wxobs20170821.txt" -datafile = open(filename, 'r') + ```python + # Read the data file + filename = "data/wxobs20170821.txt" + datafile = open(filename, 'r') -data = datafile.read() + data = datafile.read() -datafile.close() + datafile.close() -# DEBUG -print(data) -print('data') -``` + # DEBUG + print(data) + print('data') + ``` -Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object. + Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object. -Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data. + Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data. -Don't forget to execute with :bash:`python mysci.py`. + Don't forget to execute with :bash:`python mysci.py`. -14. Change the `mysci.py` script to read your whole data file using a context +15. Change the `mysci.py` script to read your whole data file using a context manager with: -```python -# Read the data file -filename = "data/wxobs20170821.txt" -with open(filename, 'r') as datafile: - data = datafile.read() + ```python + # Read the data file + filename = "data/wxobs20170821.txt" + with open(filename, 'r') as datafile: + data = datafile.read() -# DEBUG -print(data) -``` + # DEBUG + print(data) + ``` -Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it. + Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it. -The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors). + The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors). -Combined these two lines mean: with the datafile opened, I'd like to read it. + Combined these two lines mean: with the datafile opened, I'd like to read it. -And execute with `python mysci.py`. + And execute with `python mysci.py`. -15. What did we just see? What is the data object? What type is data? How do we find out? +16. What did we just see? What is the data object? What type is data? How do we find out? -Change the DEBUG section of our script to: + Change the DEBUG section of our script to: -```python -# DEBUG -print(type(data)) -``` + ```python + # DEBUG + print(type(data)) + ``` -And execute with `python mysci.py`. + And execute with `python mysci.py`. -Object types refer to `float`, `integer`, `string` or other types that you can create. + Object types refer to `float`, `integer`, `string` or other types that you can create. -Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data. + Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data. In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt file and read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and `print()`, as well as the context manager `with`. You should be familiar with the `str` datatype. From e29914456fe76ba5cb9dee10ac030e71fd348344 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 31 Mar 2021 16:48:21 -0600 Subject: [PATCH 04/12] python basic content first pass --- _toc.yml | 14 +- foundations/basic-python.md | 267 ------------------ foundations/conda.md | 5 - .../{ => getting-started-python}/Hello.ipynb | 0 .../getting-started-python/basic-python.md | 167 +++++++++++ foundations/getting-started-python/conda.md | 25 ++ .../getting-started-python.md | 0 .../how-to-run-python.md | 37 +++ foundations/getting-started-python/ides.md | 10 + foundations/getting-started-python/jupyter.md | 22 ++ .../getting-started-python/terminal.md | 17 ++ .../getting-started-python/why-python.md | 17 ++ foundations/how-to-run-python.md | 10 - 13 files changed, 304 insertions(+), 287 deletions(-) delete mode 100644 foundations/basic-python.md delete mode 100644 foundations/conda.md rename foundations/{ => getting-started-python}/Hello.ipynb (100%) create mode 100644 foundations/getting-started-python/basic-python.md create mode 100644 foundations/getting-started-python/conda.md rename foundations/{ => getting-started-python}/getting-started-python.md (100%) create mode 100644 foundations/getting-started-python/how-to-run-python.md create mode 100644 foundations/getting-started-python/ides.md create mode 100644 foundations/getting-started-python/jupyter.md create mode 100644 foundations/getting-started-python/terminal.md create mode 100644 foundations/getting-started-python/why-python.md delete mode 100644 foundations/how-to-run-python.md diff --git a/_toc.yml b/_toc.yml index 52545b6f0..3893823e0 100644 --- a/_toc.yml +++ b/_toc.yml @@ -11,14 +11,18 @@ - part: Foundational skills chapters: - file: foundations/overview - - file: foundations/getting-started-python + - file: foundations/getting-started-python/getting-started-python sections: - - file: foundations/basic-python + - file: foundationsgetting-started-python/why-python sections: - - file: foundations/Hello - - file: foundations/how-to-run-python + - file: foundations/getting-started-python/Hello + - file: foundations/getting-started-python/how-to-run-python sections: - - file: foundations/conda + - file: foundations/getting-started-python/conda + - file: foundations/getting-started-python/terminal + - file: foundations/getting-started-python/jupyter + - file: foundations/getting-started-python/ides + - file: foundations/getting-started-python/basic-python - file: foundations/getting-started-jupyter sections: - file: foundations/jupyter-notebooks diff --git a/foundations/basic-python.md b/foundations/basic-python.md deleted file mode 100644 index ee903c452..000000000 --- a/foundations/basic-python.md +++ /dev/null @@ -1,267 +0,0 @@ -# Quickstart: what is Python? - -```{note} -This content is under construction! -``` - -This section will contain tutorials on some basics of Python syntax. We will not cover a comprehensive introduction to programming concepts, but will aim this material at people who have done at least some coding before but are new to Python. - -We can use [Jupyter](jupyter) notebooks for much of the content, which will be rendered here as static pages but also be Binderized for interactive learning. - -Here's a minimal example: - -- [Fun with Python](Hello) - -## The Road to Python - -What brings you here? Are you a scientist who knows how to code, but not in Python? Are you new to coding entirely? Do you have a specific problem you are trying to solve? - -## Why Python? - -You're already here because you want to learn to use Python for your data analysis and visualizations. Python can be compared to other high-level, interpreted, object-oriented languages, but is especially great because it is free and open source! - -**High level languages:** -Other high level languages include MatLab, IDL, and NCL. The advantage of high level languages is that the provide functions, data structures, and other utilities that are commonly used, which means it takes less code to get real work done. The disadvantage of high level languages is that they tend to obscure the low level aspects of the machine such as: memory use, how many floating point operations are happening, and other information related to performance. C and C++ are all examples of lower level languages. The "higher" the level of language, the more computing fundamentals are abstracted. - -**Interpreted languages:** -Most of your work is probably already in interpreted languages if you've ever used IDL, NCL, or MatLab (interpreted languages are typically also high level). So you are already familiar with the advantages of this: you don't have to worry about compiling or machine compatability (it is portable). And you are probably familiar with their deficiencies: sometimes they can be slower than compiled languages and potentially more memory intensive. - -**Object Oriented languages:** -Objects are custom datatypes. For every custom datatype, you usually have a set of operations you might want to conduct. For example, if you have an object that is a list of numbers you might want to apply a mathematical operation, such as sum, onto this list object in bulk. Not every function can be applied to every datatype; it wouldn't make sense to apply a logarithm to a string of letters or to capitalize a list of numbers. Data and the operations applied to them are grouped together into one object. - -**Open source:** -Python as a language is open source which means that there is a community of developers behind its codebase. Anyone can join the developer community and contribute to deciding the future of the language. When someone identifies gaps to Python's abilities, they can write up the code to fill these gaps. The open source nature of Python means that Python as a language is very adaptable to shifting needs of the user community. - -Python is a language designed for rapid prototyping and efficient programming. It is easy to write new code quickly with less typing. - -## Chosing a Python Platform - -Through is no single official platform for the Python language. You can run Python from the terminal command line, through a Jupyter session, in Spyder, and in your favorite IDE (integraged develpment platform). Here we will briefly review each interface and how to choose. In general, it is always best to test your programs in the same environment in which they will be run. The biggest factors to consider when chosing your platform are: - -1. What are you already comfortable with? -2. What are the people around you using (peers, coworkers, instructors, etc)? - -### Terminal - -There are a few reasons one might chose to run Python in the terminal. For learners who are familiar with basic [Linix commands](https://cheatography.com/davechild/cheat-sheets/linux-command-line/).and text editors, this is the quickest route to learn Python syntax without the having to cover the bells and whistles of your new platform. If you are runing Python on a super computer, through an HTTP request, or ssh tunneling you might want to consider learning in the terminal. - -### Jupyter Session - -Jupyter Notebooks are very popular among data scientists. It is a free, open-source, interactive tool that allows you to run Python code in "cells." This means that your workflow can alternate between code, output, and even Markdown-formatted explanatory sections that create an easy to follow analysis from start to finish. Jupyter notebooks are a great option for presentations or learning tools. For this reason many of the lessons in this book will be taught via Jupyter. - -### Spyder - -Spyder is a Python specific IDE that comes with the Anaconda download. It is perhaps the most familiar IDE if you are coming from languages such as Matlab that have a language specific platform. Many classes at universities are taught in the Spyder interface. - -### Other IDEs - -If you already code in other languages you might already have a favorite IDE, such as Visual Studio Code, that will work just as well in Python. PyCharm is an IDE that is very popular and Python-specific. - -## Installation - -The installation may look slightly different depending on the Python platform you are using. Please follow the instructions underneath the platform you chosee. - -### Terminal - -If running Python in the terminal it is best to install Miniconda. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). - -Then create a Conda environment with Python installed by typing the following into your terminal: -`conda create --name pythia-foundations python` - -A conda environment is a directory that contains a collection of packages or libraries that you would like installed and accessible for this workflow. Type `conda create --name` and the name of your environment, and then specify that you would like to install Python in the virtual environment for this project. - -It is a good idea to create new environments for different projects because since Python is open source, new versions of the tools you use may become available. This is a way of guaranteeing that your script will use the same versions of packages and libraries and should run the same as you expect it to. - -### Jupyter Session - -To run a Jupyter session you will need to install some necessary packages into your Conda environment. -You can install `miniconda`. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). - -Then create a Conda environment with Python installed. -`conda create --name pythia-foundations python jupyterlab nb_conda_kernels` - -Or you can install the full [Anaconda](https://www.anaconda.com/products/individual), and select Jupyter in the GUI. **have a screenshot of that here** - -### Spyder - -Install the full [Anaconda](https://www.anaconda.com/products/individual), and select Spyder in the GUI. **have a screenshot of that here** - -### Other IDEs - -**Add notes or links to documentation for each IDE.** - -## First Python Script in the Terminal - -This section of the tutorial will focus on teaching you Python through the creation of your first script. I think I want to review how to execute Python in each cell, and then change this document to be platform agnostic. - -### Reading a .txt File - -In building your first Python script we will set up our workspace and read a `.txt` file. - -1. Open a terminal. - - On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **Terminal**. - -2. Activate your Conda environment: - - ``` - $ conda activate pythia_foundations - ``` - -3. Create a directory to store our work. Let's call it `pythia_foundations`. - - ``` - $ mkdir pythia_foundations - ``` - -4. Go into the directory: - - ``` - $ cd python_tutorial - ``` - -5. And create a data directory: - - ``` - mkdir data - ``` - -6. Go into the data directory: - - ``` - $ cd data - ``` - -7. Download sample data from the CU Boulder weather station: - - ``` - $ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt - ``` - - This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network. - -8. Go back to the top-level directory: - - ``` - $ cd .. - ``` - -9. And now that you've set up our workspace, create a blank Python script, called `mysci.py`: - - ``` - $ touch mysci.py - ``` - - If you are working on a Windows machine it is possible that `touch` will not be recognized as an internal or external command. If this is the case, run `conda install m2-base` to enable unix commands such as `touch`. - -10. Edit the `mysci.py` file using nano, vim, or your favorite text editor to include the classic first command - printing, "Hello, world!". - - ```python - print("Hello, world!") - ``` - - On a Windows machine, it is possible `nano` or `vim` are not recognized as text editors within your terminal. In this case simply try to run `mysci.py` to open a notepad editor. - -11. To run a Python script from the command line type, "python" and then the name of your script: - - ``` - $ python mysci.py - ``` - -12. You probably won't need to run your Hello World script again, so delete the `print("Hello, world!")` line and start over with something more useful - we'll read the first 4 lines from our datafile. - - Change the `mysci.py` script to read: - - ```python - # Read the data file - filename = "data/wxobs20170821.txt" - datafile = open(filename, 'r') - - print(datafile.readline()) - print(datafile.readline()) - print(datafile.readline()) - print(datafile.readline()) - - datafile.close() - - ``` - - First create a variable for your datafile name, which is a string - this can be in single or double quotes. - - Then create a variable associated with the opened file, here it is called `datafile`. - - The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file. - - The readline command moves through the open file, always reading the next line. - - And remember to close your datafile. - - Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter. - -13. Run your script by typing: - - ``` - $ python mysci.py - ``` - - Testing of your script with `python mysci.py` should be done every time you wish to execute the script. This will no longer be specified as a unique step in between every change to our script. - -14. Change the `mysci.py` script to read your whole data file: - - ```python - # Read the data file - filename = "data/wxobs20170821.txt" - datafile = open(filename, 'r') - - data = datafile.read() - - datafile.close() - - # DEBUG - print(data) - print('data') - ``` - - Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object. - - Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data. - - Don't forget to execute with :bash:`python mysci.py`. - -15. Change the `mysci.py` script to read your whole data file using a context - manager with: - - ```python - # Read the data file - filename = "data/wxobs20170821.txt" - with open(filename, 'r') as datafile: - data = datafile.read() - - # DEBUG - print(data) - ``` - - Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it. - - The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors). - - Combined these two lines mean: with the datafile opened, I'd like to read it. - - And execute with `python mysci.py`. - -16. What did we just see? What is the data object? What type is data? How do we find out? - - Change the DEBUG section of our script to: - - ```python - # DEBUG - print(type(data)) - ``` - - And execute with `python mysci.py`. - - Object types refer to `float`, `integer`, `string` or other types that you can create. - - Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data. - -In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt file and read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and `print()`, as well as the context manager `with`. You should be familiar with the `str` datatype. diff --git a/foundations/conda.md b/foundations/conda.md deleted file mode 100644 index c75f73447..000000000 --- a/foundations/conda.md +++ /dev/null @@ -1,5 +0,0 @@ -# Using the conda package manager - -```{note} -This content is under construction! -``` diff --git a/foundations/Hello.ipynb b/foundations/getting-started-python/Hello.ipynb similarity index 100% rename from foundations/Hello.ipynb rename to foundations/getting-started-python/Hello.ipynb diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md new file mode 100644 index 000000000..0310a6037 --- /dev/null +++ b/foundations/getting-started-python/basic-python.md @@ -0,0 +1,167 @@ +# Quickstart: what is Python? + +```{note} +This content is under construction! +``` + +This section will contain tutorials on some basics of Python syntax. We will not cover a comprehensive introduction to programming concepts, but will aim this material at people who have done at least some coding before but are new to Python. + +We can use [Jupyter](jupyter) notebooks for much of the content, which will be rendered here as static pages but also be Binderized for interactive learning. + +Here's a minimal example: + +- [Fun with Python](Hello) + +## An introduction to Python. + +This section of the tutorial will focus on teaching you Python through the + +### Reading a .txt File + +In building your first Python script we will set up our workspace and read a `.txt` file. + +1. Create a directory to store your work. We'll use the name `pythia_foundations`. + +The following steps will assume you are using a Jupyter notebook. Open your Open a terminal. + +On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **Terminal**. + +2. Activate your Conda environment: + + ``` + $ conda activate pythia_foundations + ``` + +3. Create a directory to store our work. Let's call it `pythia-foundations`. + + ``` + $ mkdir pythia-foundations + ``` + +4. Go into the directory: + + ``` + $ cd pythia-foundations + ``` + +5. And create a data directory: + + ``` + mkdir data + ``` + +6. Go into the data directory: + + ``` + $ cd data + ``` + +7. Download sample data from the CU Boulder weather station: + + ``` + $ curl -kO https://sundowner.colorado.edu/weather/atoc8/wxobs20170821.txt + ``` + + This weather station is a Davis Instruments wireless Vantage Pro2 located on the CU-Boulder east campus at the SEEC building (40.01 N, 05.24 W, 5250 ft elevation). The station is monitored by the Atmospheric and Oceanic Sciences (ATOC) department and is part of the larger University of Colorado ATOC Weather Network. + +8. Go back to the top-level directory: + + ``` + $ cd .. + ``` + +9. And now that you've set up our workspace, open a Jupyter notebook. + + ``` + jupyter lab + ``` + +10. In the Jupyter session, create a blank Python script, called `mysci.py`, you can do this from the "File" drop-down menu. + +11. In a code cell enter and execute the classic first command - printing, "Hello, world!". + + ```python + print("Hello, world!") + ``` + +You'll see that the cell output is executed right below each cell. + +12. In a new cell, let's read the first 4 lines from our datafile. + + ```python + # Read the data file + filename = "data/wxobs20170821.txt" + datafile = open(filename, 'r') + + print(datafile.readline()) + print(datafile.readline()) + print(datafile.readline()) + print(datafile.readline()) + + datafile.close() + + ``` + + First create a variable for your datafile name, which is a string - this can be in single or double quotes. + + Then create a variable associated with the opened file, here it is called `datafile`. + + The `'r'` argument in the open command indicates that we are opening the file for reading capabilities. Other input arguments for open include `'w'`, for example, if you wanted to write to the file. + + The readline command moves through the open file, always reading the next line. + + And remember to close your datafile. + + Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter. + +13. In a new cell, read your whole data file: + + ```python + # Read the data file + filename = "data/wxobs20170821.txt" + datafile = open(filename, 'r') + + data = datafile.read() + + datafile.close() + + # DEBUG + print(data) + print('data') + ``` + + Our code is similar as before, but now we've read the entire file. To test that this worked. We'll `print(data)`. Print statements in python require parenthesis around the object you wish to print, in this scenario the data object. + + Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data. + +14. Let's read your whole data file using a context manager `with`, in a new cell: + + ```python + # Read the data file + filename = "data/wxobs20170821.txt" + with open(filename, 'r') as datafile: + data = datafile.read() + + # DEBUG + print(data) + ``` + + Again this is a similar method of opening the datafile, but we now use `with open`. The `with` statement is a context manager that provides clean-up and assures that the file is automatically closed after you've read it. + + The indendation of the line `data = datafile.read()` is very important. Python is sensitive to white space and will not work if you mix spaces and tabs (Python does not know your tab width). It is best practice to use four spaces as opposed to tabs (tab width is not consistent between editors). + + Combined these two lines mean: with the datafile opened, I'd like to read it. + +15. What did we just see? What is the data object? What type is data? How do we find out? + +In a new cell print the type of our data: + + ```python + print(type(data)) + ``` + + Object types refer to `float`, `integer`, `string` or other types that you can create. + + Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data. + +In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt file and read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and `print()`, as well as the context manager `with`. You should be familiar with the `str` datatype. diff --git a/foundations/getting-started-python/conda.md b/foundations/getting-started-python/conda.md new file mode 100644 index 000000000..902f657ec --- /dev/null +++ b/foundations/getting-started-python/conda.md @@ -0,0 +1,25 @@ +# Using the Conda Package Manager + +Conda is an open-source, cross-platform, language-agnostic package manager and environment management system that allows you to quickly install, run, and update packages within your work environment(s). + +## What are packages? + +A Python package is a collection of modules, which in turn, are essentially Python files that contain published functionality. There are Python packages for data input, data analysis, data visualization, etc. Each package offers a unique toolset and may have its own unique syntax rules. + +Package management is useful because you may want to update a package for one of your projects, but keep it at the same version in other projects to ensure that they continue to run as expected. + +## Installing Conda + +We recommmend you install Miniconda. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). + +Miniconda is a paired down version of Anaconda. [Installing Anaconda](https://docs.anaconda.com/anaconda/install/) takes longer and takes up more disk space, but provides you with more functionality. Anaconda comes with a Jupyter and Spyder, a Python-specific integrated development platform (IDE), installations as well as other packages. The interface of Anaconda is great if you are uncomfortable with the terminal. + +## Creating a Conda environment + +A conda environment is a directory that contains a collection of packages or libraries that you would like installed and accessible for this workflow. To create a new Conda environment, type `conda create --name` and the name of your environment in your terminal, and then specify any packages that you would like to have installed. For example, to install a Jupyter-ready environment, type `conda create --name sample_environment python jupyterlab`. + +It is a good idea to create new environments for different projects because since Python is open source, new versions of the tools you use may become available. This is a way of guaranteeing that your script will use the same versions of packages and libraries and should run the same as you expect it to. + +Some other useful Conda commands are for checking what conda environments you have (`conda env list`), activating a specific environment (`conda activate sample_environment`), checking what packages are installed inside your environment (`conda list`), and deactivating your environment (`conda deactivate`). + +The Anaconda navigator offers functionality for selecting environments and installing packages into them without using the command line. diff --git a/foundations/getting-started-python.md b/foundations/getting-started-python/getting-started-python.md similarity index 100% rename from foundations/getting-started-python.md rename to foundations/getting-started-python/getting-started-python.md diff --git a/foundations/getting-started-python/how-to-run-python.md b/foundations/getting-started-python/how-to-run-python.md new file mode 100644 index 000000000..992035de0 --- /dev/null +++ b/foundations/getting-started-python/how-to-run-python.md @@ -0,0 +1,37 @@ +# How to run Python + +This section provides an overview of different ways to run Python code, and quickstart guides for + +- Chosing a Python platform +- Installing and running Python on various local platforms +- Running Python in the cloud + +## Chosing a Python Platform + +There is no single official platform for the Python language. Here we provide a brief rundown of 3 popular platforms: + +1. The terminal, +2. Jupyter notebooks, and +3. IDEs (integrated development environment). + We highly encourage the use of Jupyter notebooks; all of our tutorials will be in a Jupyter notebook format. Here we hope to provide you with enough information to understand the differences and similarities between each platform so that you can make the best chose for your work environment and learn along effectively, regardless of your Python platform preference. + +In general, it is always best to test your programs in the same environment in which they will be run. The biggest factors to consider when chosing your platform are: + +- What are you already comfortable with? +- What are the people around you using (peers, coworkers, instructors, etc)? + +### Terminal + +For learners who are familiar with basic [Linix commands](https://cheatography.com/davechild/cheat-sheets/linux-command-line/).and text editors, learning Python in the terminal is the quickest route straight to learning Python syntax without the covering the bells and whistles of your new platform. If you are runing Python on a super computer, through an HTTP request, or ssh tunneling you might want to consider learning in the terminal. + +### Jupyter Session + +Jupyter Notebooks are very popular among data scientists. It is a free, open-source, interactive tool that allows you to run Python code in "cells." This means that your workflow can alternate between code, output, and even Markdown-formatted explanatory sections that create an easy to follow analysis from start to finish. Jupyter notebooks are a great option for presentations or learning tools. For these reasons the lessons in this book will be taught via Jupyter notebooks. + +### Other IDEs + +If you already code in other languages you might already have a favorite IDE that will work just as well in Python. Spyder is a Python specific IDE that comes with the Anaconda download. It is perhaps the most familiar IDE if you are coming from languages such as Matlab that have a language specific platform and display a list of variables. PyCharm and Visual Studio Code are also popular IDEs. Many IDEs offer support for terminal execution, scripts, and Jupyter display. To learn about your specific IDE visit its official documentation. + +--- + +We recommend eventually learning how to develop and run Python code in each of these platforms. diff --git a/foundations/getting-started-python/ides.md b/foundations/getting-started-python/ides.md new file mode 100644 index 000000000..632c9f548 --- /dev/null +++ b/foundations/getting-started-python/ides.md @@ -0,0 +1,10 @@ +# Python in an IDE + +You'd like to learn Python in an integrated development platform (IDE). Here we will cover: + +- Installing Python in an IDE +- Running Python code in an IDE + +## Installing Python in an IDE + +## Running Python in an IDE diff --git a/foundations/getting-started-python/jupyter.md b/foundations/getting-started-python/jupyter.md new file mode 100644 index 000000000..7d392a471 --- /dev/null +++ b/foundations/getting-started-python/jupyter.md @@ -0,0 +1,22 @@ +# Running Python in Jupyter + +You'd like to learn Python in Jupyter. Here we will cover: + +- Installing Python in the Jupyter +- Running Python code in the Jupyter + +## Installing Python for Jupyter + +To run a Jupyter session you will need to install some necessary packages into your Conda environment. +You can install `miniconda`. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). + +Then create a Conda environment with Python installed. +`conda create --name pythia_foundations python jupyterlab nb_conda_kernels` + +Or you can install the full [Anaconda](https://www.anaconda.com/products/individual), and select Jupyter in the GUI. **have a screenshot of that here** + +Test that you have installed everything correctly by activating your environment with `conda activate pythia_foundations` and running `jupyter lab`. + +A new window should open automatically in your default browser. You can change the browser with `jupyter lab —browser=chrome`. + +## Running Python in Jupyter diff --git a/foundations/getting-started-python/terminal.md b/foundations/getting-started-python/terminal.md new file mode 100644 index 000000000..f5f086d53 --- /dev/null +++ b/foundations/getting-started-python/terminal.md @@ -0,0 +1,17 @@ +# Python in the Terminal + +You'd like to learn Python in the terminal. Here we will cover: + +- Installing Python in the terminal +- Running Python code in the terminal + +## Installing Python in the Terminal + +If running Python in the terminal it is best to install Miniconda. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). + +Then create a Conda environment with Python installed by typing the following into your terminal: +`conda create --name pythia_foundations python` + +You can test that this by running `python` in the command line. + +## Running Python in the Terminal diff --git a/foundations/getting-started-python/why-python.md b/foundations/getting-started-python/why-python.md new file mode 100644 index 000000000..31d1ed3e2 --- /dev/null +++ b/foundations/getting-started-python/why-python.md @@ -0,0 +1,17 @@ +# Why Python? + +You're already here because you want to learn to use Python for your data analysis and visualizations. Python can be compared to other high-level, interpreted, object-oriented languages, but is especially great because it is free and open source! + +**High level languages:** +Other high level languages include MatLab, IDL, and NCL. The advantage of high level languages is that the provide functions, data structures, and other utilities that are commonly used, which means it takes less code to get real work done. The disadvantage of high level languages is that they tend to obscure the low level aspects of the machine such as: memory use, how many floating point operations are happening, and other information related to performance. C and C++ are all examples of lower level languages. The "higher" the level of language, the more computing fundamentals are abstracted. + +**Interpreted languages:** +Most of your work is probably already in interpreted languages if you've ever used IDL, NCL, or MatLab (interpreted languages are typically also high level). So you are already familiar with the advantages of this: you don't have to worry about compiling or machine compatability (it is portable). And you are probably familiar with their deficiencies: sometimes they can be slower than compiled languages and potentially more memory intensive. + +**Object Oriented languages:** +Objects are custom datatypes. For every custom datatype, you usually have a set of operations you might want to conduct. For example, if you have an object that is a list of numbers you might want to apply a mathematical operation, such as sum, onto this list object in bulk. Not every function can be applied to every datatype; it wouldn't make sense to apply a logarithm to a string of letters or to capitalize a list of numbers. Data and the operations applied to them are grouped together into one object. + +**Open source:** +Python as a language is open source which means that there is a community of developers behind its codebase. Anyone can join the developer community and contribute to deciding the future of the language. When someone identifies gaps to Python's abilities, they can write up the code to fill these gaps. The open source nature of Python means that Python as a language is very adaptable to shifting needs of the user community. + +Python is a language designed for rapid prototyping and efficient programming. It is easy to write new code quickly with less typing. diff --git a/foundations/how-to-run-python.md b/foundations/how-to-run-python.md deleted file mode 100644 index 6e94f9d68..000000000 --- a/foundations/how-to-run-python.md +++ /dev/null @@ -1,10 +0,0 @@ -# How to run Python - -```{note} -This content is under construction! -``` - -This section will give an overview of different ways to run Python code, and quickstart guides for - -- Installing Python on a laptop with the conda package manager -- Running Python in the cloud From 7d8692e193e2a93e8f7c119daadeb3a21ee4a3db Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 31 Mar 2021 16:58:28 -0600 Subject: [PATCH 05/12] fix .py to .ipynb --- foundations/getting-started-python/basic-python.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md index 0310a6037..69487fd42 100644 --- a/foundations/getting-started-python/basic-python.md +++ b/foundations/getting-started-python/basic-python.md @@ -14,7 +14,9 @@ Here's a minimal example: ## An introduction to Python. -This section of the tutorial will focus on teaching you Python through the +This section of the tutorial will focus on teaching you Python through in a Jupyter notebook. + +If you are using a terminal, you will need to edit your script in a text editor and execute with the command "python SCRIPTNAME.PY". ### Reading a .txt File @@ -76,7 +78,7 @@ On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **T jupyter lab ``` -10. In the Jupyter session, create a blank Python script, called `mysci.py`, you can do this from the "File" drop-down menu. +10. In the Jupyter session, create a blank Python script, called `mysci.ipynb`, you can do this from the "File" drop-down menu. 11. In a code cell enter and execute the classic first command - printing, "Hello, world!". From 6a3b4ecc5af07ac19d7543634536a5e37648a420 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 31 Mar 2021 17:00:25 -0600 Subject: [PATCH 06/12] fix numbers --- foundations/getting-started-python/basic-python.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md index 69487fd42..a46dfa759 100644 --- a/foundations/getting-started-python/basic-python.md +++ b/foundations/getting-started-python/basic-python.md @@ -72,15 +72,15 @@ On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **T $ cd .. ``` -9. And now that you've set up our workspace, open a Jupyter notebook. +9. And now that you've set up our workspace, open a Jupyter notebook called `mysci.ipynb`. ``` jupyter lab ``` -10. In the Jupyter session, create a blank Python script, called `mysci.ipynb`, you can do this from the "File" drop-down menu. + Then, in the Jupyter session, create a blank notebook from the "File" drop-down menu. -11. In a code cell enter and execute the classic first command - printing, "Hello, world!". +10. In a code cell enter and execute the classic first command - printing, "Hello, world!". ```python print("Hello, world!") @@ -88,7 +88,7 @@ On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **T You'll see that the cell output is executed right below each cell. -12. In a new cell, let's read the first 4 lines from our datafile. +11. In a new cell, let's read the first 4 lines from our datafile. ```python # Read the data file @@ -116,7 +116,7 @@ You'll see that the cell output is executed right below each cell. Comments in Python are indicated with a hash, as you can see in the first line `# Read the data file`. Comments are ignored by the interpreter. -13. In a new cell, read your whole data file: +12. In a new cell, read your whole data file: ```python # Read the data file @@ -136,7 +136,7 @@ You'll see that the cell output is executed right below each cell. Try `print('data')` as well. Now Python will print the string `data`, as it did for the hello world function, instead of the information stored in the variable data. -14. Let's read your whole data file using a context manager `with`, in a new cell: +13. Let's read your whole data file using a context manager `with`, in a new cell: ```python # Read the data file @@ -154,7 +154,7 @@ You'll see that the cell output is executed right below each cell. Combined these two lines mean: with the datafile opened, I'd like to read it. -15. What did we just see? What is the data object? What type is data? How do we find out? +14. What did we just see? What is the data object? What type is data? How do we find out? In a new cell print the type of our data: From 8978cfa17f703994c2726cd93bc8507596f12ffd Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 31 Mar 2021 17:05:10 -0600 Subject: [PATCH 07/12] fix indent --- foundations/getting-started-python/basic-python.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md index a46dfa759..a5dcd9b27 100644 --- a/foundations/getting-started-python/basic-python.md +++ b/foundations/getting-started-python/basic-python.md @@ -86,7 +86,7 @@ On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **T print("Hello, world!") ``` -You'll see that the cell output is executed right below each cell. + You'll see that the cell output is executed right below each cell. 11. In a new cell, let's read the first 4 lines from our datafile. @@ -156,7 +156,7 @@ You'll see that the cell output is executed right below each cell. 14. What did we just see? What is the data object? What type is data? How do we find out? -In a new cell print the type of our data: + In a new cell print the type of our data: ```python print(type(data)) @@ -167,3 +167,5 @@ In a new cell print the type of our data: Python is a dynamically typed language, which means you don't have to explicitly specify the datatype when you name a variable, Python will automatically figure it out by the nature of the data. In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt file and read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and `print()`, as well as the context manager `with`. You should be familiar with the `str` datatype. + +--- From 18b06680f4fa1f359a0db4675a8508601e2f8a12 Mon Sep 17 00:00:00 2001 From: Brian Rose Date: Thu, 1 Apr 2021 15:22:05 -0400 Subject: [PATCH 08/12] Fix typos and formatting, add some links --- _toc.yml | 2 +- foundations/getting-started-python/conda.md | 10 ++++++++-- .../how-to-run-python.md | 11 ++++++----- foundations/getting-started-python/jupyter.md | 18 +++++++++++++++--- foundations/getting-started-python/terminal.md | 5 ++++- .../getting-started-python/why-python.md | 14 +++++++++----- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/_toc.yml b/_toc.yml index 6c9eebe40..eb80668cd 100644 --- a/_toc.yml +++ b/_toc.yml @@ -13,7 +13,7 @@ - file: foundations/overview - file: foundations/getting-started-python/getting-started-python sections: - - file: foundationsgetting-started-python/why-python + - file: foundations/getting-started-python/why-python sections: - file: foundations/getting-started-python/Hello - file: foundations/getting-started-python/how-to-run-python diff --git a/foundations/getting-started-python/conda.md b/foundations/getting-started-python/conda.md index 902f657ec..90b87d54f 100644 --- a/foundations/getting-started-python/conda.md +++ b/foundations/getting-started-python/conda.md @@ -10,13 +10,19 @@ Package management is useful because you may want to update a package for one of ## Installing Conda -We recommmend you install Miniconda. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). +We recommend you install Miniconda. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). Miniconda is a paired down version of Anaconda. [Installing Anaconda](https://docs.anaconda.com/anaconda/install/) takes longer and takes up more disk space, but provides you with more functionality. Anaconda comes with a Jupyter and Spyder, a Python-specific integrated development platform (IDE), installations as well as other packages. The interface of Anaconda is great if you are uncomfortable with the terminal. +_Elaborate on Miniconda vs Anaconda... Miniconda is recommended because we should install packages in isolated environments..._ + ## Creating a Conda environment -A conda environment is a directory that contains a collection of packages or libraries that you would like installed and accessible for this workflow. To create a new Conda environment, type `conda create --name` and the name of your environment in your terminal, and then specify any packages that you would like to have installed. For example, to install a Jupyter-ready environment, type `conda create --name sample_environment python jupyterlab`. +A conda environment is a directory that contains a collection of packages or libraries that you would like installed and accessible for this workflow. To create a new Conda environment, type `conda create --name` and the name of your environment in your terminal, and then specify any packages that you would like to have installed. For example, to install a Jupyter-ready environment called `sample_environment`, type + +``` +conda create --name sample_environment python jupyterlab +``` It is a good idea to create new environments for different projects because since Python is open source, new versions of the tools you use may become available. This is a way of guaranteeing that your script will use the same versions of packages and libraries and should run the same as you expect it to. diff --git a/foundations/getting-started-python/how-to-run-python.md b/foundations/getting-started-python/how-to-run-python.md index 992035de0..3924fcf3d 100644 --- a/foundations/getting-started-python/how-to-run-python.md +++ b/foundations/getting-started-python/how-to-run-python.md @@ -2,7 +2,7 @@ This section provides an overview of different ways to run Python code, and quickstart guides for -- Chosing a Python platform +- Choosing a Python platform - Installing and running Python on various local platforms - Running Python in the cloud @@ -13,16 +13,17 @@ There is no single official platform for the Python language. Here we provide a 1. The terminal, 2. Jupyter notebooks, and 3. IDEs (integrated development environment). - We highly encourage the use of Jupyter notebooks; all of our tutorials will be in a Jupyter notebook format. Here we hope to provide you with enough information to understand the differences and similarities between each platform so that you can make the best chose for your work environment and learn along effectively, regardless of your Python platform preference. -In general, it is always best to test your programs in the same environment in which they will be run. The biggest factors to consider when chosing your platform are: +We highly encourage the use of Jupyter notebooks; all of our tutorials will be in a Jupyter notebook format. Here we hope to provide you with enough information to understand the differences and similarities between each platform so that you can make the best chose for your work environment and learn along effectively, regardless of your Python platform preference. + +In general, it is always best to test your programs in the same environment in which they will be run. The biggest factors to consider when choosing your platform are: - What are you already comfortable with? - What are the people around you using (peers, coworkers, instructors, etc)? ### Terminal -For learners who are familiar with basic [Linix commands](https://cheatography.com/davechild/cheat-sheets/linux-command-line/).and text editors, learning Python in the terminal is the quickest route straight to learning Python syntax without the covering the bells and whistles of your new platform. If you are runing Python on a super computer, through an HTTP request, or ssh tunneling you might want to consider learning in the terminal. +For learners who are familiar with basic [Linux commands](https://cheatography.com/davechild/cheat-sheets/linux-command-line/).and text editors, learning Python in the terminal is the quickest route straight to learning Python syntax without the covering the bells and whistles of your new platform. If you are runing Python on a super computer, through an HTTP request, or ssh tunneling you might want to consider learning in the terminal. ### Jupyter Session @@ -30,7 +31,7 @@ Jupyter Notebooks are very popular among data scientists. It is a free, open-sou ### Other IDEs -If you already code in other languages you might already have a favorite IDE that will work just as well in Python. Spyder is a Python specific IDE that comes with the Anaconda download. It is perhaps the most familiar IDE if you are coming from languages such as Matlab that have a language specific platform and display a list of variables. PyCharm and Visual Studio Code are also popular IDEs. Many IDEs offer support for terminal execution, scripts, and Jupyter display. To learn about your specific IDE visit its official documentation. +If you already code in other languages you might already have a favorite IDE that will work just as well in Python. [Spyder](https://www.spyder-ide.org) is a Python specific IDE that comes with the [Anaconda download](https://www.anaconda.com/products/individual). It is perhaps the most familiar IDE if you are coming from languages such as [Matlab](https://www.mathworks.com/products/matlab.html) that have a language specific platform and display a list of variables. [PyCharm](https://www.jetbrains.com/pycharm/) and [Visual Studio Code](https://code.visualstudio.com) are also popular IDEs. Many IDEs offer support for terminal execution, scripts, and Jupyter display. To learn about your specific IDE visit its official documentation. --- diff --git a/foundations/getting-started-python/jupyter.md b/foundations/getting-started-python/jupyter.md index 7d392a471..8366d007a 100644 --- a/foundations/getting-started-python/jupyter.md +++ b/foundations/getting-started-python/jupyter.md @@ -11,12 +11,24 @@ To run a Jupyter session you will need to install some necessary packages into y You can install `miniconda`. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). Then create a Conda environment with Python installed. -`conda create --name pythia_foundations python jupyterlab nb_conda_kernels` + +``` +conda create --name pythia_foundations python jupyterlab nb_conda_kernels +``` Or you can install the full [Anaconda](https://www.anaconda.com/products/individual), and select Jupyter in the GUI. **have a screenshot of that here** -Test that you have installed everything correctly by activating your environment with `conda activate pythia_foundations` and running `jupyter lab`. +Test that you have installed everything correctly by first activating your environment: + +``` +conda activate pythia_foundations +jupyter lab +``` + +A new window should open automatically in your default browser. You can change the browser with (for example): -A new window should open automatically in your default browser. You can change the browser with `jupyter lab —browser=chrome`. +``` +jupyter lab —browser=chrome +``` ## Running Python in Jupyter diff --git a/foundations/getting-started-python/terminal.md b/foundations/getting-started-python/terminal.md index f5f086d53..35629a813 100644 --- a/foundations/getting-started-python/terminal.md +++ b/foundations/getting-started-python/terminal.md @@ -10,7 +10,10 @@ You'd like to learn Python in the terminal. Here we will cover: If running Python in the terminal it is best to install Miniconda. You can do that by following the [instructions for you machine](https://docs.conda.io/en/latest/miniconda.html). Then create a Conda environment with Python installed by typing the following into your terminal: -`conda create --name pythia_foundations python` + +``` +conda create --name pythia_foundations python +``` You can test that this by running `python` in the command line. diff --git a/foundations/getting-started-python/why-python.md b/foundations/getting-started-python/why-python.md index 31d1ed3e2..51d7215a1 100644 --- a/foundations/getting-started-python/why-python.md +++ b/foundations/getting-started-python/why-python.md @@ -2,16 +2,20 @@ You're already here because you want to learn to use Python for your data analysis and visualizations. Python can be compared to other high-level, interpreted, object-oriented languages, but is especially great because it is free and open source! -**High level languages:** +## High level languages + Other high level languages include MatLab, IDL, and NCL. The advantage of high level languages is that the provide functions, data structures, and other utilities that are commonly used, which means it takes less code to get real work done. The disadvantage of high level languages is that they tend to obscure the low level aspects of the machine such as: memory use, how many floating point operations are happening, and other information related to performance. C and C++ are all examples of lower level languages. The "higher" the level of language, the more computing fundamentals are abstracted. -**Interpreted languages:** -Most of your work is probably already in interpreted languages if you've ever used IDL, NCL, or MatLab (interpreted languages are typically also high level). So you are already familiar with the advantages of this: you don't have to worry about compiling or machine compatability (it is portable). And you are probably familiar with their deficiencies: sometimes they can be slower than compiled languages and potentially more memory intensive. +## Interpreted languages + +Most of your work is probably already in interpreted languages if you've ever used IDL, NCL, or MatLab (interpreted languages are typically also high level). So you are already familiar with the advantages of this: you don't have to worry about compiling or machine compatibility (it is portable). And you are probably familiar with their deficiencies: sometimes they can be slower than compiled languages and potentially more memory intensive. + +## Object Oriented languages -**Object Oriented languages:** Objects are custom datatypes. For every custom datatype, you usually have a set of operations you might want to conduct. For example, if you have an object that is a list of numbers you might want to apply a mathematical operation, such as sum, onto this list object in bulk. Not every function can be applied to every datatype; it wouldn't make sense to apply a logarithm to a string of letters or to capitalize a list of numbers. Data and the operations applied to them are grouped together into one object. -**Open source:** +## Open source + Python as a language is open source which means that there is a community of developers behind its codebase. Anyone can join the developer community and contribute to deciding the future of the language. When someone identifies gaps to Python's abilities, they can write up the code to fill these gaps. The open source nature of Python means that Python as a language is very adaptable to shifting needs of the user community. Python is a language designed for rapid prototyping and efficient programming. It is easy to write new code quickly with less typing. From f6a87555105bab49b41fa8695b9702787da015d4 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 6 Apr 2021 11:12:52 -0600 Subject: [PATCH 09/12] add data dictionary content --- .../getting-started-python/basic-python.md | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md index a5dcd9b27..1dc15c0f5 100644 --- a/foundations/getting-started-python/basic-python.md +++ b/foundations/getting-started-python/basic-python.md @@ -169,3 +169,195 @@ On Windows, open **Anaconda Prompt**. On a Mac or Linux machine, simply open **T In this section you set up a workspace by creating your directory and activating your conda environment. You downloaded a .txt file and read it using the Python commands of `open()`, `readline()`, `read()`, `close()`, and `print()`, as well as the context manager `with`. You should be familiar with the `str` datatype. --- + +## Creating a Data Dictionary + +You have just commited your new script file that reads in the data from a file as a string. You will now manipulate your data into a more usable format - a dictionary. In doing so you will learn how to write iterative for loops and about Python data structures. + +1. One big string isn't very useful, so use :python:`str.split()` to parse the data file into a data structure you can use. + + With your `mysci.ipynb` Jupyter notebook open and :code:`python_tutorial` environment activated, add a new code cell: + + ```python + # Initialize my data variable + data = [] + + # Read and parse the data file + filename = "data/wxobs20170821.txt" + with open(filename, 'r') as datafile: + + # Read the first three lines (header) + for _ in range(3): + datafile.readline() + + # Read and parse the rest of the file + for line in datafile: + datum = line.split() + data.append(datum) + + # DEBUG + for datum in data: + print(datum) + ``` + + The first thing that is different in this code block is an initialized data variable; `data = []` creates the variable data as an empty `list` which we will populate as we read the file. Python `list` objects are a collection data type that contain ordered and changeable - meaning you can call information out of the `list` by its index and you can add or delete elements to your `list`. Lists are denoted by square brackets, `[]`. + + Then with the datafile open for reading capabilities, we are going to write two separate `for` loops. A `for` loop is used for iterating over a sequence (such as a list). It is important to note the syntax of Python `for` loops: the `:` at the end of the `for` line, the tab-indentation of all lines within the `for` loop, and perhaps the absence of an `end for` that is found in languages such as Matlab. + + In your first `for` loop, loop through the dummy variable `_` in `range(3)`. The `range` function returns a sequence of numbers, starting at 0 and incrementing by 1 (by default), ending at the specified length. Here if you were to `print(_)` on each line of the for loop you would see: + + ``` + 0 + 1 + 2 + ``` + + Try it out in a new cell, if you are unsure of how this works. Here the `_` variable is a placeholder, meaning the variable is never called within the loop. + + So again, in the first `for` loop, you execute the `readline` command (which you will remember moves down to the next line each time it is consecutively called) 3 times to read through the file header (which is 3 lines long). + + **Yay!** You have just written your first `for` loop! + + Then in a second `for` loop, you loop through lines in the remainder of your datafile. On each line, split it along white space. The `string.split()` method splits a string into a list on a specified separator, the default being white space. You could use any character you like, but other useful options are `/t` for splitting along tabs or `,` along commas. + + Then you `append` this split line list to the end of your data `list`. The `list.append()` method adds a single item to the end of your `list`. After every line in your `for` loop iteration, the data `list` that was empty is one element longer. Now we have a `list` of `list`\s for our data variable - a `list` of the data in each line for multiple lines. + + When you print each datum in data, you'll see that each datum is a `list` of `string` values. + + We just covered a lot of Python nuances in a very little bit a code! + +2. Now, to practice list indexing, get the first, 10th, and last row in data in a new code cell: + + ``` + print(data[0]) + print(data[9]) + print(data[-1]) + ``` + + Index your list by adding the number of your index in square brackets, `[]`, after the name of the `list`. Python is 0-indexed so `data[0]` refers to the first index and `[-1]` refers to the last index. + +3. Now, to practice slice indexing, get the first 10 rows in data in a new code cell: + + ``` + for datum in data[0:10]: + print(datum) + ``` + + Using a colon, `:`, between two index integers `a` and `b`, you get all indexes between `a` and `b`. See what happens when you print `data[:10]`, `data[0:10:2]`, and `data[slice(0,10,2)]`. What's the difference? + +4. Now, to practice nested indexing, get the 5th, the first 5, and every other column of row 9 in the data object. + + ``` + print(data[8][4]) + print(data[8][:5]) + print(data[8][::2]) + ``` + + In nested `list` indexing, the first index determines the row, and the second determines the element from that row. Also try printing `data[5:8][4]`, why doesn't this work? + +5. Can you remember which column is which? Is time the first column or the second? Which column is the temperature? + + Each column is a time-series of data. We would ideally like each time-series easily accessible, which is not the case when data is row-column ordered (like it currently is). (Remember what happens when you try to do something like `data[:][4]`!) + + Let's get our data into a more convenient named-column format. + + ``` + # Initialize my data variable + data = {'date': [], + 'time': [], + 'tempout': []} + + # Read and parse the data file + filename = "data/wxobs20170821.txt" + with open(filename, 'r') as datafile: + + # Read the first three lines (header) + for _ in range(3): + datafile.readline() + + # Read and parse the rest of the file + for line in datafile: + split_line = line.split() + data['date'].append(split_line[0]) + data['time'].append(split_line[1]) + data['tempout'].append(split_line[2]) + + # DEBUG + print(data['time']) + ``` + + First we'll initialize a dictionary, `dict`, indicated by the curly brackets, `{}`. Dictionaries, like `list`\s, are changeable, but they are unordered. They have keys, rather than positions, to point to their elements. Here you have created 3 elements of your dictionary, all currently empty `list`\s, and specified by the keys `date`, `time`, and `tempout`. Keys act similarly to indexes: to pull out the `tempout` element from data you would type `data['tempout']`. + + Grab date (the first column of each line), time (the second column of each line), and temperature data (the third column), from each line and `append` it to the `list` associated with each of these data variables. + +6. Now it's easy to get the time-series information for each column that we are interested in grabbing, and we can get each column by name. However, everything read from the text file is a `str`. What if we want to do math on this data, then we need it to be a different data type! + + So, let's convert the tempout time-series to be a `float` by changing the line: + + ``` + data['tempout'].append(split_line[2]) + ``` + + to: + + ``` + data['tempout'].append(float(split_line[2])) + ``` + + The `float` datatype refers to floating point real values - the datatype of any numbers with values after a decimal point. You could also change the datatype to `int`, which will round the values down to the closest full integer. + +7. `print(data['tempout'])` + +Do you see a difference? It should now be a list of floats. + +8. This seems great, so far! But what if you want to read more columns to ourdata later? You would have to change the initialization of the data variable (at the top of `mysci.py`\) and have to add the appropriate line in the "read and parse" section. Essentially, that means you need to maintain 2 parts of the code and make sure that both remain consistent with each other. + + This is generally not good practice. Ideally, you want to be able to change only one part of the code and know that the rest of the code will remain consistent. So, let's fix this. + + ``` + # Column names and column indices to read + columns = {'date': 0, 'time': 1, 'tempout': 2} + + # Data types for each column (only if non-string) + types = {'tempout': float} + + # Initialize my data variable + data = {} + for column in columns: + data[column] = [] + + # Read and parse the data file + filename = "data/wxobs20170821.txt" + with open(filename, 'r') as datafile: + + # Read the first three lines (header) + for _ in range(3): + datafile.readline() + + # Read and parse the rest of the file + for line in datafile: + split_line = line.split() + for column in columns: + i = columns[column] + t = types.get(column, str) + value = t(split_line[i]) + data[column].append(value) + + # DEBUG + print(data['tempout']) + + ``` + + You have now created a columns `dict` that points each data variable to its column-index. And a types `dict`, that indicates what type to convert the data when necessary. When you want new variables pulled out of the datafile, change these two variables. + + Initializing the data `dict` now includes a `for` loop, where for each variable specified in columns, that key is initialized pointing to an empty `list`. This is the first time you have looped over a `dict` and added key-value pairs to a `dict` via assignment. + + When reading and parsing the file, you created your first nested `for` loop. For every line of the datafile, split that line - and then for every desired variable in the columns `dict` (date, time, tempout): grab the datum from the current split line with the specified index (0, 1, 2), use the `dict.get()` method to find the desired datatype if specified (avoiding `key-not-found` errors and defaulting to `str` if unspecified), convert the datum to the desired datatype, and `append` the datum to the `list` associated with each column key within the data `dict`. + +--- + +That concludes the second lesson of this virtual tutorial. + +In this section you saved the variables of date, time, and tempout in a data dictionary. + +You should now be familiar with the data structures `list`\s (as well as list indexing, nested lists, and the command `list.append()`), `dict`\s (their keys and the command `dict.get()`), and `range`\s. You also learned to write `for` loops, about the `float` datatype, and using the Python commands `str.split()`. From 686d61349c9683df6beaeeda37c31762306004c5 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 6 Apr 2021 11:18:46 -0600 Subject: [PATCH 10/12] indent --- foundations/getting-started-python/basic-python.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md index 1dc15c0f5..2693bc04e 100644 --- a/foundations/getting-started-python/basic-python.md +++ b/foundations/getting-started-python/basic-python.md @@ -308,7 +308,7 @@ You have just commited your new script file that reads in the data from a file a 7. `print(data['tempout'])` -Do you see a difference? It should now be a list of floats. + Do you see a difference? It should now be a list of floats. 8. This seems great, so far! But what if you want to read more columns to ourdata later? You would have to change the initialization of the data variable (at the top of `mysci.py`\) and have to add the appropriate line in the "read and parse" section. Essentially, that means you need to maintain 2 parts of the code and make sure that both remain consistent with each other. From 5e323d4215a65efadd52aba002ffd74b1ae430bb Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 7 Apr 2021 09:22:34 -0600 Subject: [PATCH 11/12] add first function content --- .../getting-started-python/basic-python.md | 128 +++++++++++++++++- 1 file changed, 126 insertions(+), 2 deletions(-) diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md index 2693bc04e..19a048cff 100644 --- a/foundations/getting-started-python/basic-python.md +++ b/foundations/getting-started-python/basic-python.md @@ -356,8 +356,132 @@ You have just commited your new script file that reads in the data from a file a --- -That concludes the second lesson of this virtual tutorial. - In this section you saved the variables of date, time, and tempout in a data dictionary. You should now be familiar with the data structures `list`\s (as well as list indexing, nested lists, and the command `list.append()`), `dict`\s (their keys and the command `dict.get()`), and `range`\s. You also learned to write `for` loops, about the `float` datatype, and using the Python commands `str.split()`. + +## Writing Functions + +You have just commited your new script that reads the file, saving the variables of date, time, and tempout in a data dictionary. In this section you will compute wind chill index by writing your first function and learning about basic math operators. + +1. Now that you've read the data in a way that is easy to modify later, it is time to actually do something with the data. + + Compute the wind chill factor, which is the cooling effect of the wind. As wind speed increases the rate at which a body loses heat increases. The formula for this is: + + .. math:: + + WCI = a + (b _ t) - (c _ v^{0.16}) + (d _ t _ v^{0.16}) + + .. + + Where _WCI_ refers to the Wind Chill in degrees F, _t_ is temperature in degrees F, _v_ is wind speed in mph, and the other variables are as follows: _a_ = 35.74, _b_ = 0.6215, _c_ = 35.75, and _d_ = 0.4275. Wind Chill Index is only defined for temperatures within the range -45 to +45 degrees F. + + You've read the temperature data into the tempout variable, but to do this calculation, you also need to read the windspeed variable from column 7. + + With your terminal open and :code:`python_tutorial` environment activated, modify columns variable in :code:`mysci.py` to read: + + ``` + # Column names and column indices to read + columns = {'date': 0, 'time': 1, 'tempout': 2, 'windspeed': 7} + ``` + + and modify the types variable to be: + + ``` + # Data types for each column (only if non-string) + types = {'tempout': float, 'windspeed': float} + ``` + +2. Now, let's write our first function to compute the wind chill factor. We'll add this function to the bottom of the file. + + ``` + # Compute the wind chill temperature + def compute_windchill(t, v): + a = 35.74 + b = 0.6215 + c = 35.75 + d = 0.4275 + + v2 = v ** 2 + wci = a + (b * t) - (c * v2) + (d * t * v2) + return wci + ``` + + To indicate a function in python you type `def` for define, the name of your function, and then in parenthesis the input arguments of that function, followed by a colon. The preceding lines,the code of your function, are all tab-indented. If necessary specify your return value. + + Here is your first introduction to math operators in Python. Addition, subtraction, and multiplication look much like you'd expect. A double astericks, `**`, indicates an exponential. A backslash, `/`, is for division, and a double backslash, `//`, is for integer division. + + And then let's compute a new list with windchill data at the bottom of `mysci.ipynb`\: + + ``` + # Compute the wind chill factor + windchill = [] + for temp, windspeed in zip(data['tempout'], data['windspeed']): + windchill.append(compute_windchill(temp, windspeed)) + ``` + + Now we'll call our function. Initialize a `list` for wind chill with empty square brackets, `[]`. And in a `for` loop, loop through our temperature and wind speed data, applying the function to each `tuple` data pair. `tuple`\s are ordered like `list`\s, but they are indicated by parenthesis, `()`, instead of square brackets and cannot be changed or appended. `tuple`\s are generally faster than `list`\s. + + We use the `zip` function in Python to automatically unravel the `tuple`\s. Take a look at `zip([1,2], [3,4,5])`. What is the result? + + And finally, add a DEBUG section to see the results: + + ``` + # DEBUG + print(windchill) + ``` + +3. Now, the wind chill factor is actually in the datafile, so we can read it from the file and compare that value to our computed values. To do this, we need to read the windchill from column 12 as a `float`: + + Edit the columns and types `dict`: + + ``` + # Column names and column indices to read + columns = {'date': 0, 'time': 1, 'tempout': 2, 'windspeed': 7, 'windchill': 12} + ``` + + and + + ``` + # Data types for each column (only if non-string) + types = {'tempout': float, 'windspeed': float, 'windchill': float} + ``` + + Then, in a DEBUG section at the end of your script, compare the twomdifferent values (one from data and one computed by our function): + + ``` + # DEBUG + for wc_data, wc_comp in zip(data['windchill'], windchill): + print(f'{wc_data:.5f} {wc_comp:.5f} {wc_data - wc_comp:.5f}') + ``` + + Using an `f-string` with float formatting you can determine the precision to which to print the values. The `.5f` means you want 5 places after the decimal point. + + Test the results. What do you see? + +4. Now, format the output so that it's easy to understand and rename this script to something indicative of what it actually does. + + In a new cell, add: + + ``` + # Output comparison of data + print(' ORIGINAL COMPUTED') + print(' DATE TIME WINDCHILL WINDCHILL DIFFERENCE') + print('------- ------ --------- --------- ----------') + zip_data = zip(data['date'], data['time'], data['windchill'], windchill) + for date, time, wc_orig, wc_comp in zip_data: + wc_diff = wc_orig - wc_comp + print(f'{date} {time:>6} {wc_orig:9.6f} {wc_comp:9.6f} {wc_diff:10.6f}') + ``` + + Here you used f-string formatting with more f-string formatting options. The `>6` indicates that you'd like the characters of the string to be right-justified and to take up 6 spaces. + + The `9f` specifies that you want the value to fill 9 spaces, so `9.6f` indicates you'd like the value to fill 9 spaces with 6 of them being after the decimal point. Same concept for `10.6f`. + + You now have your first complete Python script! + +--- + +That concludes the "First Python Script" virtual tutorial where you learned to write your first Python script. + +In this section you calculated wind chill index by writing and calling your first function. You also learned about Python math operators, the `zip()` command, `tuple` datastructure, f-string formatting, and how to push your repository to GitHub. From 0e0ecad8816981c633c3e79c045c69f6d6d79225 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 13 Apr 2021 12:50:09 -0600 Subject: [PATCH 12/12] change ` to ``` --- foundations/getting-started-python/basic-python.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/foundations/getting-started-python/basic-python.md b/foundations/getting-started-python/basic-python.md index 19a048cff..aa3a1e13a 100644 --- a/foundations/getting-started-python/basic-python.md +++ b/foundations/getting-started-python/basic-python.md @@ -174,9 +174,9 @@ In this section you set up a workspace by creating your directory and activating You have just commited your new script file that reads in the data from a file as a string. You will now manipulate your data into a more usable format - a dictionary. In doing so you will learn how to write iterative for loops and about Python data structures. -1. One big string isn't very useful, so use :python:`str.split()` to parse the data file into a data structure you can use. +1. One big string isn't very useful, so use `str.split()` to parse the data file into a data structure you can use. - With your `mysci.ipynb` Jupyter notebook open and :code:`python_tutorial` environment activated, add a new code cell: + With your `mysci.ipynb` Jupyter notebook open and `python_tutorial` environment activated, add a new code cell: ```python # Initialize my data variable @@ -368,11 +368,7 @@ You have just commited your new script that reads the file, saving the variables Compute the wind chill factor, which is the cooling effect of the wind. As wind speed increases the rate at which a body loses heat increases. The formula for this is: - .. math:: - - WCI = a + (b _ t) - (c _ v^{0.16}) + (d _ t _ v^{0.16}) - - .. + $$WCI = a + (b t) - (c v^{0.16}) + (d t v^{0.16})$$ Where _WCI_ refers to the Wind Chill in degrees F, _t_ is temperature in degrees F, _v_ is wind speed in mph, and the other variables are as follows: _a_ = 35.74, _b_ = 0.6215, _c_ = 35.75, and _d_ = 0.4275. Wind Chill Index is only defined for temperatures within the range -45 to +45 degrees F.