Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intro to os module #559

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 0 additions & 239 deletions _episodes/01-run-quit.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ objectives:
- "Create a Jupyter notebook."
- "Shutdown the JupyterLab server."
- "Understand the difference between a Python script and a Jupyter notebook."
- "Create Markdown cells in a notebook."
- "Create and run Python cells in a notebook."
keypoints:
- "Python scripts are plain text files."
- "Use the Jupyter Notebook for editing and running Python."
- "The Notebook has Command and Edit modes."
- "Use the keyboard and mouse to select and edit cells."
- "The Notebook will turn Markdown into pretty-printed documentation."
- "Markdown does most of what HTML does."
---

Many software developers will often use an integrated development environment (IDE) or a
Expand Down Expand Up @@ -293,185 +290,6 @@ more details, then see the [official notebook documentation][jupyterlab-notebook
* Notice that the <kbd>Return</kbd> and <kbd>Shift</kbd> keys on the right of the keyboard are
right next to each other.

### The Notebook will turn Markdown into pretty-printed documentation.

* Notebooks can also render [Markdown][markdown].
* A simple plain-text format for writing lists, links,
and other things that might go into a web page.
* Equivalently, a subset of HTML that looks like what you'd send in an old-fashioned email.
* Turn the current cell into a Markdown cell by entering the Command mode (<kbd>Esc</kbd>/gray)
and press the <kbd>M</kbd> key.
* `In [ ]:` will disappear to show it is no longer a code cell and you will be able to write in
Markdown.
* Turn the current cell into a Code cell by entering the Command mode (<kbd>Esc</kbd>/gray) and
press the <kbd>y</kbd> key.

### Markdown does most of what HTML does.

<div class="row">
<div class="col-md-6" markdown="1">

~~~
* Use asterisks
* to create
* bullet lists.
~~~

</div>
<div class="col-md-6" markdown="1">

* Use asterisks
* to create
* bullet lists.

</div>
</div>

<div class="row">
<div class="col-md-6" markdown="1">

~~~
1. Use numbers
1. to create
1. numbered lists.
~~~

</div>
<div class="col-md-6" markdown="1">

1. Use numbers
1. to create
1. numbered lists.

</div>
</div>

<div class="row">
<div class="col-md-6" markdown="1">

~~~
* You can use indents
* To create sublists
* of the same type
* Or sublists
1. Of different
1. types
~~~

</div>
<div class="col-md-6" markdown="1">

* You can use indents
* To create sublists
* of the same type
* Or sublists
1. Of different
1. types

</div>
</div>

<div class="row">
<div class="col-md-6" markdown="1">

~~~
# A Level-1 Heading
~~~

</div>
<div class="col-md-6" markdown="1">

# A Level-1 Heading

</div>
</div>

<div class="row">
<div class="col-md-6" markdown="1">

~~~
## A Level-2 Heading (etc.)
~~~

</div>
<div class="col-md-6" markdown="1">

## A Level-2 Heading (etc.)

</div>
</div>

<div class="row">
<div class="col-md-6" markdown="1">

~~~
Line breaks
don't matter.

But blank lines
create new paragraphs.
~~~

</div>
<div class="col-md-6" markdown="1">

Line breaks
don't matter.

But blank lines
create new paragraphs.

</div>
</div>

<div class="row">
<div class="col-md-6" markdown="1">

~~~
[Create links](http://software-carpentry.org) with `[...](...)`.
Or use [named links][data_carpentry].

[data_carpentry]: http://datacarpentry.org
~~~

</div>
<div class="col-md-6" markdown="1">

[Create links](http://software-carpentry.org) with `[...](...)`.
Or use [named links][data_carpentry].

[data_carpentry]: http://datacarpentry.org

</div>
</div>

> ## Creating Lists in Markdown
>
> Create a nested list in a Markdown cell in a notebook that looks like this:
>
> 1. Get funding.
> 2. Do work.
> * Design experiment.
> * Collect data.
> * Analyze.
> 3. Write up.
> 4. Publish.
>
> > ## Solution
> >
> > This challenge integrates both the numbered list and bullet list.
> > Note that the bullet list is indented 2 spaces so that it is inline with the items of the numbered list.
> > ~~~
> > 1. Get funding.
> > 2. Do work.
> > * Design experiment.
> > * Collect data.
> > * Analyze.
> > 3. Write up.
> > 4. Publish.
> > ~~~
> {: .solution}
{: .challenge}

> ## More Math
>
Expand All @@ -495,62 +313,6 @@ Or use [named links][data_carpentry].
> {: .solution}
{: .challenge}

> ## Change an Existing Cell from Code to Markdown
>
> What happens if you write some Python in a code cell
> and then you switch it to a Markdown cell?
> For example,
> put the following in a code cell:
>
> ~~~
> x = 6 * 7 + 12
> print(x)
> ~~~
> {: .language-python}
>
> And then run it with <kbd>Shift</kbd>+<kbd>Return</kbd> to be sure that it works as a code cell.
> Now go back to the cell and use <kbd>Esc</kbd> then <kbd>m</kbd> to switch the cell to Markdown
> and "run" it with <kbd>Shift</kbd>+<kbd>Return</kbd>.
> What happened and how might this be useful?
>
> > ## Solution
> >
> > The Python code gets treated like Markdown text.
> > The lines appear as if they are part of one contiguous paragraph.
> > This could be useful to temporarily turn on and off cells in notebooks that get used for multiple purposes.
> > ~~~
> > x = 6 * 7 + 12 print(x)
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}

> ## Equations
>
> Standard Markdown (such as we're using for these notes) won't render equations,
> but the Notebook will.
> Create a new Markdown cell
> and enter the following:
>
> ~~~
> $\sum_{i=1}^{N} 2^{-i} \approx 1$
> ~~~
>
> (It's probably easier to copy and paste.)
> What does it display?
> What do you think the underscore, `_`, circumflex, `^`, and dollar sign, `$`, do?
>
> > ## Solution
> >
> > The notebook shows the equation as it would be rendered from LaTeX equation syntax.
> > The dollar sign, `$`, is used to tell Markdown that the text in between is a LaTeX equation.
> > If you're not familiar with LaTeX, underscore, `_`, is used for subscripts and circumflex, `^`, is used for superscripts.
> > A pair of curly braces, `{` and `}`, is used to group text together so that the statement `i=1` becomes the subscript and `N` becomes the superscript.
> > Similarly, `-i` is in curly braces to make the whole statement the superscript for `2`.
> > `\sum` and `\approx` are LaTeX commands for "sum over" and "approximate" symbols.
> {: .solution}
{: .challenge}

## Closing JupyterLab

* From the Menu Bar select the "File" menu and the choose "Quit" at the bottom of the dropdown menu. You will be prompted to confirm that you wish to shutdown the JupyterLab server (don't forget to save your work!). Click "Confirm" to shutdown the JupyterLab server.
Expand All @@ -567,6 +329,5 @@ $ jupyter lab
[anaconda]: https://docs.continuum.io/anaconda/install
[jupyterlab-ui]: https://jupyterlab.readthedocs.io/en/stable/user/interface.html
[jupyterlab-notebook-docs]: https://jupyterlab.readthedocs.io/en/stable/user/notebook.html
[markdown]: https://en.wikipedia.org/wiki/Markdown

{% include links.md %}
98 changes: 98 additions & 0 deletions _episodes/06-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ objectives:
- "Explain what software libraries are and why programmers create and use them."
- "Write programs that import and use modules from Python's standard library."
- "Find and read documentation for the standard library interactively (in the interpreter) and online."
- "Print the current working directory and subdirectories and list their contents."
keypoints:
- "Most of the power of a programming language is in its libraries."
- "A program must import a library module in order to use it."
- "Use `help` to learn about the contents of a library module."
- "Import specific items from a library to shorten programs."
- "Create an alias for a library when importing it to shorten programs."
- "A Python script / JupyterLab notebook has a working directory."
- "The script / notebook will look for files in the working directory using a relative path."
---
## Most of the power of a programming language is in its libraries.

Expand Down Expand Up @@ -395,6 +398,101 @@ cos(pi) is -1.0
> {: .solution}
{: .challenge}

## Access the file system with the `os` library.

* The `os` library is part of the Python standard library. It contains
functions for working with files and directories that are consistent across
systems, regardless of whether you're using Mac, Linux, or Windows.
* Import the `os` library.
* Use the `getcwd` method from the `os` library to print your current working
directory. This is similar to `pwd` in Bash.

~~~
import os

os.getcwd()
~~~
{: .language-python}
~~~
/Users/nelle
~~~
{: .output}


* Use the `listdir` method to list the files and directories in your current
working directory. This is similar to `ls` in Bash.
* Notice that by default Python will list all files, including 'hidden'
files and directories (starting with `.`), whereas `ls` in Bash does
not.
* Use `listdir` without any input to list the contents of the
current working directory, or provide the path to another directory
to list the contents of that directory.


~~~
os.listdir()
~~~
{: .language-python}
~~~
['.bash_history',
'.bash_profile',
'Applications',
'Documents',
'Library',
'Music',
'Public',
'Desktop',
'Downloads',
'Movies',
'Pictures',
'data',
'python-novice-gapminder-data.zip']
~~~
{: .output}


* You should see the "python-novice-gapminder-data.zip" file that you
previously downloaded and the "data" directory, if you already
unzipped that file.
* We can check to see that all of the data files are there by using
`listdir` with the "data" directory as input.

~~~
os.listdir('data')
~~~
{: .language-python}
~~~
['gapminder_gdp_americas.csv',
'gapminder_gdp_europe.csv',
'gapminder_all.csv',
'gapminder_gdp_oceania.csv',
'gapminder_gdp_africa.csv',
'gapminder_gdp_asia.csv']
~~~
{: .output}


* Use the `chdir` method to change your current working directory. This
function requires a path as input.
* Note that you must use proper path delimiters for your system (e.g.,
forward-slash `/` for macOS and Linux, reverse-slash `\` for Windows).
* The path must be either (1) relative to your current working
directory or (2) be a full path (starting from the root
directory `/`).
* If successful, `os.listdir` does not print anything, but if it is not
successful, it will print an error.

~~~
os.chdir('/Users/nelle/Desktop')
os.listdir()
~~~
{: .language-python}
~~~
['shell-lesson-data']
~~~
{: .output}


[pypi]: https://pypi.python.org/pypi/
[stdlib]: https://docs.python.org/3/library/
[randommod]: https://docs.python.org/3/library/random.html
Expand Down