If you need an expression that returns different values under different conditions, Python provides conditional expressions using a ternary conditional operator.
- [Instructor] Beginning with Version 2.5,…Python includes a ternary conditional operator.…It's important to note that this operator…does 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.py…from 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 be…Feed the bear now!…If the bear is not hungry, then it will be…Do not feed the bear.…
So when I save and run this it says…Feed 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 this…is required if you're going to have…
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficiently (2017)with Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.