The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators +, -, * and / work just like in most other languages (e.g., Pascal or C); 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 returns the floor: ... 7/3 2 >>> 7/-3 -3
Like in C, the equal sign ("=") is used to assign a value to a variable. The value of an assignment is not written:
>>> width = 20 >>> height = 5*9 >>> width * height 900
>>> x = y = z = 0 # Zero x, y and z >>> x 0 >>> y 0 >>> z 0
>>> 4 * 2.5 / 3.3 3.0303030303 >>> 7.0 / 2 3.5
>>> 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)
>>> a=1.5+0.5j >>> a.real 1.5 >>> a.imag 0.5
>>> a=1.5+0.5j >>> float(a) Traceback (innermost last): File "<stdin>", line 1, in ? TypeError: can't convert complex to float; use e.g. abs(z) >>> a.real 1.5 >>> abs(a) 1.58113883008
>>> tax = 17.5 / 100 >>> price = 3.50 >>> price * tax 0.6125 >>> price + _ 4.1125 >>> round(_, 2) 4.11
This variable should be treated as read-only by the user. Don't explicitly assign a value to it -- you would create an independent local variable with the same name masking the built-in variable with its magic behavior.
guido@python.org