Expressing an if/else condition in a simple statement makes it possible to say more with less. Experienced trainer David Blaikie illustrates several uses of the ternary operator and explains how it can be combined with other statements to create powerful “one-liners” that enhance the logical constructions of your arguments. Novice Python programmers who have a basic understanding of conditional statements and variable assignment will learn how to code concisely with the ternary operator.

Sign up for Safari now and get 10 days free access to more Python training with Learning Paths.


David Blaikie is a New York City-based engineer and trainer. He has taught Python, Advanced Python, Perl, and Advanced Perl at New York University’s School of Professional Studies, and has conducted corporate Python training for companies such as Google, Cisco, and Bloomberg, as well as for the U.S. Navy. David has been a release engineer at Google and a software developer for AppNexus, Conde Nast, and several startups. He is also the author of the bestselling O’Reilly book “Python Beyond the Basics: Object-Oriented Programming.” David’s contact information can be found at www.clearpython.com.

Article image: Screenshot from "Ternary statements in Python"