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
|
Python in the shell, you will be able to use virtually everywhere
|
||||||
else.
|
else.
|
||||||
|
|
||||||
## Exercises 1.1
|
## Exercises
|
||||||
|
|
||||||
### (a) Using Python as a Calculator
|
### (a) Using Python as a Calculator
|
||||||
|
|
||||||
|
|||||||
@@ -194,11 +194,10 @@ Comments are denoted by `#` and extend to the end of the line.
|
|||||||
|
|
||||||
### Variables
|
### Variables
|
||||||
|
|
||||||
A variable is a name for a value.
|
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 `_`.
|
||||||
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.
|
||||||
Numbers can also be part of the name of a variable, except as the first character.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
height = 442 # valid
|
height = 442 # valid
|
||||||
@@ -221,7 +220,6 @@ height = 'Really tall' # A string
|
|||||||
### Case Sensitivity
|
### Case Sensitivity
|
||||||
|
|
||||||
Python is case sensitive. Upper and lower-case letters are considered different letters.
|
Python is case sensitive. Upper and lower-case letters are considered different letters.
|
||||||
|
|
||||||
These are all different variables:
|
These are all different variables:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -240,7 +238,6 @@ WHILE x < 0: # ERROR
|
|||||||
### Looping
|
### Looping
|
||||||
|
|
||||||
Looping is a way to execute a set of instructions any number of times.
|
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:
|
There are many ways to accomplish this in Python, one of them is the `while` statement:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -257,7 +254,6 @@ The statements below the `while` will execute as long as the expression after th
|
|||||||
### Indentation
|
### Indentation
|
||||||
|
|
||||||
Indentation in Python is used to denote a set of statements that go together.
|
Indentation in Python is used to denote a set of statements that go together.
|
||||||
|
|
||||||
From our previous example:
|
From our previous example:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -277,25 +273,15 @@ The indentation means that the following statements go together under the `while
|
|||||||
num_bills = num_bills * 2
|
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
|
### Indentation best practices
|
||||||
print('Number of days', days)
|
|
||||||
```
|
|
||||||
|
|
||||||
The empty line is just for readability. It does not affect the execution.
|
* Use spaces instead of tabs.
|
||||||
|
* Use 4 spaces per level.
|
||||||
### Blocks
|
* Use a Python-aware editor.
|
||||||
|
|
||||||
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
|
|
||||||
```
|
|
||||||
|
|
||||||
Indentation within the block must be consistent.
|
Indentation within the block must be consistent.
|
||||||
|
|
||||||
@@ -306,17 +292,6 @@ while num_bills * bill_thickness < sears_height:
|
|||||||
num_bills = num_bills * 2
|
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
|
### Conditionals
|
||||||
|
|
||||||
@@ -324,10 +299,10 @@ The `if` statement is used to execute a conditional:
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
if a > b:
|
if a > b:
|
||||||
# `a` is greater than `b`
|
# a is greater than b
|
||||||
print('Computer says no')
|
print('Computer says no')
|
||||||
else:
|
else:
|
||||||
# `a` is lower or equal to `b`
|
# a is lower or equal to b
|
||||||
print('Computer says yes')
|
print('Computer says yes')
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -337,13 +312,13 @@ You can check for multiple conditions with the `elif`.
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
if a > b:
|
if a > b:
|
||||||
# `a` is greater than `b`
|
# a is greater than b
|
||||||
print('Computer says no')
|
print('Computer says no')
|
||||||
elif a == b:
|
elif a == b:
|
||||||
# `a` is equal to `b`
|
# a is equal to b
|
||||||
print('Computer says yes')
|
print('Computer says yes')
|
||||||
else:
|
else:
|
||||||
# `a` is lower to `b`
|
# a is lower to b
|
||||||
print('Computer says maybe')
|
print('Computer says maybe')
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -406,16 +381,12 @@ print('Your name is', name)
|
|||||||
```
|
```
|
||||||
|
|
||||||
`input` prints a prompt to the user and returns the response.
|
`input` prints a prompt to the user and returns the response.
|
||||||
|
|
||||||
This is useful for small programs, learning exercises or simple debugging.
|
This is useful for small programs, learning exercises or simple debugging.
|
||||||
|
|
||||||
It is not widely used for real programs.
|
It is not widely used for real programs.
|
||||||
|
|
||||||
### `pass` statement
|
### `pass` statement
|
||||||
|
|
||||||
Sometimes you need to specify an empty block.
|
Sometimes you need to specify an empty block. The keyword `pass` is used for it.
|
||||||
|
|
||||||
The keyword `pass` is used for it.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
if a > b:
|
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.
|
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
|
### (a) The Bouncing Ball
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
This section covers some basics of performing mathematical calculations in Python.
|
This section covers some basics of performing mathematical calculations in Python.
|
||||||
|
|
||||||
## Reading
|
|
||||||
|
|
||||||
### Types of Numbers
|
### Types of Numbers
|
||||||
|
|
||||||
Python has 4 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).
|
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
|
> 17 digits or precision
|
||||||
> Exponent from -308 to 308
|
> 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.
|
Be aware that floating point numbers are inexact when representing decimals.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -128,7 +125,6 @@ abs(x) Absolute Value
|
|||||||
```
|
```
|
||||||
|
|
||||||
Theses are the same operators as Integers, except for the bit-wise operators.
|
Theses are the same operators as Integers, except for the bit-wise operators.
|
||||||
|
|
||||||
Additional math functions are found in the `math` module.
|
Additional math functions are found in the `math` module.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -161,7 +157,7 @@ Try it out.
|
|||||||
>>>
|
>>>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Exercise 1.3
|
## Exercises
|
||||||
|
|
||||||
### (a) Dave's mortgage
|
### (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
|
### (c) Making an Extra Payment Calculator
|
||||||
|
|
||||||
Modify the program so that extra payment information can be more generally handled.
|
Modify the program so that extra payment information can be more generally handled.
|
||||||
|
|
||||||
Make it so that the user can set these variables:
|
Make it so that the user can set these variables:
|
||||||
|
|
||||||
```python
|
```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
|
### (d) Making a table
|
||||||
|
|
||||||
Modify the program to print out a table showing the month, total paid so far, and the remaining principal.
|
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:
|
The output should look something like this:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
Reference in New Issue
Block a user