Lecture 2: Python Arithmetic CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork
Basic data types in Python
Python data types Programs manipulate data from families called data types; each data type has is own rules and capabilities. int (Integers i.e. Z) e.g. 123, 456 Capabilities: standard arithmetic (+,,, //) and more float (Real numbers R) e.g 3.14, 6.02E23 Capabilities: arithmetic +,,, /; also plethora of maths functions str (Strings i.e. text) e.g To be or not to be. Capabilities: Many text-manipulating functions Many, many others... 1
Integers (int)
Python integers Examples Standard Arabic notation; optional leading sign 123 0 456 12345678901234567890 Non-examples no internal non-digits 1 2 3 456 10lO 12, 345, 678, 901, 234, 567, 890 Notes No limit on number of digits 2
Basic arithmetic Code Result Explanation 1 + 2 3 +,, (multiplication) work as expected 3-4 -1 5 * 6 30 7 + 8 * 9 79 BODMAS rules apply 1-2 - 3-4 (7 + 8) * 9 135 () override default rules 2 ** 3 8 2 to power of 3 Note: spaces around numbers or operators are OK, but not within 3
Integer division operator Integer division operators n // d Integer part when n is divided by d e.g. 10 // 5 yields 2, 10 // 3 yields 3, n % d Remainder when n is divided by d e.g. 10 % 5 yields 0, 10 % 3 yields 1 NB // for integer result (not /) Examples Expression Result Expression Result 8 // 3 2 8 % 3 2 8 // 4 2 8 % 4 0 8 // 5 1 8 % 5 3 4
Division and negative integerss Python s // operator rounds towards (not towards zero); it s the largest integer value less than or equal to the precise real result. Code Result Explanation 5 // 2 2 2.5 2-5 // 2-3 2.5 3 5 // 3 1 1.66 1-5 // 3-2 1.66 2 Note: Python s int division operators obey following identity. (x // y) y + x % y = x 5
Programming errors Misusing Python features causes programming errors, causing program to misbehave and often crash. Code Result Explanation 10l0 SyntaxError Violates Python s rules. Program crashes. n // d ZeroDivisionError (If d = 0). Cannot divide by zero! Program crashes. 1, 000 + 1 (1, 1) Legally OK, but probably not what you had in mind. 6
Operator precedence Precedence Python applies higher precedence operators before lower Op Pre ( ) High, //, % +, Low Association Operators of equal precedence are applied left to right. Code Result Explanation 1 + 2 * (3 + 4) 15 First (), then,, then + 1 + 2-3 + 4-5 -1 Applied l-to-r 7
Real numbers (floats)
Python real numbers (floats) Examples Standard decimal or scientific notation; optional sign 1.23 0 2.718 3.14 5.893E 11 7.35e + 22 6.02E23 Non-examples no extraneous characters 1 2. 3 4, 56 6.02x10 23 Limits on precision; floats are approximations of the true values 8
Inexactitude of Python s reals Code Result Explanation 0.1 + 0.2 0.30000000000000004 Result off in 17th decimal place! math.pi 3.141592653589793 Only 15 decimal places Not all reals can be represented exactly in computer s memory Most are approximated by closest representable number Real calculations are rarely mathematically exact float R 9
Using floats can be tricky Representational and roundoff error issues: Hard to gauge how accurate any result is. Will use floats sparingly in this module. 10
Mixing integers and real numbers Code Result Explanation 1 + 2.0 3.0 ints converted to float automatically in mixed expressions float(1) 1.0 Explicitly converts int to real int(1.23) 1 Converts real to int: truncates decimal part round(3.4) 3 Rounds off to nearest int round(3.5) 4 ditto int(), float(), round() are built-in functions; note function name(...) syntax 11
Real arithmetic and the / operator Code Result Explanation 1.0 + 2.0 * 3.4 7.8 +, -, *, () work as expected 8 / 3 2.666666666666665 The / operator produces a flo result... 8 / 2 4.0 even with integer values 8.5 / 2.7 3.148148148148148 Warning Distinguish carefully between / and //. 12
Python s mathematical functions Python supports multitude of mathematical functions (logs, exponentials, trigonometrics and so on). Code Result Explanation math.sqrt(7) 2.6457513110645907 Need import math at top of program to avail of these features. Look them up as needed. 13
Notes
Notes Code progname.py Description Reading 14