Unverified Commit 4f03b177 authored by Maxim Belkin's avatar Maxim Belkin Committed by GitHub

Merge: split the first episode (#737)

parents df777fd3 c048f568
......@@ -28,16 +28,17 @@ This lesson is also available in [R][R] and [MATLAB][MATLAB].
| # | Episode | Time | Question(s) |
|--:|:---------|:----:|:------------|
| 1 | [Analyzing Patient Data][episode01] | 90 | How can I process tabular data files in Python? |
| 2 | [Repeating Actions with Loops][episode02] | 30 | How can I do the same operations on many different values? |
| 3 | [Storing Multiple Values in Lists][episode03] | 30 | How can I store many values together? |
| 4 | [Analyzing Data from Multiple Files][episode04] | 20 | How can I do the same operations on many different files? |
| 5 | [Making Choices][episode05] | 30 | How can my programs do different things based on data values? |
| 6 | [Creating Functions][episode06] | 30 | How can I define new functions?<br>What’s the difference between defining and calling a function?<br>What happens when I call a function? |
| 7 | [Errors and Exceptions][episode07] | 30 | How does Python report errors?<br>How can I handle errors in Python programs? |
| 8 | [Defensive Programming][episode08] | 30 | How can I make my programs more reliable? |
| 9 | [Debugging][episode09] | 30 | How can I debug my program? |
|10 | [Command-Line Programs][episode10] | 30 | How can I write Python programs that will work like Unix command-line tools? |
| 1 | [Python Fundamentals][episode01] | 30 | What basic data types can I work with in Python?<br>How can I create a new variable in Python?<br>Can I change the value associated with a variable after I create it? |
| 2 | [Analyzing Patient Data][episode02] | 90 | How can I process tabular data files in Python? |
| 3 | [Repeating Actions with Loops][episode03] | 30 | How can I do the same operations on many different values? |
| 4 | [Storing Multiple Values in Lists][episode04] | 30 | How can I store many values together? |
| 5 | [Analyzing Data from Multiple Files][episode05] | 20 | How can I do the same operations on many different files? |
| 6 | [Making Choices][episode06] | 30 | How can my programs do different things based on data values? |
| 7 | [Creating Functions][episode07] | 30 | How can I define new functions?<br>What’s the difference between defining and calling a function?<br>What happens when I call a function? |
| 8 | [Errors and Exceptions][episode08] | 30 | How does Python report errors?<br>How can I handle errors in Python programs? |
| 9 | [Defensive Programming][episode09] | 30 | How can I make my programs more reliable? |
|10 | [Debugging][episode10] | 30 | How can I debug my program? |
|11 | [Command-Line Programs][episode11] | 30 | How can I write Python programs that will work like Unix command-line tools? |
## Contributing
......@@ -91,13 +92,14 @@ To cite this lesson, please consult with [CITATION](CITATION)
[slack_channel_url]: https://swcarpentry.slack.com/messages/C9Y0L6MF0
[travis_svg]: https://travis-ci.org/swcarpentry/python-novice-inflammation.svg?branch=gh-pages
[travis_url]: https://travis-ci.org/swcarpentry/python-novice-inflammation
[episode01]: https://swcarpentry.github.io/python-novice-inflammation/01-numpy/index.html
[episode02]: https://swcarpentry.github.io/python-novice-inflammation/02-loop/index.html
[episode03]: https://swcarpentry.github.io/python-novice-inflammation/03-lists/index.html
[episode04]: https://swcarpentry.github.io/python-novice-inflammation/04-files/index.html
[episode05]: https://swcarpentry.github.io/python-novice-inflammation/05-cond/index.html
[episode06]: https://swcarpentry.github.io/python-novice-inflammation/06-func/index.html
[episode07]: https://swcarpentry.github.io/python-novice-inflammation/07-errors/index.html
[episode08]: https://swcarpentry.github.io/python-novice-inflammation/08-defensive/index.html
[episode09]: https://swcarpentry.github.io/python-novice-inflammation/09-debugging/index.html
[episode10]: https://swcarpentry.github.io/python-novice-inflammation/10-cmdline/index.html
[episode01]: https://swcarpentry.github.io/python-novice-inflammation/01-intro/index.html
[episode02]: https://swcarpentry.github.io/python-novice-inflammation/02-numpy/index.html
[episode03]: https://swcarpentry.github.io/python-novice-inflammation/03-loop/index.html
[episode04]: https://swcarpentry.github.io/python-novice-inflammation/04-lists/index.html
[episode05]: https://swcarpentry.github.io/python-novice-inflammation/05-files/index.html
[episode06]: https://swcarpentry.github.io/python-novice-inflammation/06-cond/index.html
[episode07]: https://swcarpentry.github.io/python-novice-inflammation/07-func/index.html
[episode08]: https://swcarpentry.github.io/python-novice-inflammation/08-errors/index.html
[episode09]: https://swcarpentry.github.io/python-novice-inflammation/09-defensive/index.html
[episode10]: https://swcarpentry.github.io/python-novice-inflammation/10-debugging/index.html
[episode11]: https://swcarpentry.github.io/python-novice-inflammation/11-cmdline/index.html
---
title: Python Fundamentals
teaching: 20
exercises: 10
questions:
- "What basic data types can I work with in Python?"
- "How can I create a new variable in Python?"
- "Can I change the value associated with a variable after I create it?"
objectives:
- "Assign values to variables."
keypoints:
- "Basic data types in Python include integers, strings, and floating-point numbers."
- "Use `variable = value` to assign a value to a variable in order to record it in memory."
- "Variables are created on demand whenever a value is assigned to them."
- "Use `print(something)` to display the value of `something`."
---
## Variables
Any Python interpreter can be used as a calculator:
~~~
3 + 5 * 4
~~~
{: .language-python}
~~~
23
~~~
{: .output}
This is great but not very interesting.
To do anything useful with data, we need to assign its value to a _variable_.
In Python, we can [assign]({{ page.root }}/reference/#assign) a value to a
[variable]({{ page.root }}/reference/#variable), using the equals sign `=`.
For example, to assign value `60` to a variable `weight_kg`, we would execute:
~~~
weight_kg = 60
~~~
{: .language-python}
From now on, whenever we use `weight_kg`, Python will substitute the value we assigned to
it. In layman's terms, **a variable is a name for a value**.
In Python, variable names:
- can include letters, digits, and underscores
- cannot start with a digit
- are [case sensitive]({{ page.root }}/reference/#case-sensitive).
This means that, for example:
- `weight0` is a valid variable name, whereas `0weight` is not
- `weight` and `Weight` are different variables
## Types of data
Python knows various types of data. Three common ones are:
* integer numbers
* floating point numbers, and
* strings.
In the example above, variable `weight_kg` has an integer value of `60`.
To create a variable with a floating point value, we can execute:
~~~
weight_kg = 60.0
~~~
{: .language-python}
And to create a string, we add single or double quotes around some text, for example:
~~~
weight_kg_text = 'weight in kilograms:'
~~~
{: .language-python}
## Using Variables in Python
To display the value of a variable to the screen in Python, we can use the `print` function:
~~~
print(weight_kg)
~~~
{: .language-python}
~~~
60.0
~~~
{: .output}
We can display multiple things at once using only one `print` command:
~~~
print(weight_kg_text, weight_kg)
~~~
{: .language-python}
~~~
weight in kilograms: 60.0
~~~
{: .output}
Moreover, we can do arithmetic with variables right inside the `print` function:
~~~
print('weight in pounds:', 2.2 * weight_kg)
~~~
{: .language-python}
~~~
weight in pounds: 132.0
~~~
{: .output}
The above command, however, did not change the value of `weight_kg`:
~~~
print(weight_kg)
~~~
{: .language-python}
~~~
60.0
~~~
{: .output}
To change the value of the `weight_kg` variable, we have to
**assign** `weight_kg` a new value using the equals `=` sign:
~~~
weight_kg = 65.0
print('weight in kilograms is now:', weight_kg)
~~~
{: .language-python}
~~~
weight in kilograms is now: 65.0
~~~
{: .output}
> ## Variables as Sticky Notes
>
> A variable is analogous to a sticky note with a name written on it:
> assigning a value to a variable is like putting that sticky note on a particular value.
>
> ![Variables as Sticky Notes](../fig/python-sticky-note-variables-01.svg)
>
> This means that assigning a value to one variable does **not** change
> values of other variables.
> For example, let's store the subject's weight in pounds in its own variable:
>
> ~~~
> # There are 2.2 pounds per kilogram
> weight_lb = 2.2 * weight_kg
> print(weight_kg_text, weight_kg, 'and in pounds:', weight_lb)
> ~~~
> {: .language-python}
>
> ~~~
> weight in kilograms: 65.0 and in pounds: 143.0
> ~~~
> {: .output}
>
> ![Creating Another Variable](../fig/python-sticky-note-variables-02.svg)
>
> Let's now change `weight_kg`:
>
> ~~~
> weight_kg = 100.0
> print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)
> ~~~
> {: .language-python}
>
> ~~~
> weight in kilograms is now: 100.0 and weight in pounds is still: 143.0
> ~~~
> {: .output}
>
> ![Updating a Variable](../fig/python-sticky-note-variables-03.svg)
>
> Since `weight_lb` doesn't "remember" where its value comes from,
> it is not updated when we change `weight_kg`.
{: .callout}
> ## Check Your Understanding
>
> What values do the variables `mass` and `age` have after each statement in the following program?
> Test your answers by executing the commands.
>
> ~~~
> mass = 47.5
> age = 122
> mass = mass * 2.0
> age = age - 20
> print(mass, age)
> ~~~
> {: .language-python}
>
> > ## Solution
> > ~~~
> > 95.0 102
> > ~~~
> > {: .output}
> {: .solution}
{: .challenge}
> ## Sorting Out References
>
> What does the following program print out?
>
> ~~~
> first, second = 'Grace', 'Hopper'
> third, fourth = second, first
> print(third, fourth)
> ~~~
> {: .language-python}
>
> > ## Solution
> > ~~~
> > Hopper Grace
> > ~~~
> > {: .output}
> {: .solution}
{: .challenge}
{% include links.md %}
......@@ -8,16 +8,12 @@ objectives:
- "Explain what a library is and what libraries are used for."
- "Import a Python library and use the functions it contains."
- "Read tabular data from a file into a program."
- "Assign values to variables."
- "Select individual values and subsections from data."
- "Perform operations on arrays of data."
- "Plot simple graphs from data."
keypoints:
- "Import a library into a program using `import libraryname`."
- "Use the `numpy` library to work with arrays in Python."
- "Use `variable = value` to assign a value to a variable in order to record it in memory."
- "Variables are created on demand whenever a value is assigned to them."
- "Use `print(something)` to display the value of `something`."
- "The expression `array.shape` gives the shape of an array."
- "Use `array[x, y]` to select a single element from a 2D array."
- "Array indices start at 0, not 1."
......@@ -29,173 +25,6 @@ keypoints:
- "Use the `pyplot` library from `matplotlib` for creating simple visualizations."
---
In this episode we will learn how to work with arthritis inflammation datasets in Python. However,
before we discuss how to deal with many data points, let's learn how to work with
single data values.
## Variables
Any Python interpreter can be used as a calculator:
~~~
3 + 5 * 4
~~~
{: .language-python}
~~~
23
~~~
{: .output}
This is great but not very interesting.
To do anything useful with data, we need to assign its value to a _variable_.
In Python, we can [assign]({{ page.root }}/reference/#assign) a value to a
[variable]({{ page.root }}/reference/#variable), using the equals sign `=`.
For example, to assign value `60` to a variable `weight_kg`, we would execute:
~~~
weight_kg = 60
~~~
{: .language-python}
From now on, whenever we use `weight_kg`, Python will substitute the value we assigned to
it. In layman's terms, **a variable is a name for a value**.
In Python, variable names:
- can include letters, digits, and underscores
- cannot start with a digit
- are [case sensitive]({{ page.root }}/reference/#case-sensitive).
This means that, for example:
- `weight0` is a valid variable name, whereas `0weight` is not
- `weight` and `Weight` are different variables
## Types of data
Python knows various types of data. Three common ones are:
* integer numbers
* floating point numbers, and
* strings.
In the example above, variable `weight_kg` has an integer value of `60`.
To create a variable with a floating point value, we can execute:
~~~
weight_kg = 60.0
~~~
{: .language-python}
And to create a string, we add single or double quotes around some text, for example:
~~~
weight_kg_text = 'weight in kilograms:'
~~~
{: .language-python}
## Using Variables in Python
To display the value of a variable to the screen in Python, we can use the `print` function:
~~~
print(weight_kg)
~~~
{: .language-python}
~~~
60.0
~~~
{: .output}
We can display multiple things at once using only one `print` command:
~~~
print(weight_kg_text, weight_kg)
~~~
{: .language-python}
~~~
weight in kilograms: 60.0
~~~
{: .output}
Moreover, we can do arithmetic with variables right inside the `print` function:
~~~
print('weight in pounds:', 2.2 * weight_kg)
~~~
{: .language-python}
~~~
weight in pounds: 132.0
~~~
{: .output}
The above command, however, did not change the value of `weight_kg`:
~~~
print(weight_kg)
~~~
{: .language-python}
~~~
60.0
~~~
{: .output}
To change the value of the `weight_kg` variable, we have to
**assign** `weight_kg` a new value using the equals `=` sign:
~~~
weight_kg = 65.0
print('weight in kilograms is now:', weight_kg)
~~~
{: .language-python}
~~~
weight in kilograms is now: 65.0
~~~
{: .output}
> ## Variables as Sticky Notes
>
> A variable is analogous to a sticky note with a name written on it:
> assigning a value to a variable is like putting that sticky note on a particular value.
>
> ![Variables as Sticky Notes](../fig/python-sticky-note-variables-01.svg)
>
> This means that assigning a value to one variable does **not** change
> values of other variables.
> For example, let's store the subject's weight in pounds in its own variable:
>
> ~~~
> # There are 2.2 pounds per kilogram
> weight_lb = 2.2 * weight_kg
> print(weight_kg_text, weight_kg, 'and in pounds:', weight_lb)
> ~~~
> {: .language-python}
>
> ~~~
> weight in kilograms: 65.0 and in pounds: 143.0
> ~~~
> {: .output}
>
> ![Creating Another Variable](../fig/python-sticky-note-variables-02.svg)
>
> Let's now change `weight_kg`:
>
> ~~~
> weight_kg = 100.0
> print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)
> ~~~
> {: .language-python}
>
> ~~~
> weight in kilograms is now: 100.0 and weight in pounds is still: 143.0
> ~~~
> {: .output}
>
> ![Updating a Variable](../fig/python-sticky-note-variables-03.svg)
>
> Since `weight_lb` doesn't "remember" where its value comes from,
> it is not updated when we change `weight_kg`.
{: .callout}
Words are useful, but what's more useful are the sentences and stories we build with them.
Similarly, while a lot of powerful, general tools are built into Python,
specialized tools built up from these basic units live in
......@@ -828,46 +657,6 @@ and that we want a tight layout.
(If we leave out that call to `fig.tight_layout()`,
the graphs will actually be squeezed together more closely.)
> ## Check Your Understanding
>
> What values do the variables `mass` and `age` have after each statement in the following program?
> Test your answers by executing the commands.
>
> ~~~
> mass = 47.5
> age = 122
> mass = mass * 2.0
> age = age - 20
> print(mass, age)
> ~~~
> {: .language-python}
>
> > ## Solution
> > ~~~
> > 95.0 102
> > ~~~
> > {: .output}
> {: .solution}
{: .challenge}
> ## Sorting Out References
>
> What does the following program print out?
>
> ~~~
> first, second = 'Grace', 'Hopper'
> third, fourth = second, first
> print(third, fourth)
> ~~~
> {: .language-python}
>
> > ## Solution
> > ~~~
> > Hopper Grace
> > ~~~
> > {: .output}
> {: .solution}
{: .challenge}
> ## Slicing Strings
>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment