HW01 Connections - Expressions, values, types, operators, order of evaluation, variables, sequencing

c8 touched on values, expressions, call expressions

Let's get clear on values, operators, precedence, and types

``````

In [ ]:

5+3

``````
``````

In [ ]:

5-3

``````
``````

In [ ]:

5*3

``````
``````

In [ ]:

-47

``````
``````

In [ ]:

# This is a comment.  It doesn't 'do anything', but ...

# integers are like the integers you know, floats are like reals
5/3

``````
``````

In [ ]:

# if you want to stick to ints, be explicit about it
5//3

``````
``````

In [ ]:

# modulo or remainder
5%3

``````
``````

In [ ]:

# what do we mean 'rounds down'
2//3

``````
``````

In [ ]:

# we mean DOWN, towards negative infinity, not towards zero
-2//3

``````
``````

In [ ]:

-5//3

``````
``````

In [ ]:

# floats - they don't look like integers, and they aren't
3.141592

``````
``````

In [ ]:

# they behave like you would expect when you mix them
2*3.141592

``````
``````

In [ ]:

3.141592*3.141592

``````
``````

In [ ]:

# floats can have the same value as ints, but they are still floats
3.141592/3.141592

``````
``````

In [ ]:

# exponentiation
2**3

``````
``````

In [ ]:

# roots
2**(1/2)

``````

What you should know:

