Python syntax for conditional statements is simple and intuitive. These statements work as expected using indented blocks for the associated code.
- [Instructor] The Python syntax for conditional statements is consistent with the rest of the language. Here in Komodo, I've opened a working copy of conditional.py from Chapter Four of the exercise files. This is an example of the conditional statement in Python and it should look familiar. We have if, elif, and else. If and elif require a condition. In this case, we're just using the boolean constant true, and the boolean constant false, and else is optional.
So if I save this and run it, we see if is true, but if I make that false, the execution should go all the way to the else, and we have neither is true, and if I make the elif true, then it has elif true. Of course, I can have as many elifs as I want to, so I'll number them, elif one, elif two.
Let's move this down a little bit so we have more room to edit, three and four, and I can just pick one, and say I'll make this one true, and of course, it will say elif three is true. So you can cascade the else, ifs to as many as you want, and this comes in handy when you want to do something like this, x equals five. If x equals zero, then zero is true, and you need to make that a double equals.
If x equals one, and one is true, or we can just leave these numbers here 'cause those won't match up, and so this works very much like a switch statement does in other languages, and when I save and run this, we see that elif five is the one that is true, and of course, if we change this to 42, then we get the none of them are true. So the conditional statement in Python is simple and easy.
It uses the familiar if, else, elif keywords, and the typical Python indented block syntax.
- 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 Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m 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.