Source:
#! /Bin/ENV python <br/> # Coding = gb2312 <br/> #-*-coding: gb2312-*-<br/> from _ future _ import Division <br/> #### if-else #### <br/> Print '### # If-else ####' <br/> A = input (": ") #12 or 10 + 2 <br/> B = input (" B: ") <br/> If (A> B): <br/> Print" Max: ", a <br/> else: <br/> Print" MAX :", B <br/>#### if-Elif-else ####< br/> Print '#### if-Elif-else ####' <br /> score = raw_input ("score: ") # string <br/> score = int (score) <br/> If (score> = 90) and (score <= 100 ): <br/> Print "A" <br/> Elif (score> = 80 and score <90 ): <br/> Print "B" <br/> Elif (score> = 60 and score <80): <br/> Print "C" <br/> else: <br/> Print "D" <br/> #### switch I #### <br/> Print '#### switch ####' <br /> x = 1 <br/> Y = 2 <br/> operator = "/" <br/> result = {<br/> "+ ": X + Y, <br/> "-": x-y, <br/> "*": x * y, <br/> "/": x/y <br/>}< br/> Print result. get (operator) <br/>#### switch II #### <br/> Print '#### switch II #####' <br/> class switch (object): <br/> def _ init _ (self, value): # init value <br/> self. value = value <br/> self. fall = false # No break, then fall = false <br/> def _ ITER _ (Self): <br/> yield self. match # match method to create <br/> raise stopiteration # exception to check loop <br/> def match (self, * ARGs): <br/> If self. fall or not ARGs: <br/> return true <br/> Elif self. value in ARGs: # successful <br/> self. fall = true <br/> return true <br/> else: # fail <br/> return false <br/> operator = "+" <br/> x = 1 <br/> Y = 2 <br/> for case in switch (operator): <br/> If case ('+'): <br/> Print X + Y <br/> Break <br/> If case ('-'): <br/> Print x-y <br/> Break <br/> If case ('*'): <br/> Print x * Y <br/> Break <br/> If case ('/'): <br/> Print x/y <br/> Break <br/> If case (): <br/> Print 'null' <br/>
Result:
[Work@db-testing-com06-vm3.db01.baidu.com Python] $ Python if_else.py
#### If-else ####
A: 12 + 8
B: 30
MAX: 30
#### If-Elif-else ####
Score: 88
B
#### Switch ####
0.5
#### Switch II ####
3
========================================================== ======================================
Chinese comments:
A very small problem. In PythonCodeAnd cannot be understood by the python interpreter (Python 2.5 ). Solution:
# Coding = gb2312
Print 'OK' # No problem with Chinese comments
Or:
#-*-Coding: gb2312 -*-
Print 'OK' # do the same.
Code details:
Http://blog.csdn.net/Lynn_yan/archive/2010/04/08/5464911.aspx