Download presentation
Presentation is loading. Please wait.
1
LING 388: Computers and Language
Lecture 8
2
Administrivia Next week I'm out of town
The TA Patricia Lee will lead both classes You will do Python programming exercises in class
3
Python numbers revisited
Python invisibly automatically converts to type long int from int (32 bit 2's complement), (page 64–65, section 3.5) explicit type coercion: float() int() long() Python2.7 complex(r,i) complex(string)
4
Python numbers revisited
Note the difference in precision here
5
Python numbers revisited
page 60: see factorial.py Python 3.x Python 2.7 ( )
6
Python eval() Think of eval(input()) as being the same as the user typing directly at the Python console …
7
Python numbers revisited
factorial2.py
8
argv[1] and argv[2] ignored
Python argv List of arguments from the command line: what's argv[0] then? test.py argv[1] and argv[2] ignored
9
Python example How about printing to 2 decimal places?
Python 2.7 program How about printing to 2 decimal places?
10
Formatted Output Python 2.7 program
11
Formatted Output: floating point (f)
$100 principal for 10 years at 4% (0.04) compound interest = $148.02
12
Formatted Output Lots of ways: (Old way)
notation comes originally from the C Programming Language: sprintf function
13
Now use instead: <template-string>.format(<values>)
Formatted Output Now use instead: <template-string>.format(<values>) %<width>.<precision><type> <type> = d, f, s <width> = minimum number of characters; right-justified by default, -width => left-justified 0 = as wide as needed <precision> = number of places after decimal point e.g. 02d two digits wide, pad with 0 if needed
14
Formatted Output: examples
Function str() coerces integer or float above into type 'str'
15
Formatted Output Newer way:
formatting Use {} for each argument (can be numbered, e.g. {0}, {1},… or referenced by keyword {x}) {:width}
16
Formatted Output {0:..} and {1:…} index into the format arguments
0 in {0:..} can be omitted but not the colon
17
A note on string encoding
Python 2.7 Python 3.x
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.