Introduction to Python: 2018 Edition by Mark Lassoff & Julius Hernandez

Introduction to Python: 2018 Edition by Mark Lassoff & Julius Hernandez

Author:Mark Lassoff & Julius Hernandez [Lassoff, Mark]
Language: eng
Format: azw3, epub
Publisher: LearnToProgram.tv, LLC.
Published: 2018-04-10T04:00:00+00:00


Figure 6.13. The expected output from our sample program, where age = 23 is evaluated as true, since 23 >= 18.

If we change age = 17, then save and run the program again, we now get the Ineligible to buy alcohol output, since age=17 is evaluated as false, as it does not meet the age >= 18 condition[SW5].

Figure 6.14. The expected output from our sample program, where age = 17 is evaluated as false, since 17 >= 18.

As you can see from the above examples, the ternary operator puts the logic of the If…Else statements we discussed in the previous section all together in one line.

Let’s add a couple of lines using the ternary operator to our program.

Enter the following on the editor window:

citizen = “true”

print (‘You may vote’ if citizen == “true” else ‘You may not vote’)

After saving and running the program, since citizen = “true”, the output would be You may vote. If citizen = “false”, the output would be You may not vote instead.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.