python basics! - operators, expressions, computing · python basics! - operators, expressions,...

56
Python Basics! operators, expressions, computing CS101 Lecture #2 2016-09-28

Upload: others

Post on 19-Jul-2020

35 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Python Basics!operators, expressions, computing

CS101 Lecture #2

2016-09-28

Page 2: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Administrivia 1/43

Administrivia

Page 3: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Administrivia

Administrivia 2/43

Complete homework before THIS Friday at 6:00p.m.

Page 4: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Warmup Quiz 3/43

Warmup Quiz

Page 5: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Question #1

Warmup Quiz 4/43

A set of instructions executed by a computer toachieve a goal is called:A a processB a programC a procedureD an algorithm

Page 6: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Question #2

Warmup Quiz 5/43

A group of eight bits is called:A a nybbleB a chompC a byteD a gobble

Page 7: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Question #3

Warmup Quiz 6/43

Python is:A a high-level languageB a low-level language

Page 8: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Question #4

Warmup Quiz 7/43

Python is:A an interpreted languageB a compiled language

Page 9: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Elements of Programming 8/43

Elements of Programming

Page 10: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a literal?

Elements of Programming 9/43

Fixed value (noun)Represents data that doesn’t change(3 or 'firefly')

Page 11: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Executing a literal?

Elements of Programming 10/43

Page 12: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Executing a literal?

Elements of Programming 11/43

Page 13: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Executing a literal?

Elements of Programming 12/43

Page 14: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is an operator?

Elements of Programming 13/43

Manipulates data (verb)

Page 15: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Executing an operator?

Elements of Programming 14/43

Page 16: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

It needs a statement to make sense!

Elements of Programming 15/43

Page 17: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is an expression?

Elements of Programming 16/43

Combines literals and operators (phrase)

Produce a new value3 * 5100 - 23

Page 18: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is an expression?

Elements of Programming 16/43

Combines literals and operators (phrase)Produce a new value

3 * 5100 - 23

Page 19: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Executing an expression?

Elements of Programming 17/43

Page 20: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Executing an expression?

Elements of Programming 18/43

Page 21: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is an expression?

Elements of Programming 19/43

Can be arbitrarily complicated3 + 8*5 + 4 - 7/100

Page 22: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Question

Elements of Programming 20/43

1+ 1 ∗ 2 ?=

A 4B 3C Something else

Page 23: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Question

Elements of Programming 21/43

23+ 6/2− 4 ?=

A 22B 18C -9D Something else

Page 24: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Use parentheses!

Elements of Programming 22/43

23+ (6/2)− 4 is always clearer.

Page 25: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What are some other operators?

Elements of Programming 23/43

exponentiation, **

modulus, % (important)floor division, //

Page 26: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What are some other operators?

Elements of Programming 23/43

exponentiation, **modulus, % (important)

floor division, //

Page 27: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What are some other operators?

Elements of Programming 23/43

exponentiation, **modulus, % (important)floor division, //

Page 28: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What are some other operators?

Elements of Programming 24/43

bitwise OR, |bitwise XOR, ˆbitwise AND, &bitwise left shift, <<bitwise right shift, >>

Page 29: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Example

Elements of Programming 25/43

1 ˆ 2 ?=

A 0B 1C 2D 3

Page 30: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

So what?

Elements of Programming 26/43

The machine state hasn’t changed.

Programs are complex, and we need to rememberresults.

Page 31: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

So what?

Elements of Programming 26/43

The machine state hasn’t changed.Programs are complex, and we need to rememberresults.

Page 32: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we keep values around?

Elements of Programming 27/43

Page 33: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we keep values around?

Elements of Programming 28/43

Page 34: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we reuse values?

Elements of Programming 29/43

Low-level languages refer directly to memoryaddress:ADD DATA AT 10101101 11010100TO DATA AT 11010100 01001001STORE RESULT AT 00001101 01001110

Page 35: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a variable?

Elements of Programming 30/43

The solution: name memory locations!

Variables name a memory locationVariables store a valueThis value can change over time—it is aplaceholder.

Page 36: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a variable?

Elements of Programming 30/43

The solution: name memory locations!Variables name a memory location

Variables store a valueThis value can change over time—it is aplaceholder.

Page 37: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a variable?

Elements of Programming 30/43

The solution: name memory locations!Variables name a memory locationVariables store a value

This value can change over time—it is aplaceholder.

Page 38: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a variable?

Elements of Programming 30/43

The solution: name memory locations!Variables name a memory locationVariables store a valueThis value can change over time—it is aplaceholder.

Page 39: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What new operator do we need?

Elements of Programming 31/43

assignment, = (single equals sign)

Page 40: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we reuse values?

Elements of Programming 32/43

Page 41: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we reuse values?

Elements of Programming 33/43

Page 42: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we reuse values?

Elements of Programming 34/43

Page 43: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we reuse values?

Elements of Programming 35/43

Page 44: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

How do we reuse values?

Elements of Programming 36/43

Page 45: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Example

Elements of Programming 37/43

What value is stored in the variable x?x = 17 + 7*9A 3B 31C 55D 78

Page 46: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Example

Elements of Programming 38/43

What value is stored in the variable x?x = 17 + 7*9x = 3A 0B 1C 2D 3

Page 47: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a statement?

Elements of Programming 39/43

A statement changes the state of the computer(sentence)

Example: an assignment

Page 48: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a statement?

Elements of Programming 39/43

A statement changes the state of the computer(sentence)Example: an assignment

Page 49: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a program?

Elements of Programming 40/43

Programs consist of series of statements:

A script is a file containing a series of Pythonstatement.A notebook (as we use in the lab) alsocollects series of Python statements.These are stored in text (there’s no magic,just text).

Each instruction is executed in order from top tobottom—together, these statements make up aprogram.

Page 50: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a program?

Elements of Programming 40/43

Programs consist of series of statements:A script is a file containing a series of Pythonstatement.

A notebook (as we use in the lab) alsocollects series of Python statements.These are stored in text (there’s no magic,just text).

Each instruction is executed in order from top tobottom—together, these statements make up aprogram.

Page 51: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a program?

Elements of Programming 40/43

Programs consist of series of statements:A script is a file containing a series of Pythonstatement.A notebook (as we use in the lab) alsocollects series of Python statements.

These are stored in text (there’s no magic,just text).

Each instruction is executed in order from top tobottom—together, these statements make up aprogram.

Page 52: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a program?

Elements of Programming 40/43

Programs consist of series of statements:A script is a file containing a series of Pythonstatement.A notebook (as we use in the lab) alsocollects series of Python statements.These are stored in text (there’s no magic,just text).

Each instruction is executed in order from top tobottom—together, these statements make up aprogram.

Page 53: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

What is a program?

Elements of Programming 40/43

Programs consist of series of statements:A script is a file containing a series of Pythonstatement.A notebook (as we use in the lab) alsocollects series of Python statements.These are stored in text (there’s no magic,just text).

Each instruction is executed in order from top tobottom—together, these statements make up aprogram.

Page 54: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Our first program

Elements of Programming 41/43

x = 10y = x ** 2y = y + y

Page 55: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Reminders 42/43

Reminders

Page 56: Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions, computing Author: CS101 Lecture #2 Created Date: 9/26/2016 9:37:11 PM

Reminders

Reminders 43/43

Homework #1 due Friday, Sept. 30, 6:00 p.m.