python basics! - data types, strings, indexing...administrivia administrivia 3/44 lab #2 tomorrow...
TRANSCRIPT
Python Basics!data types, strings, indexing
CS101 Lecture #3
2016-10-08
Administrivia 1/44
Administrivia
Administrivia
Administrivia 2/44
Homework #2–#6 will be later.
Final answer counts.Answers will be released 18 hours later.
Administrivia
Administrivia 2/44
Homework #2–#6 will be later.Final answer counts.
Answers will be released 18 hours later.
Administrivia
Administrivia 2/44
Homework #2–#6 will be later.Final answer counts.Answers will be released 18 hours later.
Administrivia
Administrivia 3/44
Lab #2 tomorrow Sunday.
Where can you get help in this class?Blackboard forumInstructors in labs and office hours
You don’t need to install Python—but if you do,use Python 3.This is not a “weeder” class—you can succeed!
Administrivia
Administrivia 3/44
Lab #2 tomorrow Sunday.Where can you get help in this class?
Blackboard forumInstructors in labs and office hours
You don’t need to install Python—but if you do,use Python 3.This is not a “weeder” class—you can succeed!
Administrivia
Administrivia 3/44
Lab #2 tomorrow Sunday.Where can you get help in this class?
Blackboard forumInstructors in labs and office hours
You don’t need to install Python—but if you do,use Python 3.
This is not a “weeder” class—you can succeed!
Administrivia
Administrivia 3/44
Lab #2 tomorrow Sunday.Where can you get help in this class?
Blackboard forumInstructors in labs and office hours
You don’t need to install Python—but if you do,use Python 3.This is not a “weeder” class—you can succeed!
Quick Review & A Bit New 4/44
Quick Review & A Bit New
How Assignment Works
Quick Review & A Bit New 5/44
x = 10
y = x * xx * x = y
x,y = y,x # a neat trick
How Assignment Works
Quick Review & A Bit New 5/44
x = 10y = x * x
x * x = y
x,y = y,x # a neat trick
How Assignment Works
Quick Review & A Bit New 5/44
x = 10y = x * xx * x = y
x,y = y,x # a neat trick
How Assignment Works
Quick Review & A Bit New 5/44
x = 10y = x * xx * x = y
x,y = y,x # a neat trick
Warmup Quiz 6/44
Warmup Quiz
Reminder
Warmup Quiz 7/44
You will have graded quiz starting from theupcoming Monday lecture!
Our execution model
Warmup Quiz 8/44
Question #1
Warmup Quiz 9/44
x = 10y = x + 1y = x * y
What is the value of y?A 11B 100C 110D None of the above
Question #2
Warmup Quiz 10/44
x = 10y = x + 1y = x * y
What do we call x?A a literalB a variableC an expressionD a statement
Question #3
Warmup Quiz 11/44
x = 10y = x + 1y = x * y
What do we call 10?A a literalB a variableC an expressionD a statement
Question #2
Warmup Quiz 12/44
x = 10y = x + 1y = x * y
What do we call y = x * y?A a literalB a variableC an expressionD a statement
Question #5
Warmup Quiz 13/44
x = 10y = xx = 5
What is the value of y?A 10B 5
Data Types 14/44
Data Types
What is an encoding?
Data Types 15/44
01001000 01000101 01001100 01001100What does a binary data value like this represent?
What does binary data represent?
How does the processor know?
What is an encoding?
Data Types 15/44
01001000 01000101 01001100 01001100What does a binary data value like this represent?
What does binary data represent?How does the processor know?
Our execution model
Data Types 16/44
What is an encoding?
Data Types 17/44
01001000 01000101 01001100 01001100What does a binary data value like this represent?
What does binary data represent?How does the processor know?The encoding interprets the value.
Our execution model
Data Types 18/44
What is a data type?
Data Types 19/44
A data type defines an encoding rule.All values have a type.
The type defines how data is represented inmemory.The type defines allowed operations and how theywork.
What is a data type?
Data Types 19/44
A data type defines an encoding rule.All values have a type.The type defines how data is represented inmemory.
The type defines allowed operations and how theywork.
What is a data type?
Data Types 19/44
A data type defines an encoding rule.All values have a type.The type defines how data is represented inmemory.The type defines allowed operations and how theywork.
Example
Data Types 20/44
01100111 can be the number 103, the letter g,hexadecimal 67, 3.5, etc.
So what are these data types?
Numeric Data Types 21/44
Numeric Data Types
How do binary numbers work?
Numeric Data Types 22/44
Numeric types can be represented in binary:000 0 100 4001 1 101 5010 2 110 6011 3 111 7
If we add more, the number overflows.Negative numbers? Add a sign bit.
How do binary numbers work?
Numeric Data Types 22/44
Numeric types can be represented in binary:000 0 100 4001 1 101 5010 2 110 6011 3 111 7
If we add more, the number overflows.
Negative numbers? Add a sign bit.
How do binary numbers work?
Numeric Data Types 22/44
Numeric types can be represented in binary:000 0 100 4001 1 101 5010 2 110 6011 3 111 7
If we add more, the number overflows.Negative numbers? Add a sign bit.
Integers,Z
Numeric Data Types 23/44
Integers have been our only type thus far....,−4,−3,−2,−1, 0,+1,+2,+3, ...
What are limits?
Integer operations
Numeric Data Types 24/44
Evaluating an expression of integers will generallyresult in an integer answer
3 + 5
EXCEPTION: DIVISION!3 / 4 → 0.753 // 4 → 0 (floor division)
Integer operations
Numeric Data Types 24/44
Evaluating an expression of integers will generallyresult in an integer answer
3 + 5EXCEPTION: DIVISION!
3 / 4 → 0.753 // 4 → 0 (floor division)
Integer operations
Numeric Data Types 24/44
Evaluating an expression of integers will generallyresult in an integer answer
3 + 5EXCEPTION: DIVISION!3 / 4 → 0.75
3 // 4 → 0 (floor division)
Integer operations
Numeric Data Types 24/44
Evaluating an expression of integers will generallyresult in an integer answer
3 + 5EXCEPTION: DIVISION!3 / 4 → 0.753 // 4 → 0 (floor division)
Floating-point numbers,R
Numeric Data Types 25/44
Floating-point numbers include a fractional part.
(Anything with a decimal point—2.4, 3.0.)What are limits?
Overflow/underflowArbitrary precision (π, e)
Floating-point numbers,R
Numeric Data Types 25/44
Floating-point numbers include a fractional part.(Anything with a decimal point—2.4, 3.0.)
What are limits?Overflow/underflowArbitrary precision (π, e)
Floating-point numbers,R
Numeric Data Types 25/44
Floating-point numbers include a fractional part.(Anything with a decimal point—2.4, 3.0.)What are limits?
Overflow/underflowArbitrary precision (π, e)
Floating-point numbers,R
Numeric Data Types 25/44
Floating-point numbers include a fractional part.(Anything with a decimal point—2.4, 3.0.)What are limits?
Overflow/underflowArbitrary precision (π, e)
Floating-point operations
Numeric Data Types 26/44
Evaluating an expression of floating-point valueswill result in a floating-point answer.
3.0 + 5.5 → 8.53.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)
Engineers and scientists need to think carefullyabout the precision of answers.
Floating-point operations
Numeric Data Types 26/44
Evaluating an expression of floating-point valueswill result in a floating-point answer.
3.0 + 5.5 → 8.5
3.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)
Engineers and scientists need to think carefullyabout the precision of answers.
Floating-point operations
Numeric Data Types 26/44
Evaluating an expression of floating-point valueswill result in a floating-point answer.
3.0 + 5.5 → 8.53.0 + 5.0 → 8.0
3 + 5.5 → ? (what happens here?)Engineers and scientists need to think carefullyabout the precision of answers.
Floating-point operations
Numeric Data Types 26/44
Evaluating an expression of floating-point valueswill result in a floating-point answer.
3.0 + 5.5 → 8.53.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)
Engineers and scientists need to think carefullyabout the precision of answers.
Floating-point operations
Numeric Data Types 26/44
Evaluating an expression of floating-point valueswill result in a floating-point answer.
3.0 + 5.5 → 8.53.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)
Engineers and scientists need to think carefullyabout the precision of answers.
Complex numbers,C
Numeric Data Types 27/44
Represent numbers with an imaginary component.
Use j for i:1.0 + 1jThink of ”jmaginary” numbers, I suppose.
Complex numbers,C
Numeric Data Types 27/44
Represent numbers with an imaginary component.Use j for i:1.0 + 1j
Think of ”jmaginary” numbers, I suppose.
Complex numbers,C
Numeric Data Types 27/44
Represent numbers with an imaginary component.Use j for i:1.0 + 1jThink of ”jmaginary” numbers, I suppose.
Example
Numeric Data Types 28/44
x = 4y = 3 + 1jz = 33.3333print( x + y + z )
What is printed to the screen?
A 40B 40.3333C 40.3333 + 1jD None of the above
Example
Numeric Data Types 28/44
x = 4y = 3 + 1jz = 33.3333print( x + y + z )
What is printed to the screen?A 40B 40.3333C 40.3333 + 1jD None of the above
Attribute operator .
Numeric Data Types 29/44
Reaches inside of a value to access part of itsdata (called an attribute).
Extracts special variables stored “inside” of thetype.print(x.real)print(x.imag)Both of these components are floats.
Attribute operator .
Numeric Data Types 29/44
Reaches inside of a value to access part of itsdata (called an attribute).Extracts special variables stored “inside” of thetype.print(x.real)print(x.imag)
Both of these components are floats.
Attribute operator .
Numeric Data Types 29/44
Reaches inside of a value to access part of itsdata (called an attribute).Extracts special variables stored “inside” of thetype.print(x.real)print(x.imag)Both of these components are floats.
Example
Numeric Data Types 30/44
x = (3.5 + 1j)y = 1z = x + y
What is the value of z.imag?
A 4.5 + 1jB 4.5C 1jD 1.0
Example
Numeric Data Types 30/44
x = (3.5 + 1j)y = 1z = x + y
What is the value of z.imag?A 4.5 + 1jB 4.5C 1jD 1.0
String Data Type 31/44
String Data Type
How does text work?
String Data Type 32/44
Each symbol is stored individually, one byte long:01001000 7201000101 6901001100 7601001100 7601001111 79
ASCII encoding table
String Data Type 33/44
72 69 76 76 79 = H E L L O'HELLO'
ASCII encoding table
String Data Type 33/44
72 69 76 76 79 = H E L L O
'HELLO'
ASCII encoding table
String Data Type 33/44
72 69 76 76 79 = H E L L O'HELLO'
Strings
String Data Type 34/44
As a literal: text surrounded by quotes."DEEP"
Each symbol is a character.Unlike numeric types, strings vary in length.
Strings
String Data Type 34/44
As a literal: text surrounded by quotes."DEEP"
Each symbol is a character.
Unlike numeric types, strings vary in length.
Strings
String Data Type 34/44
As a literal: text surrounded by quotes."DEEP"
Each symbol is a character.Unlike numeric types, strings vary in length.
String operations
String Data Type 35/44
Concatenation: combine two stringsUses the + symbol'RACE' + 'CAR'
Repetition: repeat a stringUses the *'HELLO '*10
Formatting: used to encode other data as stringUses % symbol
String operations
String Data Type 35/44
Concatenation: combine two stringsUses the + symbol'RACE' + 'CAR'
Repetition: repeat a stringUses the *'HELLO '*10
Formatting: used to encode other data as stringUses % symbol
String operations
String Data Type 35/44
Concatenation: combine two stringsUses the + symbol'RACE' + 'CAR'
Repetition: repeat a stringUses the *'HELLO '*10
Formatting: used to encode other data as stringUses % symbol
Formatting operator
String Data Type 36/44
Creates string with value inserted
Formats nicelyRequires indicator of type inside of string
x = 100 * 54s = "String is: %i" % xprint(s)
Formatting operator
String Data Type 36/44
Creates string with value insertedFormats nicelyRequires indicator of type inside of string
x = 100 * 54s = "String is: %i" % xprint(s)
Formatting operator
String Data Type 36/44
Creates string with value insertedFormats nicelyRequires indicator of type inside of string
x = 100 * 54s = "String is: %i" % xprint(s)
Example
String Data Type 37/44
name = "Tao"grade = 2 / 3m1 = "Hello, %s!" % namem2 = "Your grade is: %f." % gradeprint(m1)print(m2)
Hello, Tao!Your grade is 0.66667.
Example
String Data Type 37/44
name = "Tao"grade = 2 / 3m1 = "Hello, %s!" % namem2 = "Your grade is: %f." % gradeprint(m1)print(m2)
Hello, Tao!Your grade is 0.66667.
Example
String Data Type 38/44
x = 3s = ("%i" % (x+1)) * x**(5%x)print(s)
What does this program print?A 333333333333B 444444444C 9999D %i%i%i%i%i
Indexing operator
String Data Type 39/44
Extracts single character
a = "FIRE"a[0]The integer is the index.We count from zero!If negative, counts down from end.
Indexing operator
String Data Type 39/44
Extracts single charactera = "FIRE"a[0]
The integer is the index.We count from zero!If negative, counts down from end.
Indexing operator
String Data Type 39/44
Extracts single charactera = "FIRE"a[0]The integer is the index.
We count from zero!If negative, counts down from end.
Indexing operator
String Data Type 39/44
Extracts single charactera = "FIRE"a[0]The integer is the index.We count from zero!
If negative, counts down from end.
Indexing operator
String Data Type 39/44
Extracts single charactera = "FIRE"a[0]The integer is the index.We count from zero!If negative, counts down from end.
Question
String Data Type 40/44
s = "ABCDE"i = 3x = s[i]
What is the value of x?A 'A'B 'B'C 'C'D 'D'E 'E'
Question
String Data Type 41/44
s = "ABCDE"i = 25 % 3y = s[i]
What is the value of y?A 'A'B 'B'C 'C'D 'D'E 'E'
Question
String Data Type 42/44
s = "ABCDE"i = (11 % 3) - 7z = s[i]
What is the value of z?A 'A'B 'B'C 'C'D 'D'E 'E'
Reminders 43/44
Reminders
Reminders
Reminders 44/44
Lab #2 tomorrow Sunday.