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 Lacy lsl92@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 Lacy § lsl92@cornell.edu § Will review by next week
§ Complete in online system § Show at start of next lab
9/3/19 Variables & Assignments 2
Official Announcement
9/3/19 Variables & Assignments 3
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 in Bailey lower lobby between lectures
9/3/19 Variables & Assignments 4
Consulting Hours Drama
§ Violates fire code to use ACCEL after 4:30 § Affects consultant hours and Section 211
§ Sunday consulting hours are in Upson 142 § Mon-Thurs consulting hours are in Gates G15 § Section 211 meets in Phillips 318
9/3/19 Variables & Assignments 5
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. 25
§ Assign points out of 100
§ Resubmit until perfect grade
§ 5-6pm, 3rd floor of Gates
9/3/19 Variables & Assignments 6
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
9/3/19 Variables & Assignments 7
iClickers
§ https://cs1110.cs.cornell.edu/py/clicker
§ http://www.cs.cornell.edu/courses/cs1110/2019fa § Click “Materials/Textbook” § Look under “iClickers”
9/3/19 Variables & Assignments 8
Warm-Up: Using Python
9/3/19 Variables & Assignments 9
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
9/3/19 Variables & Assignments 10
Converting Values Between Types
§ This is an expression § Evaluates to value, converted to new type § This is sometimes called casting
§ float(2) evaluates to 2.0 (a float) § int(2.6) evaluates to 2 (an int) § Note information loss in 2nd example
9/3/19 Variables & Assignments 11
Converting Values Between Types
bool ⇒ int ⇒ float
§ Python does automatically § Example: 1/2.0 evaluates to 0.5
§ Python never does automatically § Example: float(int(2.6)) evaluates to 2.0
9/3/19 Variables & Assignments 12
Operator Precedence
§ 2*(1+3) § 2*1 + 3
9/3/19 Variables & Assignments 13
Operator Precedence
§ 2*(1+3) § 2*1 + 3
§ Parentheses make the order explicit § What happens when no parentheses? add, then multiply multiply, then add
9/3/19 Variables & Assignments 14
Operator Precedence
§ 2*(1+3) § 2*1 + 3
§ Parentheses make the order explicit § What happens when no parentheses? add, then multiply multiply, then add
9/3/19 Variables & Assignments 15
Operator Precedence: The fixed order Python processes
Precedence of Python Operators
§ Parentheses highest § Logical ops lowest
§ Read “ties” left to right § Example: 1/2*3 is (1/2)*3
9/3/19 Variables & Assignments 16
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
Literal Complex
9/3/19 Variables & Assignments 17
Variables
§ is a box (memory location) § with a name § and a value in the box
5 x
Variable x, with value 5 (of type int)
20.1 area
Variable area, w/ value 20.1 (of type float)
9/3/19 Variables & Assignments 18
Using Variables
§ Evaluate to the value that is in the box § Example: 1 + x evaluates to 6
§ Example: 1 + x evaluates to 2.5 § Can even change the type of their value § Different from other languages (e.g. Java)
5
x
5
x
1.5
x
9/3/19 Variables & Assignments 19
Naming Variables
§ Names must only contain letters, numbers, _ § They cannot start with a number
§ e1 is a valid name § 1e2 is not valid (it is a float) § a_b is a valid name § a+b is not valid (it is + on two variables)
9/3/19 Variables & Assignments 20
Variables and Assignment Statements
x = 5
§ Expression: Something Python turns into a value § Statement: Command for Python to do something § Difference is that has no value itself
>>> x = 5 (NOTHING) x the value the variable 5 But can now use x as an expression
9/3/19 Variables & Assignments 21
Variables Do Not Exist Until Made
>>> y Error! >>> y = 3 >>> y 3
§ Before we just typed in one line at a time § Now program is a sequence of lines
9/3/19 Variables & Assignments 22
Assignments May Contain Expressions
§ Left of equals must always be variable: 1 + 2 = x § Read assignment statements right-to-left! § Evaluate the expression on the right § Store the result in the variable on the left
§ Example: x = y+2 § Example: x = x+2
This is not circular! Read right-to-left. x 5 y 2
9/3/19 Variables & Assignments 23
Execute the Statement: x = x + 2
5 x
9/3/19 Variables & Assignments 24
Execute the Statement: x = x + 2
§ For x, use the value in variable x § Write the expression somewhere on your paper 5 x
9/3/19 Variables & Assignments 25
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
9/3/19 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
neighbor, discuss it if you did something different.
5 x
9/3/19 Variables & Assignments 27
Which One is Closest to Your Answer?
A: B:
9/3/19 Variables & Assignments 28
C: D:
5 x 7
x
5 x 5 x x 7 x 7 x
Which One is Closest to Your Answer?
A: B:
9/3/19 Variables & Assignments 29
C:
5 x 7
x
5 x 5 x x 7 x 7 x
x = x + 2
Execute the Statement: x = 3.0 * x + 1.0
5 x 7
9/3/19 Variables & Assignments 30
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
9/3/19 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
neighbor, discuss it if you did something different.
5 x 7
9/3/19 Variables & Assignments 32
x
Which One is Closest to Your Answer?
A: B:
9/3/19 Variables & Assignments 33
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:
9/3/19 Variables & Assignments 34
C:
5 x 7
x
5 x 5 x x 22.0 x 22.0 x 22.0
x
7
x
7
x
x = 3.0 * x + 1.0
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
9/3/19 Variables & Assignments 35
x x
Exercise: Understanding Assignment
interestRate = x / interestRate
neighbor, discuss it if you did something different.
4 interestRate 5 x 7 22.0
9/3/19 Variables & Assignments 36
x x
Which One is Closest to Your Answer?
A: B:
9/3/19 Variables & Assignments 37
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:
9/3/19 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
E:
Which One is Closest to Your Answer?
B:
9/3/19 Variables & Assignments 39
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
interestRate = x/interestRate
Exercise: Understanding Assignment
intrestRate = x + interestRate
neighbor, discuss it if you did something different.
4 interestRate 5.5 5 x 7 22.0
9/3/19 Variables & Assignments 40
x x x
Which One is Closest to Your Answer?
A: B:
9/3/19 Variables & Assignments 41
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:
9/3/19 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
E:
Which One is Closest to Your Answer?
A: B:
9/3/19 Variables & Assignments 43
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
intrestRate = x + interestRate
^ e
Which One is Closest to Your Answer?
A: B:
9/3/19 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
intrestRate = x + interestRate
^ e Spelling mistakes in Python are bad!!
Dynamic Typing
§ Variables can hold values of any type § Variables can hold different types at different times
>>> x = 1 >>> x = x / 2.0
§ Each variable restricted to values of just one type § This is true in Java , C, C++, etc.
9/3/19 Variables & Assignments 45
ç x contains an int value ç x now contains a float value
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
9/3/19 Variables & Assignments 46