(An Unofficial) Python Tutorial Wiki

putting the community back in "maintained by the community"

Beginners - Numbers

COMMENT: This is part of a suggested restructuring of the Tutorial. Use the sidebar links to return to the current Tutorial.

  1. Intro
  2. Interpreter
  3. Numbers
  4. Strings
  5. Lists
  6. Dictionaries
  7. Control Flow
  8. Functions
  9. Classes

Numbers

The interpreter acts as a simple calculator: you type in expressions, and it echoes back the resulting value. Python's expression syntax is straightforward: the operators +, -, * and / work just like in most other programming languages (for example, C or Java), and parentheses can be used for grouping. For example:

>>> 2+2
4
>>> # This is a comment
... 2+2
4
>>> 2+2  # and a comment on the same line as code
4
>>> (50-5*6)/4
5
>>> # Integer division rounds down
... 7/3
2
>>> 7/-3
-3

The equal sign ("=") is used to assign a value to a variable. In this case, the value isn't echoed back, but you can type in the name of a variable to see its value:

>>> width = 20
>>> width
20
>>> height = 5*9
>>> width * height
900

A value can be assigned to several variables simultaneously:

>>> x = y = z = 0  # Zero is assigned to x, y and z
>>> x
0
>>> y
0
>>> z
0

There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:

>>> 3 * 3.75 / 1.5
7.5
>>> 7.0 / 2
3.5

The interpreter prints up to seventeen significant digits when it prints a floating point number, which can cause surprises when you print out decimal values that cannot be exactly represented by the internal binary representation. For example, the decimal value 0.1 ends up with more decimals than one would expect:

>>> 1.0 / 10.0
0.10000000000000001

For more background, see Appendix B, Floating Point Arithmetic: Issues and Limitations.

Complex numbers are a somewhat advanced mathematical concept, and Python supports them. Imaginary numbers are written with a suffix of "j" or "J". Complex numbers with a nonzero real component are written as "(real+imagj)", or can be created with the "complex(real, imag)" function.

>>> 1j * 1J
(-1+0j)
>>> 1j * complex(0,1)
(-1+0j)
>>> 3+1j*3
(3+3j)
>>> (3+1j)*3
(9+3j)
>>> (1+2j)/(1+1j)
(1.5+0.5j)

Complex numbers are always represented as two floating point numbers, the real and imaginary part. To extract these parts from a complex number z, use z.real and z.imag.

>>> a=1.5+0.5j
>>> a.real
1.5
>>> a.imag
0.5

The conversion functions to floating point and integer (float(), int() and long()) don't work for complex numbers -- there is no one correct way to convert a complex number to a real number. Use abs(z) to get its magnitude (as a float) or z.real to get its real part.

>>> a=3.0+4.0j
>>> float(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: can't convert complex to float; use abs(z)
>>> a.real
3.0
>>> a.imag
4.0
>>> abs(a)  # sqrt(a.real**2 + a.imag**2)
5.0
>>>

In interactive mode, the last printed expression is automatically assigned to the variable _, behind the scenes. This makes it easy to reuse the last result when you want to continue a calculation, for example:

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
>>>

This interpreter-specific variable should be treated as read-only by the user. If you assign to it, you will create an independent local variable with the same name, which masks the built-in variable with its magic behavior.


Previous (Interpreter) - Next (Strings)


FAQS (http://pyfaq.infogami.com)

Examples