hello im new with python and i want to you ask a question.
is there any function like switch on C++ ?

Recommended Answers

All 18 Replies

Standard answer îs that for fixed values use dictionary lookup, otherwise use if...elif...else.. Learn to use 'in' check in condions. If you want we can recode some meaningfull switch using C snippet in Python.

An if ... elif ... elif ... sequence is a substitute for the switch or case statements found in other languages.

Source

Cheers and Happy coding

Thanks to both of you
Just one more question
is there fucntion like goto on C++
I was thinking something with class but i dont know how to call it to code. :D

You can use break to get out of loops and continue to continue iteration from next value.

If you want to jump out deeper from loop I recommend to move the inner loops to function and do return from there.

Yea thanks a lot all ! (:

Switch/case is rather slow in C or C++ and therefore was not implemented in Python. You can however use the much faster dictionary look-up to mimic a switch/case type function ...

# 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")

# Python2
#num = raw_input("Input a number between 1 and 3: ")
# Python3
num = input("Input a number between 1 and 3: ")
print( switch_case(num) )

Note: Sorry about the confusing statement above. A C style switch/case statement in Python would have been too slow. To be general, it would have to be usable with all object types and not just be limited to integers and single characters.

Switch/case is rather slow in C or C++ and therefore was not implemented in Python.

Switch/case is slow in C or C++? Not in my experience. Do you have evidence to support this claim?

I think it's easy as making a case and a for loop and testing them.


Cheers and Happy coding

I think it's easy as making a case and a for loop and testing them.

Assuming you're talking about testing the speed of switch statements: I can test only the compilers I have available, and on those I can see by looking at the machine code that switch statements often generated faster code than could readily be obtained by hand coding.

Moreover, in decades of working with C and C++ compilers, I don't think I've ever encountered one that generates slower code for a switch statement than it does for the corresponding if-else statements.

So when someone makes the blanket claim that switch statements are slow, I think it's up to the person making that claim to prove it.

Switch/case is slow in C or C++? Not in my experience. Do you have evidence to support this claim?

It is hard to compare a Python dictionary lookup with a C switch/case construct.

You have to think a little out of your box, and put yourself into the shoes of the Python team. Remember that Python is totally object oriented. The C switch/case is very limited in application. I think it's integers and characters only. To be truly useful, the Python switch/case would have to include all types of objects, that's what would have made so slow!

It is hard to compare a Python dictionary lookup with a C switch/case construct.
Also switch/case is very limited in application.

Of course.

I was responding to the specific claim that Python does not have switch statements because they're slower in C than if/else statements. I haven't commented on whether I think switch statements might be useful in Python for other reasons.

Of course.

I was responding to the specific claim that Python does not have switch statements because they're slower in C than if/else statements. I haven't commented on whether I think switch statements might be useful in Python for other reasons.

This is my original statement:

Switch/case is rather slow in C or C++ and therefore was not implemented in Python. You can however use the much faster dictionary look-up to mimic a switch/case type function ...

It does not make any reference to an if/else if construct.

This is my original statement:
It does not make any reference to an if/else if construct.

There's no other real alternative to switch/case in C or C++.
In particular, on every C or C++ implementation I've seen, the overhead of calling a function is much greater than that of a switch statement, so fetching an element from an array of function pointers is very unlikely to be a more efficient substitute for a switch.

I think it's easy as making a case and a for loop and testing them.


Cheers and Happy coding

Oh no, the switch/case will be the winner over multiple if/else. However to get the speed, the C switch/case is very limited and the Python team didn't want to limit it to just integers and single characters. You have to remember that C was developed to write operating systems (Unix). Also remember that the Python core is written in C, and those early team members were experts in C and C++.

Come to think of it, C might have borrowed the switch/case concept from Pascal's case statement. Clever anyway!

There's no other real alternative to switch/case in C or C++.
In particular, on every C or C++ implementation I've seen, the overhead of calling a function is much greater than that of a switch statement, so fetching an element from an array of function pointers is very unlikely to be a more efficient substitute for a switch.

Python uses the dictionary container internally, and it is highly optimized. Actually the dictionary is the heart of the Python interpreter and avoids fixed memory locations for objects. I am not surprised that the a dictionary look-up won out over any general purpose switch/case construct.

I never said to compare it with if..elif..else.

Cheers and Happy coding

I never said to compare it with if..elif..else.

Cheers and Happy coding

I understand, I am just saying that there is no need to compare. In C switch/case is the racecar, if/else is the bicycle.

Enjoy Python!

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.