• two types: integers and floats
• arithemetic operators: (+,-,*,/,//,%)

Expressions

follow 'operator precedence' The operations are performed one at at time in a specific order. Parentheses are used to specify order, just like in math class.

``````

In [ ]:

# equal precedence, left to right
2 + 3 - 4 + 5

``````
``````

In [ ]:

# order matters.  Parentheses are your friend.
2 + 3 - (4 + 5)

``````
``````

In [ ]:

# make it explicit
(((2 + 3) - 4) + 5)

``````
``````

In [ ]:

2 + 3 * 4

``````
``````

In [ ]:

# * binds more tightly than + or -
2 + (3 * 4)

``````
``````

In [ ]:

(2 + 3) * 4

``````
``````

In [ ]:

2 + 3/4 * 5

``````
``````

In [ ]:

2 + 3*4 / 5

``````
``````

In [ ]:

# left to right for same precedence
2 + ((3*4)/5)

``````
``````

In [ ]:

# can you always tell?
2 + 3 * (4/5)

``````
``````

In [ ]:

2 + (3*(4/5))

``````
``````

In [ ]:

2 + (3*4)/5

``````

Call expressions

All of these operators are also functions. They have names. And those names reside in a namespace. Let's bring some of these operators into our namespace

``````

In [ ]:

from operator import add, sub, mul, truediv

``````
``````

In [ ]:

``````

Variables

Introduced in c8 as "names for values". Assignment binds a value to a variable.

``````

In [ ]:

pie = 3.1415

``````
``````

In [ ]:

# use variables just like values
pie*2

``````
``````

In [ ]:

# here's a better one
from math import pi
pi

``````
``````

In [ ]:

pi-pie

``````

Order of evalation: one step at a time, in sequence

``````

In [ ]:

# What is the order of evaluation?  Use variables to do this one step
# at a time
2 + 3/4 * 5

``````
``````

In [ ]:

x1 = 3/4
x2 = x1 * 5
x3 = 2 + x2
x3

``````
``````

In [ ]:

# but, all the variables remain.
# Not just the value returned by the last expression
x1

``````
``````

In [ ]:

# what is the order of evaluation of
x1*x3+x1*x2

``````
``````

In [ ]:

# give each step a name

``````
``````

In [ ]:

# and variables and call all go together as you'd expect
y1

``````
``````

In [ ]:

# so humpty dumpty can put it all back together again
# 2 + 3/4 * 5

``````

Comparison operators: V x V => Bool

``````

In [ ]:

2 >= 3

``````
``````

In [ ]:

2 < 3

``````

So far everything you've seen looks the same as the arithmetic you did in elementary school. But back then you used the equals sign in two different ways without even knowing it. We need to be a little more careful.

When we mean the operator that tests for equality, we use ==.

``````

In [ ]:

# Are these two the same?
2 == 3

``````
``````

In [ ]:

# Are they 'not equal'?
2 != 3

``````
``````

In [ ]:

3.141592 == 3.141592

``````
``````

In [ ]:

# Nice to be able to do this too
1 == 1.0

``````
``````

In [ ]:

# what's the order here?
1 / 2 == 3

``````
``````

In [ ]:

# nonsensical expressions do funny things
1 / (2 == 3)

``````
``````

In [ ]:

# makes more sense here
1/0

``````
``````

In [ ]:

# what is the difference between asignment and equality?
x = 2 == 3

``````
``````

In [ ]:

x

``````

``````

In [ ]:

# this look right?
1/11

``````
``````

In [ ]:

# floats are really similar to reals
11*(1/11)

``````
``````

In [ ]:

1/11 == 0.09090909090909091

``````
``````

In [ ]:

11*0.09090909090909091

``````
``````

In [ ]:

0.0909090909090909090909

``````
``````

In [ ]:

# but not quite
(2**(1/2))**2

``````
``````

In [ ]:

# Be a little careful with floats
(2**(1/2))**2 == 2

``````

Values all have types. Just ask them.

``````

In [ ]:

type(2)

``````
``````

In [ ]:

type(2.0)

``````
``````

In [ ]:

2 == 2.0

``````
``````

In [ ]:

type(2 == 2.0)

``````
``````

In [ ]:

type("cool")

``````

Are all values numbers??? No. You can represent anything in the computer. How about strings.

So what operators are defined on strings?

``````

In [ ]:

# Concatenation
"cal"+"rocks"

``````
``````

In [ ]:

# Replication
"cal"*3

``````
``````

In [ ]:

# let's try something weird
"cal"*pie

``````
``````

In [ ]:

# string equality,
"cal" == "cal"

``````
``````

In [ ]:

"cal" == "rocks"

``````
``````

In [ ]:

# regardless of the kind of quotes
"cal" == 'cal'

``````
``````

In [ ]:

# lexicographic order
'cal' < 'rocks'

``````
``````

In [ ]:

# case matters
'Cal' == 'cal'

``````
``````

In [ ]:

# Uppercase before lower case
'Cal' < 'cal'

``````
``````

In [ ]:

# go on, it won't break anything
"cal"*"rocks"

``````
``````

In [ ]:

# These are really different, but we can still ask if they are the same - they aren't
0 == "hello world"

``````
``````

In [ ]:

# Not like C (what's 'C'? another programming language)
0 == ""

``````
``````

In [ ]:

False == ""

``````
``````

In [ ]:

# Not everything is comparable
0 < "cal"

``````

Logical operations make sense on boolean values. And you can build expressions

``````

In [ ]:

1 < 2 and 2 < 3

``````
``````

In [ ]:

1 < 2 or 2 > 3

``````
``````

In [ ]:

1 != 2 or "cal" == "rocks"

``````
``````

In [ ]:

not "cal" == "rocks"

``````
``````

In [ ]:

not "cal" != "rocks"

``````

Strings are a very special type of sequence. There are others. We call these data structures. For example, tuples, lists, arrays.

``````

In [ ]:

# tuples - created by separating values by commas (often in parens)
1,2

``````
``````

In [ ]:

type((1,2))

``````
``````

In [ ]:

# You can even assign tuples
a,b = 1,2

``````
``````

In [ ]:

a

``````
``````

In [ ]:

# lists - are in brackets
[1,2,3]

``````
``````

In [ ]:

type([1,2,3])

``````

Every datatype has a set of operators defined on it - not just values. Try out some operators on sequences.

``````

In [ ]:

# operators: concatenation
(1,2)+(3,4)

``````
``````

In [ ]:

[1,2,3]+[4,5]

``````
``````

In [ ]:

# replication
(1,2)*3

``````
``````

In [ ]:

# does this make any sense?
(1,2)*(3,4)

``````
``````

In [ ]:

[1,2]+(3,4)

``````
``````

In [ ]:

# conversion
list((1,2))

``````

Variables are defined in a namespace

``````

In [ ]:

from math import pi

``````
``````

In [ ]:

``````
``````

In [ ]:

# assignment defines the variable
pi = 3.141592

``````
``````

In [ ]:

# Sequence of statements
# They happen one after the other
# Each can use the variables assigned before
r = 3
c = 2*pi*r
# and an expression
c

``````
``````

In [ ]:

# assignment
L = [1,2,3]

``````
``````

In [ ]:

# use it like any other variable
L

``````
``````

In [ ]:

T = (1,2,3)
T

``````
``````

In [ ]:

# comparisons - element by element
L == L

``````
``````

In [ ]:

L < [1,2,4]

``````
``````

In [ ]:

L == T

``````
``````

In [ ]:

L == [1,2,3]

``````
``````

In [ ]:

L > [1,2]

``````
``````

In [ ]:

L > 

``````