Skip to content

Commit

Permalink
Merge pull request hpc-carpentry#7 from tkphd/issue6-code_formatting
Browse files Browse the repository at this point in the history
Issue6 code formatting
  • Loading branch information
psteinb authored Dec 10, 2019
2 parents 147dac4 + 7f3df08 commit 3af2e9b
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 271 deletions.
63 changes: 33 additions & 30 deletions _episodes/01-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ It is very easy to do basic maths in Python.
```python
print(5 + 1)
```
{: .language-python}
```
6
```
Expand All @@ -27,7 +28,7 @@ Notice how leaving out `print()` gives us the same result as above.
```python
5 + 1
```

{: .language-python}
```
6
```
Expand All @@ -41,7 +42,7 @@ Python can do all of the normal basic maths operations you'd expect.
4 * 6
14 / 3
```

{: .language-python}
```
8
-7
Expand All @@ -55,7 +56,7 @@ You can also use it to more complicated operations, like exponentiation (`**`):
```python
5 ** 2
```

{: .language-python}
```
25
```
Expand All @@ -69,7 +70,7 @@ Remainder division (`%`), gives the remainder after division.
5 // 2 # floor division
5 % 2 # remainder division
```

{: .language-python}
```
2
1
Expand All @@ -81,7 +82,7 @@ Python follows the normal order of operations for maths.
```python
4 + 1 * 6
```

{: .language-python}
```
10
```
Expand All @@ -94,7 +95,7 @@ Note that there is no limit to the number of parentheses you can use.
```python
(4 + 1) * 6
```

{: .language-python}
```
30
```
Expand All @@ -110,15 +111,15 @@ We can set them with the `=` sign.
```python
weight_kg = 55
```

{: .language-python}

If we want to retrieve the information we've stored,
we can do it by simply typing the name of the variable again.

```python
weight_kg
```

{: .language-python}
```
55
```
Expand All @@ -129,7 +130,7 @@ We can perform maths on variables the same way we would normally.
```python
print('weight in pounds:', 2.2 * weight_kg)
```

{: .language-python}
```
weight in pounds: 121.00000000000001
```
Expand All @@ -144,7 +145,7 @@ We can also change a variable’s value by assigning it a new one:
weight_lb = 2.2 * weight_kg
print(weight_lb)
```

{: .language-python}
```
121.00000000000001
```
Expand All @@ -159,7 +160,7 @@ weight_kg = 10000
print('after updating, weight_kg ending value is', weight_kg)
print('weight in lb ending value is', weight_lb)
```

{: .language-python}
```
weight_kg starting value is 55
after updating, weight_kg ending value is 10000
Expand All @@ -181,7 +182,7 @@ we will need to perform this operation explicitly.
weight_lb = weight_kg * 2.2
print('new value for weight_lb is', weight_lb)
```

{: .language-python}
```
new value for weight_lb is 22000.0
```
Expand All @@ -193,16 +194,17 @@ Don't know how something works?
Try it and find out!

> ## Where are variables stored?
>
> Your computer has two places where it stores information: hard disk and memory.
> What are they and what are they used for?
> Where do variables get stored?
>
>
> Memory is where temporary information on your computer gets placed.
> It is very fast and easy to access, but has one important drawback:
> data here is erased when your program quits or your computer shuts down.
> All information you save as variables in Python will be stored in memory!
> When programming, we always need to save our data as a file (on our hard disk) if we want to keep it!
>
>
> Your computer's hard disk is used to store information long-term.
> This is where files get stored, and the information on your hard drive is more or less permanent.
> Hard drives can also store lots of data very cheaply - a terabyte of hard drive space is very cheap, whereas the same amount of memory costs a lot more.
Expand All @@ -220,7 +222,7 @@ For instance, what happens if we accidentally don't finish a command?
```python
1 +
```

