Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions,...

Post on 19-Jul-2020

35 views 0 download

Transcript of Python Basics! - operators, expressions, computing · Python Basics! - operators, expressions,...

Python Basics!operators, expressions, computing

CS101 Lecture #2

2016-09-28

Administrivia 1/43

Administrivia

Administrivia

Administrivia 2/43

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

Warmup Quiz 3/43

Warmup Quiz

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

Question #2

Warmup Quiz 5/43

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

Question #3

Warmup Quiz 6/43

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

Question #4

Warmup Quiz 7/43

Python is:A an interpreted languageB a compiled language

Elements of Programming 8/43

Elements of Programming

What is a literal?

Elements of Programming 9/43

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

Executing a literal?

Elements of Programming 10/43

Executing a literal?

Elements of Programming 11/43

Executing a literal?

Elements of Programming 12/43

What is an operator?

Elements of Programming 13/43

Manipulates data (verb)

Executing an operator?

Elements of Programming 14/43

It needs a statement to make sense!

Elements of Programming 15/43

What is an expression?

Elements of Programming 16/43

Combines literals and operators (phrase)

Produce a new value3 * 5100 - 23

What is an expression?

Elements of Programming 16/43

Combines literals and operators (phrase)Produce a new value

3 * 5100 - 23

Executing an expression?

Elements of Programming 17/43

Executing an expression?

Elements of Programming 18/43

What is an expression?

Elements of Programming 19/43

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

Question

Elements of Programming 20/43

1+ 1 ∗ 2 ?=

A 4B 3C Something else

Question

Elements of Programming 21/43

23+ 6/2− 4 ?=

A 22B 18C -9D Something else

Use parentheses!

Elements of Programming 22/43

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

What are some other operators?

Elements of Programming 23/43

exponentiation, **

modulus, % (important)floor division, //

What are some other operators?

Elements of Programming 23/43

exponentiation, **modulus, % (important)

floor division, //

What are some other operators?

Elements of Programming 23/43

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

What are some other operators?

Elements of Programming 24/43

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

Example

Elements of Programming 25/43

1 ˆ 2 ?=

A 0B 1C 2D 3

So what?

Elements of Programming 26/43

The machine state hasn’t changed.

Programs are complex, and we need to rememberresults.

So what?

Elements of Programming 26/43

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

How do we keep values around?

Elements of Programming 27/43

How do we keep values around?

Elements of Programming 28/43

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

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.

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.

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.

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.

What new operator do we need?

Elements of Programming 31/43

assignment, = (single equals sign)

How do we reuse values?

Elements of Programming 32/43

How do we reuse values?

Elements of Programming 33/43

How do we reuse values?

Elements of Programming 34/43

How do we reuse values?

Elements of Programming 35/43

How do we reuse values?

Elements of Programming 36/43

Example

Elements of Programming 37/43

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

Example

Elements of Programming 38/43

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

What is a statement?

Elements of Programming 39/43

A statement changes the state of the computer(sentence)

Example: an assignment

What is a statement?

Elements of Programming 39/43

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

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.

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.

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.

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.

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.

Our first program

Elements of Programming 41/43

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

Reminders 42/43

Reminders

Reminders

Reminders 43/43

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