Edit
This commit is contained in:
@@ -59,7 +59,7 @@ shell. This is Python's native environment. If you are able to use
|
||||
Python in the shell, you will be able to use virtually everywhere
|
||||
else.
|
||||
|
||||
## Exercises 1.1
|
||||
## Exercises
|
||||
|
||||
### (a) Using Python as a Calculator
|
||||
|
||||
|
||||
@@ -194,11 +194,10 @@ Comments are denoted by `#` and extend to the end of the line.
|
||||
|
||||
### Variables
|
||||
|
||||
A variable is a name for a value.
|
||||
|
||||
You can use letters (lower and upper-case) from a to z. As well as the character underscore `_`.
|
||||
|
||||
Numbers can also be part of the name of a variable, except as the first character.
|
||||
A variable is a name for a value. You can use letters (lower and
|
||||
upper-case) from a to z. As well as the character underscore `_`.
|
||||
Numbers can also be part of the name of a variable, except as the
|
||||
first character.
|
||||
|
||||
```python
|
||||
height = 442 # valid
|
||||
@@ -221,7 +220,6 @@ height = 'Really tall' # A string
|
||||
### Case Sensitivity
|
||||
|
||||
Python is case sensitive. Upper and lower-case letters are considered different letters.
|
||||
|
||||
These are all different variables:
|
||||
|
||||
```python
|
||||
@@ -240,7 +238,6 @@ WHILE x < 0: # ERROR
|
||||
### Looping
|
||||
|
||||
Looping is a way to execute a set of instructions any number of times.
|
||||
|
||||
There are many ways to accomplish this in Python, one of them is the `while` statement:
|
||||
|
||||
```python
|
||||
@@ -257,7 +254,6 @@ The statements below the `while` will execute as long as the expression after th
|
||||
### Indentation
|
||||
|
||||
Indentation in Python is used to denote a set of statements that go together.
|
||||
|
||||
From our previous example:
|
||||
|
||||
```python
|
||||
@@ -277,25 +273,15 @@ The indentation means that the following statements go together under the `while
|
||||
num_bills = num_bills * 2
|
||||
```
|
||||
|
||||
Because the next statement is not indented, it means that it does not belong to the previous set.
|
||||
Because the next statement is not indented, it means that it does not
|
||||
belong to the previous set. The empty line is just for
|
||||
readability. It does not affect the execution.
|
||||
|
||||
```python
|
||||
print('Number of days', days)
|
||||
```
|
||||
### Indentation best practices
|
||||
|
||||
The empty line is just for readability. It does not affect the execution.
|
||||
|
||||
### Blocks
|
||||
|
||||
A block is a set of statements grouped together.
|
||||
|
||||
In our previous example, the statements within the `while` form a *block*.
|
||||
|
||||
```python
|
||||
print(day, num_bills, num_bills * bill_thickness)
|
||||
day = day + 1
|
||||
num_bills = num_bills * 2
|
||||
```
|
||||
* Use spaces instead of tabs.
|
||||
* Use 4 spaces per level.
|
||||
* Use a Python-aware editor.
|
||||
|
||||
Indentation within the block must be consistent.
|
||||
|
||||
@@ -306,17 +292,6 @@ while num_bills * bill_thickness < sears_height:
|
||||
num_bills = num_bills * 2
|
||||
```
|
||||
|
||||
The character colon `:` indicates the start of a block and must be present.
|
||||
|
||||
```python
|
||||
while num_bills * bill_thickness < sears_height:
|
||||
```
|
||||
|
||||
### Indentation best practices
|
||||
|
||||
* Use spaces instead of tabs.
|
||||
* Use 4 spaces per level.
|
||||
* Use a Python-aware editor.
|
||||
|
||||
### Conditionals
|
||||
|
||||
@@ -324,10 +299,10 @@ The `if` statement is used to execute a conditional:
|
||||
|
||||
```python
|
||||
if a > b:
|
||||
# `a` is greater than `b`
|
||||
# a is greater than b
|
||||
print('Computer says no')
|
||||
else:
|
||||
# `a` is lower or equal to `b`
|
||||
# a is lower or equal to b
|
||||
print('Computer says yes')
|
||||
```
|
||||
|
||||
@@ -337,13 +312,13 @@ You can check for multiple conditions with the `elif`.
|
||||
|
||||
```python
|
||||
if a > b:
|
||||
# `a` is greater than `b`
|
||||
# a is greater than b
|
||||
print('Computer says no')
|
||||
elif a == b:
|
||||
# `a` is equal to `b`
|
||||
# a is equal to b
|
||||
print('Computer says yes')
|
||||
else:
|
||||
# `a` is lower to `b`
|
||||
# a is lower to b
|
||||
print('Computer says maybe')
|
||||
```
|
||||
|
||||
@@ -406,16 +381,12 @@ print('Your name is', name)
|
||||
```
|
||||
|
||||
`input` prints a prompt to the user and returns the response.
|
||||
|
||||
This is useful for small programs, learning exercises or simple debugging.
|
||||
|
||||
It is not widely used for real programs.
|
||||
|
||||
### `pass` statement
|
||||
|
||||
Sometimes you need to specify an empty block.
|
||||
|
||||
The keyword `pass` is used for it.
|
||||
Sometimes you need to specify an empty block. The keyword `pass` is used for it.
|
||||
|
||||
```python
|
||||
if a > b:
|
||||
@@ -426,7 +397,7 @@ else:
|
||||
|
||||
This is also called a "no-op" statement. It does nothing. It serves as a placeholder for statements. Possibly to be added later.
|
||||
|
||||
## Exercises 1.2
|
||||
## Exercises
|
||||
|
||||
### (a) The Bouncing Ball
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
This section covers some basics of performing mathematical calculations in Python.
|
||||
|
||||
## Reading
|
||||
|
||||
### Types of Numbers
|
||||
|
||||
Python has 4 types of numbers:
|
||||
@@ -95,12 +93,11 @@ c = -1.345e-10
|
||||
```
|
||||
|
||||
Floats are represented as double precision using the native CPU representation [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754).
|
||||
This is the same as the `double` type in the programming language C.
|
||||
|
||||
> 17 digits or precision
|
||||
> Exponent from -308 to 308
|
||||
|
||||
This is the same as the `double` type in the programming language C.
|
||||
|
||||
Be aware that floating point numbers are inexact when representing decimals.
|
||||
|
||||
```python
|
||||
@@ -128,7 +125,6 @@ abs(x) Absolute Value
|
||||
```
|
||||
|
||||
Theses are the same operators as Integers, except for the bit-wise operators.
|
||||
|
||||
Additional math functions are found in the `math` module.
|
||||
|
||||
```python
|
||||
@@ -161,7 +157,7 @@ Try it out.
|
||||
>>>
|
||||
```
|
||||
|
||||
## Exercise 1.3
|
||||
## Exercises
|
||||
|
||||
### (a) Dave's mortgage
|
||||
|
||||
@@ -198,7 +194,6 @@ When you run the new program, it should report a total payment of `929,965.62` o
|
||||
### (c) Making an Extra Payment Calculator
|
||||
|
||||
Modify the program so that extra payment information can be more generally handled.
|
||||
|
||||
Make it so that the user can set these variables:
|
||||
|
||||
```python
|
||||
@@ -214,7 +209,6 @@ How much will Dave pay if he pays an extra $1000/month for 4 years starting in y
|
||||
### (d) Making a table
|
||||
|
||||
Modify the program to print out a table showing the month, total paid so far, and the remaining principal.
|
||||
|
||||
The output should look something like this:
|
||||
|
||||
```bash
|
||||
|
||||
Reference in New Issue
Block a user