prev | Draft Version 590 (Thu Dec 1 09:18:29 2005) | next |
Python
is:
Numeric
package isn't badPython Cookbook
for the on-line version$ python
>>> 3 + 5 8 >>> x = 5 * 3 ** 2 # assign 5 times 3 squared to x >>> print x 45 >>> print 'some' + 'thing' # concatenate strings something
.py
extension, and type python filename.py
.py
file#!/usr/bin/env python
python
, and run it with the rest of the file as its input.py
files with Python.py
will then run itpowers.py
print 2, 2**2, 2**3, 2**4 print 3, 3**2, 3**3, 3**4 print 4, 4**2, 4**3, 4**5
python powers.py
2 4 8 16 3 9 27 81 4 16 64 1024
print something
Traceback (most recent call last): File "undefined.py", line 1, in ? print something NameError: name 'something' is not defined
x = 'two' # 'two' is a string y = 2 # 2 is an integer print x * y # multiplying a string concatenates it repeatedly print x + y # but you can't add an integer and a string
twotwo
Traceback (most recent call last): File "add_int_str.py", line 4, in ? print x + y # but you can't add an integer and a string TypeError: cannot concatenate 'str' and 'int' objects
print
statement prints zero or more valuesprint
on its own just prints a blank lineprint 'a', "b", '"c"', "'d'"
a b "c" 'd'
str
converts things to stringsprint 'carbon-' + str(14)
carbon-14
int
, float
, etc. to convert values to other types'This isn\'t unusual.'
"She said, \"You can quote me on that!\""
'\tIndented line\n'
14
is an integer (32 bits long on most machines)14.0
is floating point (double precision, i.e., 64 bits long)1+4j
is a complex number (two 64-bit values)123456789L
is a long integerName | Operator | Use | Value | Notes |
---|---|---|---|---|
Addition | + | 35 + 22 | 57 | |
'Py' + 'thon' | 'Python' | |||
Subtraction | - | 35 - 22 | 13 | |
Multiplication | * | 3 * 2 | 6 | |
'Py' * 2 | 'PyPy' | 2 * 'Py' is illegal | ||
Division | / | 3.0 / 2 | 1.5 | |
3 / 2 | 1 | Integer division rounds down: -3 / 2 is -2 , not -1 | ||
Exponentiation | ** | 2 ** 0.5 | 1.4142135623730951 | |
Remainder | % | 13 % 5 | 3 |
x += 3
does the same thing as x = x + 3
5 += 3
is an error, since you can't assign a new value to 5…True
and False
are true and false (d'oh)None
are equivalent to falseand
, or
, not
and
and or
are short-circuit operatorsTrue
or False
Expression | Result | Notes |
---|---|---|
True or False | True | |
True and False | False | |
'a' or 'b' | 'a' | or is true if either side is true, so it stops after evaluating 'a' |
'a' and 'b' | 'b' | and is only true if both sides are true, so it doesn't stop until it has evaluated 'b' |
0 or 'b' | 'b' | 0 is false, but 'b' is true |
0 and 'b' | 0 | Since 0 is false, Python can stop evaluating there |
0 and (1/0) | 0 | 1/0 would be an error, but Python never gets that far |
(x and 'set') or 'not set' | It depends | If x is true, this expression's value is 'set' ; if x is false, it is 'not set' |
=
for assignment==
to test if two things have equal valuesif
, elif
(not else if
), and else
a = 3 if a < 0: print 'less' elif a == 0: print 'equal' else: print 'greater'
a = 3 while a > 0: print a a -= 1
3 2 1
print 'before' a = -1 while a > 0: print a a -=1 print 'after'
before after
a = 3 while a > 0: print a # oops --- forgot to subtract one from a
3 3 3
break
a = 30 while True: print a a -= 1 if (a % 5) == 0: # if a is divisible by 5 break
30 29 28 27 26
continue
a = 5 while a > 0: print 'top of loop', a a -= 1 if (a % 2) == 0: continue print '...bottom of loop', a
top of loop 5 top of loop 4 ...bottom of loop 3 top of loop 3 top of loop 2 ...bottom of loop 1 top of loop 1
prev | Copyright © 2005, Python Software Foundation. See License for details. | next |