Variables & Assignment
Lecture 2
Variables & Assignment Announcements for Today If Not Done - - PowerPoint PPT Presentation
Lecture 2 Variables & Assignment Announcements for Today If Not Done Already Lab 1 Please stay in your section Enroll in Piazza If you drop, you are stuck Sign into CMS E-mail conflicts to Jenna jls478@cornell.edu
Lecture 2
Announcements for Today
If Not Done Already
§ Fill out the Survey § Complete AI Quiz
§ Chapter 1 (browse) § Chapter 2 (in detail)
Lab 1
§ If you drop, you are stuck § E-mail conflicts to Jenna § jls478@cornell.edu § Will review by next week
§ Complete in online system § Show at start of next lab
8/27/18 Variables & Assignments 2
Helping You Succeed in this Class
§ Daily office hours (see website) with consultants § Very useful when working on assignments
§ Runs parallel to this class – completely optional § See website; talk to advisors in Olin 167.
§ Go here first before sending question in e-mail
§ Available outside Call Auditorium between lectures
8/27/18 Variables & Assignments 3
Labs vs. Assignments
Labs
§ Always S/U § Try again if not finished
§ Can miss up to 2 labs
§ After that, grade reduced
§ Simple, but take time
Assignments
§ First one due Sep. 18
§ Assign points out of 100
§ Resubmit until perfect grade
§ Graphics, game design
8/27/18 Variables & Assignments 4
ACCEL Labs
8/27/18 Variables & Assignments 5
Academic Integrity
§ Claiming the work of others as your own § This is an Academic Integrity violation
§ Do not listen to (non-staff) upperclassmen § Look at the course website for the new details
§ Must complete successfully to stay in class
8/27/18 Variables & Assignments 6
iClickers
§ http://atcsupport.cit.cornell.edu/pollsrvc/
§ http://pollinghelp.cit.cornell.edu/iclicker-basics/
§ Find these links on the course webpage § Click Texts/iClickers § Look under “iClickers”
8/27/18 Variables & Assignments 7
Warm-Up: Using Python
8/27/18 Variables & Assignments 8
Type: Set of values and the operations on them
§ Values: integers § Ops: +, –, *, //, %, **
§ Values: real numbers § Ops: +, –, *, /, **
§ Values: True and False § Ops: not, and, or
§ Values: string literals
§ Ops: + (concatenation)
Will see more types in a few weeks
8/27/18 Variables & Assignments 9
Converting Values Between Types
§ float(2) converts value 2 to type float (value now 2.0) § int(2.6) converts value 2.6 to type int (value now 2) § Explicit conversion is also called “casting”
§ Example: 1/2.0 evaluates to 0.5 (casts 1 to float)
§ Narrowing conversions cause information to be lost § Example: float(int(2.6)) evaluates to 2.0
8/27/18 Variables & Assignments 10
Operator Precedence
§ 2*(1+3) § 2*1 + 3
§ Parentheses make the order explicit § What happens when there are no parentheses?
processes operators in absence of parentheses
8/27/18 Variables & Assignments 11
Operator Precedence
§ 2*(1+3) § 2*1 + 3
§ Parentheses make the order explicit § What happens when there are no parentheses?
processes operators in absence of parentheses
add, then multiply multiply, then add
8/27/18 Variables & Assignments 12
Precedence of Python Operators
§ Parentheses highest § Logical ops lowest
§ Read “ties” left to right § Example: 1/2*3 is (1/2)*3
8/27/18 Variables & Assignments 13
Expressions vs Statements
Expression
§ Python evaluates it § End result is a value
§ 2.3 § (3+5)/4
Statement
§ Python executes it § Need not result in a value
§ print('Hello') § import sys Will see later this is not a clear cut separation
Value Complex Expression
8/27/18 Variables & Assignments 14
Variables (Section 2.1)
§ is a named memory location (box) § contains a value (in the box) § can be used in expressions
8/27/18 Variables & Assignments 15
5 x
Variable x, with value 5 (of type int)
20.1 area
Variable area, w/ value 20.1 (of type float)
Variable names must start with a letter (or _).
Variables (Section 2.1)
§ is a named memory location (box) § contains a value (in the box) § can be used in expressions
8/27/18 Variables & Assignments 16
5 x
Variable x, with value 5 (of type int)
20.1 area
Variable area, w/ value 20.1 (of type float)
Variable names must start with a letter (or _). The type belongs to the value, not to the variable.
Variables (Section 2.1)
§ is a named memory location (box) § contains a value (in the box) § can be used in expressions
5 x
Variable x, with value 5 (of type int)
20.1 area
Variable area, w/ value 20.1 (of type float)
Variable names must start with a letter (or _). The type belongs to the value, not to the variable. The value in the box is then used in evaluating the expression.
8/27/18 Variables & Assignments 17
Variables (Section 2.1)
§ is a named memory location (box) § contains a value (in the box) § can be used in expressions
5 x
Variable x, with value 5 (of type int)
20.1 area
Variable area, w/ value 20.1 (of type float)
Variable names must start with a letter (or _). The type belongs to the value, not to the variable. The value in the box is then used in evaluating the expression.
8/27/18 Variables & Assignments 18
1e2 is a float, but e2 is a variable name
Variables and Assignment Statements
§ Create a new variable name and give it a value x = 5
§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)
§ These expressions can even have variables in them x = x + 2
Two steps to execute an assignment:
Variables and Assignment Statements
§ Create a new variable name and give it a value x = 5
§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)
§ These expressions can even have variables in them x = x + 2
Two steps to execute an assignment:
the value the variable
Variables and Assignment Statements
§ Create a new variable name and give it a value x = 5
§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)
§ These expressions can even have variables in them x = x + 2 x
Two steps to execute an assignment:
the value the variable
Variables and Assignment Statements
§ Create a new variable name and give it a value x = 5
§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)
§ These expressions can even have variables in them x = x + 2 x 5
Two steps to execute an assignment:
the value the variable
Variables and Assignment Statements
§ Create a new variable name and give it a value x = 5
§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)
§ These expressions can even have variables in them x = x + 2 the value the variable the expression the variable x 5
Two steps to execute an assignment:
Variables and Assignment Statements
§ Create a new variable name and give it a value x = 5
§ Tells the computer to DO something (not give a value) § Typing it into >>> gets no response (but it is working)
§ These expressions can even have variables in them x = x + 2 x 5
Two steps to execute an assignment:
“gets”
the value the variable the expression the variable
Execute the Statement: x = x + 2
5 x
8/27/18 Variables & Assignments 25
Execute the Statement: x = x + 2
§ For x, use the value in variable x § Write the expression somewhere on your paper 5 x
8/27/18 Variables & Assignments 26
Execute the Statement: x = x + 2
§ For x, use the value in variable x § Write the expression somewhere on your paper
§ Cross off the old value in the box § Write the new value in the box for x 5 x
8/27/18 Variables & Assignments 27
Execute the Statement: x = x + 2
§ For x, use the value in variable x § Write the expression somewhere on your paper
§ Cross off the old value in the box § Write the new value in the box for x
neighbor, discuss it if you did something different.
5 x
8/27/18 Variables & Assignments 28
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 29
C: D:
5 x 7
x
5 x 5 x x 7 x 7 x
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 30
C:
5 x 7
x
5 x 5 x x 7 x 7 x
Execute the Statement: x = 3.0 * x + 1.0
5 x 7
8/27/18 Variables & Assignments 31
x
Execute the Statement: x = 3.0 * x + 1.0
§ Step 1: Evaluate the expression 3.0 * x + 1.0 § Step 2: Store its value in x 5 x 7
8/27/18 Variables & Assignments 32
x
Execute the Statement: x = 3.0 * x + 1.0
§ Step 1: Evaluate the expression 3.0 * x + 1.0 § Step 2: Store its value in x
neighbor, discuss it if you did something different.
5 x 7
8/27/18 Variables & Assignments 33
x
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 34
C: D:
5 x 7
x
5 x 5 x x 22.0 x 22.0 x 22.0
x
7
x
7
x
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 35
C:
5 x 7
x
5 x 5 x x 22.0 x 22.0 x 22.0
x
7
x
7
x
Execute the Statement: x = 3.0 * x + 1.0
§ Step 1: Evaluate the expression 3.0 * x + 1.0 § Step 2: Store its value in x
§ Performing it is called executing the command § Command requires both evaluate AND store to be correct § Important mental model for understanding Python 5 x 7 22.0
8/27/18 Variables & Assignments 36
x x
Exercise: Understanding Assignment
interestRate = x / interestRate
neighbor, discuss it if you did something different.
4 interestRate 5 x 7 22.0
8/27/18 Variables & Assignments 37
x x
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 38
C: D:
4 interestRate 5 x 7 22.0
x x
5.5
x
5.5
x
4 interestRate 5 x 7 22.0
x x x
4 interestRate 5 x 7 22.0
x x
5.5
x
4 interestRate 5 x 7 22.0
x x
5
x
5.5 interestRate
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 39
C: D:
4 interestRate 5 x 7 22.0
x x
5.5
x
5.5
x
4 interestRate 5 x 7 22.0
x x x
4 interestRate 5 x 7 22.0
x x
5.5
x
4 interestRate 5 x 7 22.0
x x
5
x
5.5 interestRate
E:
Which One is Closest to Your Answer?
B:
8/27/18 Variables & Assignments 40
C: D:
4 interestRate 5 x 7 22.0
x x x
4 interestRate 5 x 7 22.0
x x
5.5
x
4 interestRate 5 x 7 22.0
x x
5
x
5.5 interestRate
Exercise: Understanding Assignment
intrestRate = x + interestRate
neighbor, discuss it if you did something different.
4 interestRate 5.5 5 x 7 22.0
8/27/18 Variables & Assignments 41
x x x
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 42
C: D:
4 interestRate 5 x 7 22.0
x x
5.5
x
4 interestRate 5 x 7 22.0
x x x
interestRate 5 x 7 22.0
x x
interestRate 5 x 7 22.0
x x
27.5 intrestRate 5.5 4
x 5.5
27.5 intrestRate
x
27.5
x
4 5.5
x
27.5
x
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 43
C: D:
4 interestRate 5 x 7 22.0
x x
5.5
x
4 interestRate 5 x 7 22.0
x x x
interestRate 5 x 7 22.0
x x
interestRate 5 x 7 22.0
x x
27.5 intrestRate 5.5 4
x 5.5
27.5 intrestRate
x
27.5
x
4 5.5
x
27.5
x
E:
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 44
4 interestRate 5 x 7 22.0
x x
5.5
x
4 interestRate 5 x 7 22.0
x x x
27.5 intrestRate 5.5 27.5
x
^ e
Which One is Closest to Your Answer?
A: B:
8/27/18 Variables & Assignments 45
4 interestRate 5 x 7 22.0
x x
5.5
x
4 interestRate 5 x 7 22.0
x x x
27.5 intrestRate 5.5 27.5
x
^ e Spelling mistakes in Python are bad!!
Dynamic Typing
§ 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
>>> x = 1 >>> x = x / 2.0
§ Each variable restricted to values of just one type
type(x) == int x = float(x) type(x) == float
8/27/18 Variables & Assignments 46
Dynamic Typing
§ 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
>>> x = 1 >>> x = x / 2.0
§ 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/27/18 Variables & Assignments 47
Dynamic Typing
§ What is the result of evaluating x / y? § Depends on whether x, y are int or float values
§ type(2) evaluates to <type 'int'> § type(x) evaluates to type of contents of x
§ type('abc') == str evaluates to True
8/27/18 Variables & Assignments 48