### 02-numpy.md: remove Python basics from the episode

parent 1690754b
 ... ... @@ -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!