Skip to content

Commit

Permalink
Merge branch 'gh-pages' into patch-6
Browse files Browse the repository at this point in the history
  • Loading branch information
richyvk authored Jun 4, 2017
2 parents 1fb89da + dede22e commit 7c0f9c8
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ for instructions on formatting, building, and submitting material,
or run `make` in this directory for a list of helpful commands.

Maintainer(s):
Richard Vankoningsveld, @richyvk
[Richard Vankoningsveld](https://github.com/richyvk) and [Carlos Martinez](https://github.com/c-martinez).
1 change: 1 addition & 0 deletions _episodes/02-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ h
* The difference between stop and start is the slice's length.
* Taking a slice does not change the contents of the original string. Instead,
the slice is a copy of part of the original string.

~~~
atom_name = 'sodium'
print(atom_name[0:3])
Expand Down
79 changes: 52 additions & 27 deletions _episodes/03-types-conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ keypoints:
## Every value has a type.

* Every value in a program has a specific type.
* Integer (`int`): counting numbers like 3 or -512.
* Integer (`int`): whole numbers like 3 or -512.
* Floating point number (`float`): fractional numbers like 3.14159 or -2.5.
* Integers are used to count, floats are used to measure.
* Whole numbers may also be stored as floats, e.g. `1.0`, but `1.0` would still be stored as a `float`.
* Character string (usually called "string", `str`): text.
* Written in either single quotes or double quotes (as long as they match).
* The quotation marks aren't printed when the string is displayed.
* The quotation marks aren't printed using `print()`, but may appear when viewing a value in the Jupyter Notebook or other Python interpreter.

## Use the built-in function `type` to find the type of a value.

* Use the built-in function `type` to find out what type a value has.
* Works on variables as well.
* This works on variables as well.
* But remember: the *value* has the type --- the *variable* is just a label.
* When you change the value of a variable to a new data type, the results of `print(type(your_variable))` will change accordingly.

Expand Down Expand Up @@ -82,7 +82,7 @@ TypeError: unsupported operand type(s) for -: 'str' and 'str'
~~~
{: .error}

## You can use the "+" and "*" operators on strings.
## You can use the `+` and `*` operators on strings.

* "Adding" character strings concatenates them.

Expand All @@ -98,6 +98,7 @@ Ahmed Walsh

* Multiplying a character string by an integer _N_ creates a new string that consists of that character string repeated _N_ times.
* Since multiplication is repeated addition.
* There are more ways that traditional math operators will work on other data types. There isn't a perfect formula for figuing out what they do, so experimentation is valuable.

~~~
separator = '=' * 10
Expand Down Expand Up @@ -262,12 +263,26 @@ first is 2 and second is 5
> 5. Number of reference queries in a year.
> 6. Average library classes taught per semester.
>
> > ## Solution
> > 1. Integer
> > 2. Float
> > 3. String
> > 4. Integer
> > 5. Integer
> > 6. Float
> {: .solution}
{: .challenge}
> ## Division Types
> There are three different types of division:
> 1. 'Normal' division (aka floating-point division) is what most people may be
> familiar with: 5 / 2 = 2.5
> 2. Floor division, which cuts out the part after the period: 5 / 2 = 2
> 3. Modulo division, which only keeps the remained after division: 5 / 2 = 1
>
> In Python 3, the `//` operator performs integer (whole-number) floor division, the `/` operator performs floating-point
> division, and the '%' (or *modulo*) operator calculates and returns the remainder from integer division:
> In Python 3, the `/` operator performs floating-point division, the `//`
> operator performs floor division, and the '%' (or *modulo*) operator
> calculates the modulo division:
>
> ~~~
> print('5 // 3:', 5//3)
Expand All @@ -283,28 +298,30 @@ first is 2 and second is 5
> ~~~
> {: .output}
>
> If `num_students` is the number of students enrolled in a course,
> and `num_per_class` is the number that can attend a single class,
> If `num_students` is the number of students enrolled in a course (let say 600),
> and `num_per_class` is the number that can attend a single class (let say 42),
> write an expression that calculates the number of classes needed
> to teach everyone.
>
> > ## Solution
> > Depending on requirements it might be important to detect when the number of students per class doesn't divide the
> > number of students evenly. Detect it with the `%` operator and test if the remainder that it returns is greater than
> > 0.
> >
> >
> >
> > ~~~
> > num_students = 600
> > num_per_class = 42
> > num_classes = num_students // num_per_class
> > remainder = num_students % num_per_class
> >
> > print(num_students, 'students,', num_per_class, 'per class:', num_classes)
> > print(num_students, 'students,', num_per_class, 'per class')
> > print(num_classes, ' full classes, plus an extra class with only ', remainder, 'students')
> > ~~~
> > {: .python}
> > ~~~
> > 600 students, 42 per class: 14
> > 600 students, 42 per class
> > 14 full classes, plus an extra class with only 12 students
> > ~~~
> > {: .output}
> {: .solution}
Expand All @@ -327,6 +344,8 @@ first is 2 and second is 5
> ~~~
> {: .output}
>
> **Note:** conversion is some times also called typecast.
>
> If the conversion doesn't make sense, however, an error message will occur
>
> ~~~
Expand Down Expand Up @@ -354,30 +373,29 @@ first is 2 and second is 5
> print("fractional string to int:", int("3.4"))
> ~~~
> {: .python}
>
>
> > ## Solution
> > What do you expect this program to do? It would not be so unreasonable to expect the Python 3 `int` command to
> > convert the string "3.4" to 3.4 and an additional type conversion to 3. After all, Python 3 performs a lot of other
> > What do you expect this program to do? It would not be so unreasonable to
> > expect the Python `int` command to convert the string "3.4" to 3.4 and an
> > additional type conversion to 3. After all, Python performs a lot of other
> > magic - isn't that part of its charm?
> >
> > However, Python 3 throws an error. Why? To be consistent, possibly. If you ask Python to perform two consecutive
> > typecasts, you must convert it explicitly in code.
> >
> > However, Python throws an error. Why? To be consistent, possibly. If you
> > ask Python to perform two consecutive typecasts, you must convert it
> > explicitly in code.
> >
> > ~~~
> > int("3.4")
> > int(float("3.4"))
> > num_as_string = "3.4"
> > num_as_float = float(num_as_string)
> > num_as_int = int(num_as_float)
> > print(num_as_int)
> > ~~~
> > {: .python}
> > ~~~
> > In [2]: int("3.4")
> > ---------------------------------------------------------------------------
> > ValueError Traceback (most recent call last)
> > <ipython-input-2-ec6729dfccdc> in <module>()
> > ----> 1 int("3.4")
> > ValueError: invalid literal for int() with base 10: '3.4'
> > 3
> > ~~~
> > {: .output}
> > We could also write it in a single line like this: `int(float("3.4"))`
> {: .solution}
{: .challenge}
Expand All @@ -402,6 +420,13 @@ first is 2 and second is 5
>
> > ## Solution
> >
> > Answer: 1 and 4
> > Answer: 1 and 4.
> >
> > 1. is correct
> > 2. gives 2.1
> > 3. gives an arror because we cannot convert text to int directly
> > 4. is correct
> > 5. gives 2 (as an integer not as a float)
> > 6. gives an error because `second` is a string.
> {: .solution}
{: .challenge}
8 changes: 5 additions & 3 deletions _episodes/04-built-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ adjustment = 0.5 # Neither is this - anything after '#' is ignored.

* We have seen some functions already --- now let's take a closer look.
* An *argument* is a value passed into a function.
* Any arguments you want to pass into a function must go into the `()`
* print("I'm an argument and must go here.")
* You must always use parentheses, because this is how Python knows you are calling a function.
* You leave them empty if you don't want or need to pass any arguments in.
* `len` takes exactly one.
* `int`, `str`, and `float` create a new value from an existing one.
* `print` takes zero or more.
* `print` with no arguments prints a blank line.
* Must always use parentheses, even if they're empty,
so that Python knows a function is being called.
* `print()` prints a blank line.

~~~
print('before')
Expand Down
15 changes: 8 additions & 7 deletions _episodes/06-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ cos(pi) is -1.0
> >
> > The [datetime module](https://docs.python.org/3/library/datetime.html) seems like it could help you.
> >
> >
> >
> > You could use `date(year, month, date).isoformat()` to convert your date:
> >
> > ~~~
Expand All @@ -186,14 +186,15 @@ cos(pi) is -1.0
> > ~~~
> > {: .python}
> >
> {: .solution}
{: .challenge}
> ## Jigsaw Puzzle (Parson's Problem) Programming Example
>
>
> Rearrange the following statements so that a random
> DNA base is printed. Not all statements may be needed. Feel free to use/add
> intermediate variables.
> DNA base is printed. Not all statements may be needed. Feel free to use/add
> intermediate variables.
>
> ~~~
> bases="ACTTGCTTGAC"
Expand Down Expand Up @@ -248,7 +249,7 @@ cos(pi) is -1.0
> > ~~~
> > {: .python}
> >
> > can bewritten as
> > can be written as
> >
> > ~~~
> > import math
Expand All @@ -268,13 +269,13 @@ cos(pi) is -1.0
> ## There Are Many Ways To Import Libraries!
>
> Match the following print statements with the appropriate library calls
>
>
> Library calls:
> 1. `from math import sin,pi`
> 2. `import math`
> 3. `import math as m`
> 4. `from math import *`
>
>
> Print commands:
> 7. `print("sin(pi/2) =",sin(pi/2))`
> 8. `print("sin(pi/2) =",m.sin(m.pi/2))`
Expand Down
4 changes: 2 additions & 2 deletions _episodes/11-lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ primes after removing last item: [2, 3, 5, 7]
* Use `[]` on its own to represent a list that doesn't contain any values.
* "The zero of lists."
* Helpful as a starting point for collecting values
(which we will see in the [next episode]({{page.root}}/09-for-loops/)).
(which we will see in the [next episode]({{page.root}}/12-for-loops/)).
## Lists may contain values of different types.
Expand Down Expand Up @@ -183,7 +183,7 @@ TypeError: 'str' object does not support item assignment
## Indexing beyond the end of the collection is an error.
* Python reports an `IndexError` if we attempt to access a value that doesn't exist.
* This is a kind of [runtime error]({{ page.root }}/05-error-messages/).
* This is a kind of runtime error.
* Cannot be detected as the code is parsed
because the index might be calculated based on data.
Expand Down
35 changes: 29 additions & 6 deletions _episodes/12-for-loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@ for number in [2, 3, 5]:
* The loop variable, `number`, is what changes for each *iteration* of the loop.
* The "current thing".

## Loop variables can be called anything.

* As with all variables, loop variables are:
* Created on demand.
* Meaningless: their names can be anything at all.

## Loop variable names follow the normal variable name conventions.

* Loop variables will:
* Be created on demand during the course of each loop.
* Persist after the loop finishes.
* Use a new variable name to avoid overwriting a data collection you need to keep for later
* Often be used in the course of the loop
* So give them a meaningful name you'll understand as the body code in your loop grows.
* Example: `for single_letter in ['A', 'B', 'C', 'D']:` instead of `for asdf in ['A', 'B', 'C', 'D']:`
~~~
for kitten in [2, 3, 5]:
print(kitten)
Expand Down Expand Up @@ -152,6 +155,26 @@ a range is not a list: range(0, 3)
~~~
{: .output}

## Or use `range` to repeat an action an arbitrary number of times.

* You don't actually have to use the iterable variable's value.
* Use this structure to simply repeat an action some number of times.
* That number of times goes into the `range` function.

~~~
for number in range(5):
print("Again!")
~~~
{: .python}
~~~
Again!
Again!
Again!
Again!
Again!
~~~
{: .output}

## The Accumulator pattern turns many values into one.

* A common pattern in programs is to:
Expand Down
20 changes: 10 additions & 10 deletions setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ permalink: /setup/

## Installing Python Using Anaconda

[Python][python] is great for general-purpose programming, and a popular language
[Python][python] is great for general-purpose programming and is a popular language
for scientific computing as well. Installing all of the packages required for this
lesosn individually can be a bit difficult, however, so we recommend the all-in-one
lessons individually can be a bit difficult, however, so we recommend the all-in-one
installer [Anaconda][anaconda].

Regardless of how you choose to install it, please make sure you install Python
version 3.x (e.g., Python 3.6 version). Also, please set up your python environment at
least a day in advance of the workshop. If you encounter problems with the
version 3.x (e.g., Python 3.6 version). Also, please set up your Python environment at
least a day in advance of the workshop. If you encounter problems with the
installation procedure, ask your workshop organizers via e-mail for assistance so
you are ready to go as soon as the workshop begins.

Expand All @@ -28,12 +28,12 @@ you are ready to go as soon as the workshop begins.
default settings. The only exception is to check the
**Make Anaconda the default Python** option.

### Mac OS X - [Video tutorial][video-mac]
### macOS - [Video tutorial][video-mac]

1. Open [http://continuum.io/downloads][continuum-mac]
with your web browser.

2. Download the Python 3 installer for OS X.
2. Download the Python 3 installer for macOS.

3. Install Python 3 using all of the defaults for installation.

Expand Down Expand Up @@ -77,7 +77,7 @@ for Python. If you installed Python using Anaconda, Spyder should already be on
you did not use Anaconda, use the Python package manager pip
(see the [Spyder website][spyder-install] for details.)
To start Spyder, open a terminal or git bash and type the command:
To start Spyder, open a terminal or Git Bash and type the command:
~~~
$ spyder3
Expand All @@ -93,9 +93,9 @@ $ python
{: .bash}
[anaconda]: https://www.continuum.io/anaconda
[continuum-mac]: http://continuum.io/downloads#_macosx
[continuum-linux]: http://continuum.io/downloads#_unix
[continuum-windows]: http://continuum.io/downloads#_windows
[continuum-mac]: http://continuum.io/downloads#macos
[continuum-linux]: http://continuum.io/downloads#linux
[continuum-windows]: http://continuum.io/downloads#windows
[python]: https://python.org
[spyder]: https://pythonhosted.org/spyder/
[spyder-install]: https://pythonhosted.org/spyder/installation.html
Expand Down

0 comments on commit 7c0f9c8

Please sign in to comment.