Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Python</strong> <strong>Tutorial</strong>, Release 2.6.2<br />
2<br />
>>> 7/-3<br />
-3<br />
The equal sign (’=’) is used to assign a value to a variable. Afterwards, no result is displayed before the next<br />
interactive prompt:<br />
>>> width = 20<br />
>>> height = 5*9<br />
>>> width * height<br />
900<br />
A value can be assigned to several variables simultaneously:<br />
>>> x = y = z = 0 # Zero x, y and z<br />
>>> x<br />
0<br />
>>> y<br />
0<br />
>>> z<br />
0<br />
Variables must be “defined” (assigned a value) before they can be used, or an error will occur:<br />
>>> # try to access an undefined variable<br />
... n<br />
Traceback (most recent call last):<br />
File "", line 1, in <br />
NameError: name ’n’ is not defined<br />
There is full support for floating point; operators with mixed type operands convert the integer operand to floating<br />
point:<br />
>>> 3 * 3.75 / 1.5<br />
7.5<br />
>>> 7.0 / 2<br />
3.5<br />
Complex numbers are also supported; imaginary numbers are written with a suffix of j or J. Complex numbers<br />
with a nonzero real component are written as (real+imagj), or can be created with the complex(real,<br />
imag) function.<br />
>>> 1j * 1J<br />
(-1+0j)<br />
>>> 1j * complex(0,1)<br />
(-1+0j)<br />
>>> 3+1j*3<br />
(3+3j)<br />
>>> (3+1j)*3<br />
(9+3j)<br />
>>> (1+2j)/(1+1j)<br />
(1.5+0.5j)<br />
Complex numbers are always represented as two floating point numbers, the real and imaginary part. To extract<br />
these parts from a complex number z, use z.real and z.imag.<br />
10 Chapter 3. An Informal Introduction to <strong>Python</strong>