{: .language-python}
```
SyntaxError: invalid syntax (<ipython-input-15-70475fc083df, line 1)
File "<ipython-input-15-70475fc083df", line 1
Expand Down Expand Up @@ -290,7 +292,7 @@ It will print the data type of whatever is inside the parentheses.
type('this is a string')
type("this is also a string")
```

{: .language-python}
```
str
str
Expand All @@ -310,7 +312,7 @@ multiline = '''
print(multiline)
type(multiline)
```

{: .language-python}
```
This string
spans
Expand All @@ -329,7 +331,7 @@ For instance, we can even use the `+` sign to put strings together!
'some text' + 'MORE TEXT'
'repetition' * 3
```

{: .language-python}
```
'some textMORE TEXT'
'repetitionrepetitionrepetition'
Expand All @@ -342,7 +344,7 @@ Attempting to add a string to a number doesn't work!
```python
'5' + 5
```

{: .language-python}
```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Expand All @@ -365,7 +367,7 @@ type(5)
type(-1000)
type(6 + -33)
```

{: .language-python}
```
int
int
Expand All @@ -378,7 +380,7 @@ But what happens when we perform a maths operation that would result in a decima
```python
type(10 / 3)
```

{: .language-python}
```
float
```
Expand All @@ -392,7 +394,7 @@ To explicitly set a number as a float, just add a decimal point.
type(1.3)
type(22.)
```

{: .language-python}
```
float
float
Expand All @@ -408,7 +410,7 @@ Unsurprisingly, these are defined as `True` and `False`.
type(True)
type(False)
```

{: .language-python}
```
bool
bool
Expand All @@ -421,7 +423,7 @@ We will revisit `None` in more detail later, so for now, just be aware it exists
```python
type(None)
```

{: .language-python}
```
NoneType
```
Expand All @@ -440,7 +442,7 @@ NoneType
> ```python
> print(int('5') + 5)
> ```
>
> {: .language-python}
> ```
> 10
> ```
Expand All @@ -458,12 +460,13 @@ NoneType
> Use only the commands shown above.
>
> ```python
> 1 + '1' == '11'
> '6' - 7 == -1
> 7.23 == 7
> '5' == True
> 4 / 1.3 == 4
> 1 + '1' == '11'
> '6' - 7 == -1
> 7.23 == 7
> '5' == True
> 4 / 1.3 == 4
> ```
> {: .language-python}
{: .challenge}
> ## Data type conversion pitfalls
Expand Down
14 changes: 7 additions & 7 deletions _episodes/02-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Enter the following text in a text editor and save it under any name you like
```python
print('it works!!!')
```

{: .language-python}

We can now run this program in several ways.
If we were to open up a terminal in the folder where we had saved our program,
Expand All @@ -36,6 +36,7 @@ it works!!!
{: .output}

> ## What's the point of print()?
>
> We saw earlier that there was no difference between printing something with `print()`
> and just entering a command on the command line.
> But is this really the case?
Expand All @@ -47,7 +48,7 @@ it works!!!
> print('this involves print')
> 'this does not'
> ```
>
> {: .language-python}
> What gets printed if you execute this as a script?
> What gets printed if you execute things line by line?
> Using this information, what's the point of `print()`?
Expand All @@ -65,7 +66,7 @@ I've called my script `test.py` as an example.
```
!python3 test.py
```
{: .language-python}
```
it works!!!
```
Expand All @@ -83,7 +84,7 @@ To access a package, we need to `import` it.
```python
import sys
```

{: .language-python}

You'll notice that there's no output.
Only one thing is changed:
Expand All @@ -96,16 +97,15 @@ Let's make a new script called `command-args.py` to try this out.

```python
import sys

print('we typed: ', sys.argv)
```

{: .language-python}

We can then execute this program with:
```
!python3 test.py word1 word2 3
```

{: .language-python}
```
we typed: ['test.py', 'word1', 'word2', '3']
```
Expand Down
Loading

0 comments on commit 3af2e9b

Please sign in to comment.