cs 105: variables and expressions · excel: relative and absolute references every cell in excel...

37
CS 105: VARIABLES AND EXPRESSIONS Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ June 14, 2020

Upload: others

Post on 07-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

CS 105:

VARIABLES AND EXPRESSIONS

Max Fowler (Computer Science)

https://pages.github-dev.cs.illinois.edu/cs-105/web/

June 14, 2020

Page 2: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Video Series Two Topics

Objects, Literals

Types and Representation

Identifiers, Assignment, Immutability

Expressions and Operator Precedence, Module Imports

Excel Referencing and Moving Formulas Between Cells

Page 3: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Objects and Literals

Page 4: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

All data in Python is stored in an object

Objects have:a type

a value

Literals are textual descriptions, read by Python to make an object"Hello there!" is a ->

5 is a ->

23.32 is a ->

5

int

string literalinteger literal

floating point (float) literal

Page 5: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Three types we've met

Integers: whole numbers of arbitrary precision

Strings: e.g., our string literals like “Hello CS 105!”

Floating point numbers: approximations of real numbers

Types are important, because it specifies how to store data

Computers represent everything as a finite number of 1’s and 0’s

The type says how to interpret the 1’s and 0’s

Page 6: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Video Question – Objects have a WHAT and a

WHAT?

Page 7: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Types and Representations

Page 8: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

How ints are stored

Integers are usually used for counting things

Page 9: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

How strings are stored

Page 10: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Which of the following are considered ‘whitespace’?

A) Spaces

B) Tabs

C) Newlines

D) Spaces and Tabs

E) Spaces, Tabs, and Newlines

Page 11: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Which of the following are considered ‘whitespace’?

A) Spaces

B) Tabs

C) Newlines

D) Spaces and Tabs

E) Spaces, Tabs, and Newlines

In computer programming, whitespace is any character or series of characters that represent

horizontal or vertical space in typography. When rendered, a whitespace character does not

correspond to a visible mark, but typically does occupy an area on a page. --Wikipedia

Page 12: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

How strings are stored

Unicode can encode pretty much any character

Including many things that aren’t on your computer keyboard

How do we tell Python we want to use those characters?

Can specify the Unicode codepoint: e.g., 0394 is the Greek delta (Δ)

How do we distinguish a codepoint from a number?

Page 13: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Escaping

Treat slash (\) as a special character

\ means that the following characters should be interpreted differently

\u followed by a number is a code point

'\u0394’ is the Greek delta (Δ)

\” and \’ are quote characters that don’t end a string

\t encodes a tab

\n encodes a new line

\\ encodes a slash

Page 14: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Numbers beyond integers

Integers only represent whole numbers

Sometimes you need to represent numbers between

integers

Often when measuring things (lengths, speeds, etc.)

Real numbers:

Mathematically, there are an infinite number of numbers between each

integer

On computers, we can’t represent an infinite number of possible

numbers with a finite number of bits

Can only approximate real numbers

Page 15: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

How floats are stored

Like scientific notation: 6.02 x 1023

mantissa x 10exponent

Fixed-size mantissa: finite precision

Normally hidden by python

format(0.1, '.17f')

Fixed-size exponent: limited range

100.1 ** 200

Can specify in scientific notation

Page 16: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

We’ve now met three types:

Integers: whole numbers of arbitrary precision

Strings: e.g., our string literals like “Hello CS 105!”

Floating point numbers: approximations of real numbers

You can ask a value what its type is using:

type(expression)

You can convert between them with str() and

int() and float()

Page 17: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Video Question – How many visible characters are printed

with print('\\n\t\\t')?

Page 18: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Identifiers, Assignments, Immutability

Page 19: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

What's a variable?

A variable is effectively a name for an object

Names in Python have rules…

Begin with letter or underscore

Contain only letters, numbers, underscores

While not a rule, AVOID reserved words (key words)

Python recommends Snake Case

snek_case_uses_underscores

Page 20: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Danger Noodle is not the only case

art by

@allison_horst

Page 21: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Assignment?

Variables names are bound to values with

assignment statements

Structure: variable_name = expression

How does this work?

First, expression is evaluated to a value

Second, variable_name is bound to the value

Page 22: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Immutability

strings, ints, and floats are all immutable

Once an object has been created, it can’t be

changed

New ones must be made instead

Multiple variables can be bound to the same

object

If object is immutable, updating one variable

doesn’t affect the others

Page 23: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Video Question – What is the value of y after this

code executes?

x = 2

y = x + 3

x = 5

2

3

5

8

Page 24: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Expressions and Operator Precedence, Modules

Page 25: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Expressions

Any Python code fragment that produces a value

Can include:

Literals

Variables

Operators

Functions

Right-hand side of assignment can be arbitrary expression

Page 26: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Order of Operations

Parentheses () highest precedence

Exponentiation **

(unary) Positive, negative +x, -x

Multiplication, Division, Modulo *, /, %

Addition, Subtraction +, - lowest precedence

Left-to-right within a precedence level

Page 27: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Order of operations (full gory

details)

highest

lowest

Page 28: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Good style with expressions

Put a single space between every variable, operator, and number

this_is + a_readable – expression

Be generous with parentheses – almost no such thing as too much

Break up complicated expressions

total = num_machines * (cost_per_machine * (1 + tax_rate) + shipping rate)

machine_cost = num_machines * cost_per_machine

machine_cost_with_tax = machine_cost * (1 + tax_rate)

shipping_cost = num_machines * shipping_rate

total = machine_cost_with_tax + shipping_cost

Page 29: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Expression types

Result type generally depends on types of values in

expression:

an_integer + another_integer -> an integer

a_float + another_float -> a float

a_string + another_string -> a string

If you mix ints and floats, ints will be promoted to floats:

3.0 + 7 -> 3.0 + 7.0 -> 10.0

Generally can’t mix strings with either ints or floats

Page 30: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Division, Floor Division, and Modulo

Division operator (/) gives best approximation to true result

always results in a float

Floor Division (//) rounds down to closest whole number

Uses normal type rules for result

Modulo operator (%) performs a division and returns a

remainder

Uses normal type rules for result

For any numbers x and y, the following equality holds:

y = (y // x) * x + (y % x).

Page 31: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Floor division and modulo example

dollars = product_cost_in_pennies // 100

cents = product_cost_in_pennies % 100

31

Page 32: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Modules

Very few real computer programs are written from scratch

Too inefficient

Frequently use previously written code

Libraries

Python functions you previously wrote

We call both of these modules

32

Page 33: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Importing modules

import command puts module in your program’s namespace

Access functions and variables in module with qualified name:

math.sin(7.3)

Access documentation with help() and tab completion

33

Page 34: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Video Question – What is the value the following

expression?

-3 ** 2

-9

-8

8

9

Page 35: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Excel – cell referencing, formula dragging

Page 36: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Excel: Relative and Absolute References

Every cell in Excel has a name: e.g., C7 (column C, row 7)

When written in an Excel expression, this is a relative reference If moved/copied to another cell, it will change proportionally

If you move = 2 * C7 down two rows, it will become = 2 * C9

You can make absolute references by adding $ before row and/or column $C$7 moved anywhere stays $C$7

$C7 moved two down and two to the right becomes $C9

C$7 moved two down and two to the right becomes E$7

Page 37: CS 105: VARIABLES AND EXPRESSIONS · Excel: Relative and Absolute References Every cell in Excel has a name: e.g., C7 (column C, row 7) When written in an Excel expression, this is

Video Question – If a relative reference is drug

down in Excel, what changes?