are you sitting next to someone to talk to for the clicker ... · are you sitting next to someone...

39
Are you sitting next to someone to talk to for the clicker questions? 1

Upload: others

Post on 31-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Are you sitting next to someone to talk to for the

clicker questions?

1

Page 2: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Variables and Expressions

Craig Zilles (Computer Science)Al Harris (Computer Science)

September 6, 2019

https://go.illinois.edu/cs105fa19

Page 3: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Today1. Objects, literals2. Types and representation

• Integers• Strings, Unicode, and Escaping• Reals, floating point

3. Identifiers, assignment, immutability4. Expressions and Operator Precedence

• Division, Floor division, and modulo operator

5. Modules and Importing6. Excel: relative and absolute references

• Moving formulas between cells

3

Page 4: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

You are in good company (other muddy pts.)

• I am still new to programming, so the whole terminology will definitely take some time to getting use to. A lot of the grammar in coding can be very confusing because coding has its own way to read and write.

• I honestly think that the hardest part of the chapter was all of the errors. As I was doing the practice problems and challenge problems, the program would tell me I was wrong, and it would take me forever to find my mistake! I would really like to review how to find errors, especially since one little period or quote can ruin everything!! Knowing the meaning of each of the error symbols would also be really helpful!

4

Page 5: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

The Python interpreter is your friend!

Use it to play around with the language5

Page 6: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

All data in Python is stored in an object

• Every object has:• A type• A value

• Literals are textual descriptions, read by Python to make objects

• “hi there”

• 17

• -203.5974

6

Page 7: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

• 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

7

Page 8: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

How ints are stored

• Integers are usually used for counting things

8

int 1 000110101

int 2 000101011 110101001

Type Numberof chunks

Chunks(Store the number in binary)

Small number

Large number

Page 9: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

How strings are stored

9

String 6 001000011 001010011

TypeNumber ofcharacters

Characters(Stored using Unicode encoding)

000100000 000110001 000110000 000110101‘CS 105’

C S 1 0 5

Page 10: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Which of the following are considered ‘whitespace’?

A) SpacesB) TabsC) NewlinesD) Spaces and TabsE) Spaces, Tabs, and Newlines

10

Page 11: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Which of the following are considered ‘whitespace’?

A) SpacesB) TabsC) NewlinesD) Spaces and TabsE) Spaces, Tabs, and Newlines

11

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: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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?

12

String 6 001000011 001010011

TypeNumber ofcharacters

Characters(Stored using Unicode encoding)

000100000 000110001 000110000 000110101‘CS 105’

C S 1 0 5

Page 13: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

13

Page 14: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

How many visible characters are printed?

• print('\\n\t\\t')

A) 1B) 2C) 3D) 4E) 5

14

Page 15: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

15

Page 16: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

16

Float -3.76341 +22

Type Mantissa Exponent

Can specify in scientific notation

Page 17: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Which isn’t a valid Python literal

A) 1.000001B) 1E-7C) 1,097D) -3.00E) '\'\'\'' # Consists only of single quotes and slashes

17

Page 18: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

18

Page 19: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Variables

• Useful to give names to objects

• Names in Python have to follow certain rules:• Begin with letter or underscore• Contains only letters, numbers, or underscores• Avoid reserved words (also known as key words)

• Python recommended style: Snake Case• lower_case_words_separated_by_underscores

19

Page 20: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Which are legal Python names?

1. ______2. 12monkeys3. m0nk3yM4n

A. 1 and 2B. 1 and 3C. 2 onlyD. 2 and 3E. 1, 2, and 3

20

Page 21: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Assignment

• Variable names are bound to values with assignment statements

• Structure: variable_name = expression

• What happens:1. expression evaluated to compute a value2. variable_name is bound to the value

21

Page 22: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

What is the value of y after this code executes

x = 2y = x + 3x = 5

A) 2B) 3C) 5D) 8E) 10

22

Page 23: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

What is the value of y after this code executes

x = 7y = xx = x + 2

A) 2B) 5C) 7D) 9E) None of the above

23

Page 24: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

24

Page 25: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Garbage Collection

• If an object isn’t bound to any variable, the Python interpreter gets rid of it.

• Frees up space for new objects

25

Page 26: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Logistics• Lab next week: Spatial skills development • Homework on PL:

• More points available than you need• Report any problems with “Report an error in this question”• Code reading exercises using natural language processing

• Exam 0 next week: Sign up on https://cbtf.engr.illinois.edu/sched/• Meant to get comfortable in the CBTF• Drawn almost completely from pre-lecture 1 & 2 and HW 1 & 2

• For late adds (and everyone)• We’ll allow late submission of everything up until 9/10 10pm for 70%

credit

26

Page 27: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Logistics, cont.• Need help?

• Sending me email is probably not the answer• Questions about course content?

• Search Piazza (first) or ask question on Piazza (second)• Office Hours: Wohlers Hall Computer Lab

• Today: noon – 4:30pm• Sunday: 2:30pm – 7pm

• Questions about course policies, grading -> Piazza• Questions/documents about DRES, excused absences?

[email protected] (Jillian, our course administrator)

27

Page 28: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Expressions

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

• Literals• Variables• Operators• Functions

• Right-hand side of assignment can be arbitrary expression

28

Page 29: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

29

Page 30: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

What is the value of this expression?

-3 ** 2

A) -9B) -8C) 8D) 9E) None of the above

31

Page 31: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Good Style with Expressions

• Put a single space between every variable, operator, and number• this_is + a_readable – expression

• Break up complicated expressions

• vs

• Be generous with parentheses32

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

machine_cost = num_machines * cost_per_machinemachine_cost_with_tax = machine_cost * (1 + tax_rate)shipping_cost = num_machines * shipping_ratetotal = machine_cost_with_tax + shipping_cost

Page 32: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

What would happen…

• if I type the following into a brand new Python interpreter: x + y

A) nothingB) SyntaxErrorC) NameErrorD) ValueErrorE) TypeError

33

Page 33: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

34

Page 34: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

What (if any) kind of error is in this program?

value = input("Input your favorite number!\n")print("Your new favorite number is", value + 1)

A) No errorB) SyntaxErrorC) NameErrorD) ValueErrorE) TypeError

35

Page 35: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

36

Page 36: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

Floor division and modulo example

dollars = product_cost_in_pennies // 100cents = product_cost_in_pennies % 100

37

Page 37: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

38

Page 38: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

39

Page 39: Are you sitting next to someone to talk to for the clicker ... · Are you sitting next to someone to talk to for the clicker questions? 1. Variables and Expressions Craig Zilles (Computer

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

40