Package basics :: Module statement
[hide private]
[frames] | no frames]

Source Code for Module basics.statement

 1  """ 
 2  Python makes a distinction between C{statements} and C{expressions}, 
 3  though many things are both. 
 4   
 5  The following X{variable assignment} is a statement not an expression: 
 6   
 7     >>> X = 3 
 8     >>> X 
 9     3 
10      
11  The variable C{X} is an expression and has a value 3, which Python 
12  echoes when you type C{X}. The statement C{X=3} has no value, 
13  so Python echoes nothing when you type it in. 
14   
15  The following X{identity test} is an expression. The fact that it is an 
16  expression means that it has a value: 
17   
18     >>> X == 3  
19     True 
20   
21  The following X{arithmetic expression} is an expression, which also 
22  has a value: 
23   
24     >>> X + 3  
25     6 
26   
27  Every expression can act as a statement.  When you type a 
28  lone expression to the Python interpreter, it is acting as 
29  a statement.  So the generalization about all Python code is that it is 
30  a sequence of statements. 
31   
32  Every expression can occur in X{compound statements}: 
33   
34     >>> if X == 3: print 'hi' 
35     ... 
36     hi 
37   
38  But the following raises a SyntaxError exception 
39   
40     >>> if X = 3:  print 'hi'   
41     ... 
42     SyntaxError: invalid syntax 
43           
44  Only expressions are allowed as the constituents of compound statements.  
45  """ 
46   
47 -def demo_statements():
48 X = 3 # Statement not an expression 49 print X 50 X == 3 # statement and expression 51 print X 52 X + 3 # statement and expression 53 print X 54 55 if X == 3: 56 print 'hi'
57