O'Reilly logo

Learn Python Programming by Fabrizio Romano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying discounts

In this example, I want to show you a technique I like a lot. In many programming languages, other than the if/elif/else constructs, in whatever form or syntax they may come, you can find another statement, usually called switch/case, that in Python is missing. It is the equivalent of a cascade of if/elif/.../elif/else clauses, with a syntax similar to this (warning! JavaScript code!):

/* switch.js */switch (day_number) {    case 1:    case 2:    case 3:    case 4:    case 5:        day = "Weekday";        break;    case 6:        day = "Saturday";        break;    case 0:        day = "Sunday";        break;    default:        day = "";
        alert(day_number + ' is not a valid day number.')}

In the preceding code, we switch on a variable called day_number. This means we get its value and then we ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required