MTH 337: Week 1

Week 2 Notebook

Printing

In [1]:
# Print a string
print "Hello World!"
Hello World!
In [2]:
# Strings can have either single or double quotes
print 'Hello'
Hello
In [3]:
# Printing an integer
print 1
1

Comments

In [4]:
# This is a single-line comment

Mathematical Operations

In [5]:
# Addition
2 + 2
Out[5]:
4
In [6]:
# Subtraction
10 - 3
Out[6]:
7
In [7]:
# Multiplication
6 * 7
Out[7]:
42
In [8]:
# Integer division - throws away remainder
15 / 4
Out[8]:
3
In [9]:
# Float division
15./4
Out[9]:
3.75

Converting integers to floats

In [10]:
# Adding two integers gives an integer
1 + 1
Out[10]:
2
In [11]:
# Adding a float and an integer gives a float
1. + 1
Out[11]:
2.0
In [12]:
# How to convert an integer to a float
float(1)
Out[12]:
1.0

Other Mathematical Operations

In [13]:
# Exponent
2**5
Out[13]:
32
In [14]:
# Remainder
26 % 5
Out[14]:
1

Variable Assignment

In [15]:
# Variable on the left, value on the right
x = 1
In [16]:
# The variable x now has the value 1
print x
1

Variable names are case sensitive

In [17]:
x = 1
X = 2
print x, X
1 2
In [18]:
# Can use the variables instead of the values
y = 2
z = x + y
print z
3
In [19]:
# "print" adds a newline - usa a comma to stop this
print x, y, z
1 2 3
In [20]:
print x,
print y,
print z
1 2 3
In [21]:
a, b = 3, 4
In [22]:
print a, b
3 4
In [23]:
# Swap a and b
a, b = b, a
In [24]:
print a, b
4 3
In [25]:
# An ugly way to do the same thing
c = a
a = b
b = c
print a, b
3 4

What is the output below?

In [26]:
quiz_max = 10
quiz_ave = 9
quiz_percent = (quiz_ave/quiz_max) * 100
print "Quiz average =", quiz_percent, "%"
Quiz average = 0 %

Importing

In [27]:
import math
In [28]:
# Need to prefix variables and functions with "math"
math.pi
Out[28]:
3.141592653589793
In [29]:
math.sin(1)
Out[29]:
0.8414709848078965
In [30]:
math.cos(0)
Out[30]:
1.0
In [31]:
from math import pi
In [32]:
# Don't need to preface "pi" anymore
pi
Out[32]:
3.141592653589793
In [33]:
from math import *
In [34]:
# All math variables and functions are now available
pi
Out[34]:
3.141592653589793
In [35]:
sin(1.5)
Out[35]:
0.9974949866040544

Find what's in the "math" module

In [36]:
print dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

Getting help on a function

In [37]:
? degrees
In [38]:
degrees(pi)
Out[38]:
180.0
In [39]:
degrees(2*pi)
Out[39]:
360.0

Using Out[]

In [40]:
Out[38]
Out[40]:
180.0
In [41]:
Out[38] + Out[39]
Out[41]:
540.0

Boolean variables

In [42]:
True
Out[42]:
True
In [43]:
False
Out[43]:
False

Boolean expressions return True or False

In [44]:
# Test for equality
3 == 2
Out[44]:
False
In [45]:
# Test for inequality
3 != 2
Out[45]:
True
In [46]:
# Greater than
3 > 3
Out[46]:
False
In [47]:
# Greater than or equals
3 >= 3
Out[47]:
True
In [48]:
# Less than
2 < 1
Out[48]:
False
In [49]:
# Less than or equals
2 <= 1
Out[49]:
False

"if" statement

In [50]:
if 3 > 2:
    print "3 is greater than 2"
3 is greater than 2

"if-else" statement

In [51]:
if 3 < 2:
    print "3 is less than 2"
else:
    print "3 is not less than 2"
3 is not less than 2

"if-elif-else" statement

In [52]:
grade = 88
if grade >= 90:
    print 'A'
elif grade >= 80:
    print 'B'
elif grade >= 70:
    print 'C'
else:
    print 'F'
B

"while" statement

In [53]:
n = 10
while n > 0:
    print n
    n -= 1
print "Blast Off!"
10
9
8
7
6
5
4
3
2
1
Blast Off!

Define functions using "def"

In [54]:
# Define the function
def add(x, y):
    return x + y
In [55]:
# Call the function
add(10, 20)
Out[55]:
30
In [56]:
# Can also add a documentation string
def add(x, y):
    "Add x and y, return result"
    return x + y
In [57]:
# Our documentation string now becomes part of the help system
? add

Assignment operators +=, -=, *=, /=

In [58]:
# a += b means a = a + b
a = 10
b = 5
a += b
print a
15
In [59]:
# a -= b means a = a - b
a = 10
b = 2
a -= b
print a
8
In [60]:
# a*= b means a = a*b
a = 55
b = 2
a *= b
print a
110
In [61]:
# a /= b means a = a / b
a = 10
b = 2
a /= b
print a
5

Logical Operators

In [62]:
# "not" turns True into False and False into True
not 10 == 10
Out[62]:
False
In [63]:
# "and" is only True if all of the boolean expressions are True
6 < 5 and 2 < 3
Out[63]:
False
In [64]:
# "or" is True if any of the boolean expressions are True
6 < 5 or 2 < 3
Out[64]:
True

Order of Evaluation for Logical Operators

  1. not
  2. and
  3. or
In [65]:
# The "not" is evaluated first, then the "or"
not 3 < 2 or 3 > 2
# Gets evaluated as: (not False) or True
Out[65]:
True
In [66]:
# Expressions in parentheses are evaluated first
not (3 < 2 or 3 > 2)
# Gets evaluated as: not(False or True)
Out[66]:
False
In [67]:
# We can chain together as many logical operators as we like
False or False or True or False
Out[67]:
True

Comparisons can be chained together

In [68]:
x = 2
1 < x < 3
Out[68]:
True
In [69]:
# The code below does the same thing, but is more complicated
1 < x and x < 3
Out[69]:
True

Order of Evaluation

This is basically the same as PEMDAS: Parentheses, Exponents, Multiply/Divide, Add/Subtract

In Python, the order is:

  1. ()
  2. **
  3. *, /, %
  4. +, -

Operations at the same level are evaluated from left to right.

In [70]:
# The exponent is performed first, before the addition
3 + 6**2
Out[70]:
39
In [71]:
# Use brackets to force evaluation in a particular order
(3 + 6)**2
Out[71]:
81

IPython "magics"

In [72]:
# Execute all the code in the file "report1.py"
%run report1.py
Testing the Code Editor