CS 105: VARIABLES AND EXPRESSIONS Max Fowler (Computer Science) - - PowerPoint PPT Presentation

cs 105
SMART_READER_LITE
LIVE PREVIEW

CS 105: VARIABLES AND EXPRESSIONS Max Fowler (Computer Science) - - PowerPoint PPT Presentation

CS 105: VARIABLES AND EXPRESSIONS Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ June 14, 2020 Video Series Two Topics Objects, Literals Types and Representation Identifiers, Assignment,


slide-1
SLIDE 1

CS 105: VARIABLES AND EXPRESSIONS

Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/

June 14, 2020

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Objects and Literals

slide-4
SLIDE 4

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

  • bject

"Hello there!" is a -> 5 is a -> 23.32 is a ->

5 int

string literal integer literal floating point (float) literal

slide-5
SLIDE 5

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

slide-6
SLIDE 6

Video Question – Objects have a WHAT and a WHAT?

slide-7
SLIDE 7

Types and Representations

slide-8
SLIDE 8

How ints are stored

 Integers are usually used for counting things

slide-9
SLIDE 9

How strings are stored

slide-10
SLIDE 10

Which of the following are considered ‘whitespace’?

A) Spaces B) Tabs C) Newlines D) Spaces and Tabs E) Spaces, Tabs, and Newlines

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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?

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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()

slide-17
SLIDE 17

Video Question – How many visible characters are printed with print('\\n\t\\t')?

slide-18
SLIDE 18

Identifiers, Assignments, Immutability

slide-19
SLIDE 19

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

slide-20
SLIDE 20

Danger Noodle is not the only case

art by @allison_horst

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

  • bject

If object is immutable, updating one variable

doesn’t affect the others

slide-23
SLIDE 23

Video Question – What is the value of y after this code executes?

x = 2 y = x + 3 x = 5

 2  3  5  8

slide-24
SLIDE 24

Expressions and Operator Precedence, Modules

slide-25
SLIDE 25

Expressions

 Any Python code fragment that produces a value  Can include:

 Literals  Variables  Operators  Functions

 Right-hand side of assignment can be arbitrary expression

slide-26
SLIDE 26

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

slide-27
SLIDE 27

Order of operations (full gory details)

highest lowest

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

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).

slide-31
SLIDE 31

Floor division and modulo example

dollars = product_cost_in_pennies // 100 cents = product_cost_in_pennies % 100

31

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Video Question – What is the value the following expression?

  • 3 ** 2

 -9  -8  8  9

slide-35
SLIDE 35

Excel – cell referencing, formula dragging

slide-36
SLIDE 36

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

slide-37
SLIDE 37

Video Question – If a relative reference is drug down in Excel, what changes?