Postscript (2014/2/6)
I like switch statements. The outlook for the program is much better and your mind is tidied up. However, there is no switch statement in python. The major is to use if, elif, etc. for conditional branching. For example, if there is a switch statement like the one below,
#General switch statement used in java etc.
switch(str){
case 'a':
case 'b':
print('a,b'); break;
case 'c':
print('c'); break;
}
In python, using if and elif, it looks like this:
if str == 'a' or str == 'b':
print('a,b')
elif str == 'c':
print('c')
Writing str == twice in the first if statement can be tedious. In particular, if there are two or more of these, or if the variable name is long, the if statement will be long and the readability of the program will be reduced. Therefore,
if str in {'a', 'b'}:
print('a, b')
elif str == 'c':
print('c')
Then the outlook will be better. You can reaffirm that python's in is useful.
Recommended Posts