Section: 003

Instructor: Lori Case

E-mail: lori.case@uwaterloo.ca

Office: DC 3103

Office Hours: See Office Hours or by appointment.

Python only now (We are testing to see if CS 116 works as just Python)

**Small note:** They altered the CS 115 to accomodate for this change in CS 116. At the time of writing this in Spring 2016, I took CS 115 almost two years ago in Fall of 2014. There is some information that is familiar to people who took CS 115 last term (Winter 2016) but not to people like me due to the changes in CS 115.

###Some Python Basics:

- Written using regular mathematical notation 3+ 4 5 * (3 + 4) – 1
- Two numeric types (integers and floating point numbers) instead of one
- Strings, Booleans, lists
- No character or symbol type used in CS116.

###A very simple Python program:

x = 2 * (4 + 12) y = x +8

z = y * y

w = “hi”

u = w + w

Scheme | Python | |||
---|---|---|---|---|

Value: | Representation: | Type: | Representation: | Type: |

natural | exact | Nat | exact | Nat |

integer | exact | Int | exact | Int |

rational | exact | Num | inexact | Float |

irrational | inexact | Num | inexact | Float |

##Basic Mathematical Operations

- Addition (+), Subtraction (-), Multiplication (*):
- If combining two Int values, the result is an Int
- If combining two Float values, or a Float and an Int, the result is a Float

**Division:**

x / y

- The result is a Float for any numerical values x and y (even if both are Int)

**Integer division:**

x // y

- The result is the integer part of the division
- If x and y are both Int, the result is an Int
- If either x or y is a Float, the result is an Float, with the decimal part being .0
- Usually used with x and y as Int

**Remainder**

x % y

- x and y should both be Int
- produces the Int remainder when x divided by y

**Exponents**

x ** y

- (anyof Int Float) (anyof Int Float) -> (anyof Int Float) – produces x raised to the power of y

###Last Tips:

- Python precedence operations are standard math precedence rules (BEDMAS)
- Use ## or # for comments (from beginning or middle of line)
- Do not use dash in variable names – Use underscore instead

###Functions in Python:

fn_name (arg1, arg2, …, argN)

- must have correct number of arguments
- separate arguments by single comma

abs(-3.8) => 3.8

type(5) => <type ‘int’>

max(3,5,9) => 9

###The Math Module

To use functions from math

- Import the math module into your program
- Use math.fn or math.const to reference the function or constant you want