Variables Expressions vs Statements Expression Statement - - PowerPoint PPT Presentation

variables expressions vs statements
SMART_READER_LITE
LIVE PREVIEW

Variables Expressions vs Statements Expression Statement - - PowerPoint PPT Presentation

Mini-Lecture 3 Variables Expressions vs Statements Expression Statement Represents something Does something Python evaluates it Python executes it End result is a value Need not result in a value Examples: Examples:


slide-1
SLIDE 1

Variables

Mini-Lecture 3

slide-2
SLIDE 2

Expressions vs Statements

Expression

  • Represents something

§ Python evaluates it § End result is a value

  • Examples:

§ 2.3 § (3+5)/4

Statement

  • Does something

§ Python executes it § Need not result in a value

  • Examples:

§ print('Hello') § import sys

8/29/18 Variables & Assignments 2

Will see later this is not a clear cut separation

Literal Complex Expression

slide-3
SLIDE 3

Variables (Section 2.1)

  • A variable is

§ a named memory location (box), § a value (in the box)

  • Examples
  • Variable names must start with a letter

§ So 1e2 is a float, but e2 is a variable name

8/29/18 Variables & Assignments 3

5 x Variable x, with value 5 (of type int) 20.1 area Variable area, w/ value 20.1 (of type float)

slide-4
SLIDE 4

Variables and Assignment Statements

  • Variables are created by assignment statements

§ Create a new variable name and give it a value x = 3

  • This is a statement, not an expression

§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)

  • Assignment statements can have expressions in them

§ These expressions can even have variables in them x = x + 2

8/29/18 Variables & Assignments 4

the value the variable the expression the variable

slide-5
SLIDE 5

Execute the Statement: x = x + 2

  • Draw variable x on piece of paper:
  • Step 1: evaluate the expression x + 2

§ For x, use the value in variable x § Write the expression somewhere on your paper 5 x

8/29/18 Variables & Assignments 5

slide-6
SLIDE 6

Execute the Statement: x = x + 2

  • Draw variable x on piece of paper:
  • Step 1: evaluate the expression x + 2

§ For x, use the value in variable x § Write the expression somewhere on your paper

  • Step 2: Store the value of the expression in x

§ Cross off the old value in the box § Write the new value in the box for x 5 x

8/29/18 Variables & Assignments 6

slide-7
SLIDE 7

Which One is Closest to Your Answer?

A: B:

8/29/18 Variables & Assignments 7

C: D:

¯\_()_/¯

5 x 7

x

5 x 5 x x 7 x 7 x

slide-8
SLIDE 8

Which One is Closest to Your Answer?

A: B:

8/29/18 Variables & Assignments 8

C:

5 x 7

x

5 x 5 x x 7 x 7 x

  • x = x + 2
slide-9
SLIDE 9

Assignment Statements

  • Make new variables:

interestRate = 4

8/29/18 Variables & Assignments 9

  • Change existing variables:

x = 3.0 * x + 1.0 4 interestRate 5 x 7

x

5 x 7

x

4 interestRate 5 x 7

x

4 interestRate 5 x 7

x

22.0

x

slide-10
SLIDE 10

But Beware of Misspellings

intrestRate = x + interestRate

8/29/18 Variables & Assignments 10

4 interestRate 5 x 7

x

22.0

x

4 interestRate 5 x 7

x

22.0

x

26.0 intrestRate

slide-11
SLIDE 11

But Beware of Misspellings

intrestRate = x + interestRate

8/29/18 Variables & Assignments 11

4 interestRate 5 x 7

x

22.0

x

4 interestRate 5 x 7

x

22.0

x

26.0 intrestRate

slide-12
SLIDE 12

Dynamic Typing

  • Python is a dynamically typed language

§ Variables can hold values of any type § Variables can hold different types at different times § Use type(x) to find out the type of the value in x § Use names of types for conversion, comparison

  • The following is acceptable in Python:

>>> x = 1 >>> x = x / 2.0

  • Alternative is a statically typed language (e.g. Java)

§ Each variable restricted to values of just one type ç x contains an int value ç x now contains a float value

type(x) == int x = float(x) type(x) == float

8/29/18 Variables & Assignments 12

slide-13
SLIDE 13

Dynamic Typing

  • Often want to track the type in a variable

§ What is the result of evaluating x / y? § Depends on whether x, y are int or float values

  • Use expression type(<expression>) to get type

§ type(2) evaluates to <type 'int'> § type(x) evaluates to type of contents of x

  • Can use in a boolean expression to test type

§ type('abc') == str evaluates to True

8/29/18 Variables & Assignments 13