what is the reason why Python doesn't have switch statement ?

Probably only Guido knows for sure. This topic has come up a lot in the past. An if/elif chain is as concise as switch/case and allows for tests other than equality, so in some sense is more powerful. But it does not have the "semantic familiarity" of a switch statement, viz:

if x==1:
    do x == 1 things
elif x==2:
    do x == 2 things
elif x==3 and y==4: # try that in a case statement
    do x==2 && y==4 things
elif x==3 and y==0: # try that in a case statement
    do x==2 && y==0 things
elif x > 3:
    things to do when x > 3 # try that in a case statement
else:
    do default things

Here is a python enhancement proposal about the switch statement, by Guido Van Rossum which he apparently rejected because of lack of popular support http://www.python.org/dev/peps/pep-3103/.

what is the reason why Python doesn't have switch statement ?

Even in C the switch/case is not as efficient as multiple if/else and the cases are rather limited.

Actually, a dictionary makes an excellent replacement for a switch/case and is very high speed on the lookups:

# a dictionary switch/case like statement to replace
# multiple if/elif/else statements in Python

def switch_case(case):
    return "You entered " + {
    '1' : "one",
    '2' : "two",
    '3' : "three"
    }.get(case, "an out of range number")

num = raw_input("Input a number between 1 and 3: ")
print switch_case(num)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.