Boston Python Workshop 3/Data types

From OpenHatch wiki
Revision as of 15:25, 12 June 2011 by imported>Jesstess (Created page with '==Numbers: integers and floats== ====Addition==== <pre> >>> 2 + 2 4 </pre> ====Subtraction==== <pre> >>> 0 - 2 -2 </pre> ====Multiplication==== <pre> >>> 2 * 3 6 </pre> ==…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Numbers: integers and floats

Addition

>>> 2 + 2
4

Subtraction

>>> 0 - 2
-2

Multiplication

>>> 2 * 3
6

Division

>>> 4 / 2
2
>>> 1 / 2
0

Integer divison produces an integer. You need a number that knows about the decimal point to get a decimal out of division:

>>> 1.0 / 2
0.5
>>> float(1) / 2
0.5

Remainder

>>> 4 % 2
0
>>> 4 % 3
1

Types

>>> type(1)
<type 'int'>
>>> type(1.0)
<type 'float'>