- [Instructor] Beginning with Version 2.5,Python includes a ternary conditional operator.It's important to note that this operatordoes not work in versions of Python previous to Python 2.5.Of course, we're in Python 3.6 here.So we can demonstrate it here in Komodo.I've opened a working copy of ternary.pyfrom chapter four of the exercise files.This has a simple example of the ternary operator.

Down here on line five, we assign X to be this string,Feed the bear now!, if hungry.And we have this hungry variable above,which is set to logical value true.So that's being tested here in this if clause.And there's an else, it says, Do not feed the bear.So if the bear is hungry, then the string will beFeed the bear now!If the bear is not hungry, then it will beDo not feed the bear.

So when I save and run this it saysFeed the bear now!If I set hungry to false,remember zero works fine for false,I save and run and it says, Do not feed the bear.Now it's important to note that all of thisis required if you're going to havethe ternary conditional operator,you must have both if and else.You must have both clauses.Of course, if you want to,you can have else, set it to none.So when I save and run it will say None.

But you must have an else clause.It does not work with just the if clause.The ternary conditional operator is used occasionally,but it can be very convenient in some circumstances.

Resume Transcript Auto-Scroll

Author

Released

1/18/2018

Due to its power and simplicity, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. A thorough understanding of Python 3, the latest version, will help you write more efficient and effective scripts. In this course, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Learn how types and values are related to objects; how to use control statements, loops, and functions; and how to work with generators and decorators. Bill also introduces the Python module system and shows examples of Python scripting at work in a real-world application.