Data and Expressions I CS 160: Computer Science Orientation

advertisement
Data and Expressions I
CS 160: Computer Science Orientation
Today’s Schedule
•
•
•
•
•
•
Numeric Literals
String Literals
Encoding
Formatting
Joining
Live Coding
1
July 24, 2016
Numeric Literals
• Literal – sequence of one or more characters
that stands for itself
2
July 24, 2016
Numeric Literals
• Contains only:
• Digits: 0 – 9
• Sign: + or • Exponential notation: e
• Commas CANNOT be used!!!
• Explicit + for positive number rarely used
3
July 24, 2016
Numeric Literals
• Integer
• Positive or negative whole numbers
• E.g., 10
• NO SIZE LIMIT!
• Floating Point
• Decimal number
• E.g., 10.24
• Has limited range and precision...
4
July 24, 2016
Numeric Literals
• Floating points have limited range:
• “only” 10-308 to 10308
• ... and limited precision:
• “only” 16-17 digits of precision (after the decimal)
5
July 24, 2016
Numeric Literals
• Watch out for overflow! E.g.,
>>> 1.5e200 * 2.0e210
>>> Inf
• Watch out for underflow! E.g.,
>>> 1.0e-300 / 1.0e100
>>> 0.0
6
July 24, 2016
Numeric Literals
• Computers are discrete
• ==> Only approximations of infinite series can be stored in memory
• Python limits floating point values to 16 decimal points
>>> 1.0 / 3.0
>>> 0.3333333333333333
7
July 24, 2016
Numeric Literals
• Watch out for integer division!!
>>> 3 / 2
>>> ???
8
July 24, 2016
Numeric Literals
• Watch out for integer division!!
>>> 3 / 2
>>> 1
Wha???
Shouldn’t this be 1.5?
9
July 24, 2016
Numeric Literals
• Solution? explicitly declare value as floating point number
• (I.e., use a decimal)
>>> 3 / 2
>>> 1
>>> 3.0 / 2.0
>>> 1.5
10
July 24, 2016
Today’s Schedule
•
•
•
•
•
•
Numeric Literals
String Literals
Encoding
Formatting
Joining
Live Coding
11
July 24, 2016
String Literals
• Delimited by single OR double quotes
• Strings must be contained on a single line of code
• (Except when using triple quotes: ‘’’ – more later)
>>> print ‘hello’
hello
>>> print “hello”
hello
12
July 24, 2016
String Literals
• Python convention: use single quotes
• Unless double quotes are required, e.g.,
>>> print ‘you’re awesome at programming’
SYNTAX ERROR!!!
>>> print “you’re awesome at programming”
you’re awesome at programming
13
July 24, 2016
String Literals
• Strings can be empty or contain whitespace, e.g.,
>>> print ‘’
>>> print ‘
>>>
14
July 24, 2016
‘
Today’s Schedule
•
•
•
•
•
•
Numeric Literals
String Literals
Encoding
Formatting
Joining
Live Coding
15
July 24, 2016
Encoding
• Computers only deal with 0s and 1s
• So how do they represent numbers and strings?
16
July 24, 2016
Encoding
• Computers only deal with 0s and 1s
• So how do they represent numbers and strings?
binary encoding!!
17
July 24, 2016
Encoding
• 8-bit ASCII encoding standard
• Sequence of eight 0s and 1s determine character, e.g.,
‘A’ encoded as 0100 0001
‘B’ encoded as 0100 0010
...
18
July 24, 2016
Encoding
0100 0001 is ‘A’ according to ASCII encoding
and
FYI, 0100 0001 is binary for 65
= 26 + 20
= 64 + 1
= 65
19
July 24, 2016
Encoding (ASCII Table)
20
July 24, 2016
Encoding
• Python has built-in functions for ASCII encoding
• ord( ) function returns decimal value of character
>>> ord( ‘A’ )
65
>>> ord( ‘E’ )
69
21
July 24, 2016
Encoding
• Python has built-in functions for ASCII encoding
• chr( ) function returns character of encoded value
>>> chr( 65)
‘A’
>>> chr( 69 )
‘E’
22
July 24, 2016
Encoding
• Unicode is an international encoding standard
• Intended to be universal
•
•
•
•
8 to 32 bits per character
Currently over 100,000 Unicode defined characters
Capable of defining 4,000,000,000+ more
Encode characters for ALL past and present languages
23
July 24, 2016
Encoding
• Difference between numeric and string representation...
Numeric: value 124 is 01111100 in binary
String: “124” is 00110001 00110010 00110100 in binary!
(contains characters ‘1’, ‘2’, and ‘4’)
24
July 24, 2016
Encoding
• Aside: binary numbers
Numeric: value 124 is 01111100 in binary
= 26 + 25 + 24 + 23 + 22
= 64 + 32 + 16 + 8 + 4
= 124
25
July 24, 2016
Encoding
• Control characters – special chars not displayed on screen
• Control display of output (among other things)
• Represented by escape sequence
• Escape sequence• Begins with escape character (i.e., backslash \ )
• Escape sequence allows character to escape its original meaning
• E.g., \n is “newline”
26
July 24, 2016
Encoding
• Example: \n escape sequence => newline control character
>>> print “Hello there!!!\nHow are you doing?\n”
Hello there!!!
How are you doing?
>>>
27
July 24, 2016
Today’s Schedule
•
•
•
•
•
•
Numeric Literals
String Literals
Encoding
Formatting
Joining
Live Coding
28
July 24, 2016
Formatting
• Python’s format function controls display of strings
• format( value, format_specifier )
• Round floating point to 2 decimal places:
>>> format( 22.0 / 7.0, ‘.2f’)
‘3.14’
NOTE: format implicitly converts float to string...
29
July 24, 2016
Formatting
• Python’s format function controls display of strings
• format( value, format_specifier )
• Right justify string, set width to 20 characters
>>> format(‘hello’, ‘>20’)
'
hello’
30
July 24, 2016
Formatting
• Python’s format function controls display of strings
• format( value, format_specifier )
• Center string, set width to 50 characters
>>> format(‘center?’, ‘^50’)
'
center?
31
July 24, 2016
'
Today’s Schedule
•
•
•
•
•
•
Numeric Literals
String Literals
Encoding
Formatting
Joining
Live Coding
32
July 24, 2016
Joining
• Python syntax is based on whitespace
• More on this later...
• Long lines of code may need to span multiple lines for
increased readability
• Lines may be joined explicitly using backslash \
33
July 24, 2016
Joining
>>> x = 10 + \
... 20 + \
... 30 + \
... 40
>>> print x
100
NOTE: Python interpreter automatically adds the ellipses
34
July 24, 2016
Today’s Schedule
•
•
•
•
•
•
Numeric Literals
String Literals
Encoding
Formatting
Joining
Live Coding
35
July 24, 2016
Live Coding
•
•
•
•
Integer division
Floating point range and precision
‘Hello World’ ASCII encoding
Joining
36
July 24, 2016
Download