Set Multiple Choices with Elif in Python 3


show more Setting multiple choices with elif provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Setting multiple choices with elif

If statements in Python can be extended to test multiple conditions using elif. Let's go ahead and make a working copy of conditionals.py, call it conditionals-working.py, and we'll go ahead and open our working copy and define a variable here. We'll say if v is == to one, print v is one elif v == two, print v is two, and we could do as many of these as we wanted to.

So, when we save this and run it, you'll notice we get v is one. And if we change this one to, say, a three, save that and run it, then we get v is three, but if we make this a seven, then save that and run it, nothing at all happens. Now, if you want to have a default case, you can simply say, else: and this still gets executed if none of the conditions turn out to be true. Say, print v is some other thing, and save that and run it, and we get V is some other thing, because v is seven.

So, the thing to understand about this is that only one of these suites is ever going to be executed. So, you have if, and an expr...

Setting multiple choices with elif
Video duration: 2m 14s 6h 36m Beginner

Viewers:

Setting multiple choices with elif provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...