Why does python not need the three-object operator and switch?

Source: Internet
Author: User

Why does python not need the three-object operator and switch?

For ternary operator, python can be replaced by conditional expressions.

For example, for x <5? 1-0 can be implemented in the following way

1if x <5 else 0

Note: conditional expressions was introduced before python 2.5, so the above Code is only applicable to versions 2.5 and later.

For versions earlier than 2.5, the following form can be used:

X <5and1or 0

We can use dictionary to implement the switch. See the following example.

>>> Def switch (choice): return dict (enumerate (range (4) [choice] >>> switch (1) >>> switch (0) values = {value1: do_something1, value2: do_something2 ,... valueN: do_somethingN,} values. get (var, do_default_something )()

The above is the small series for everyone to talk about why python does not need the three-object operator and switch all the content, I hope you can support a lot of help house ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.