Join Bill Weinman for an in-depth discussion in this video Selecting code with conditionals, part of Python 3 Essential Training.
So, continuing with our Python Quick Start, let's take a look at how conditionals work in Python. I am going ahead and make a copy of this conditionals.py, and it's a good idea to work with copies. I am just going to name this conditionals -working. We'll go ahead and open that. There we have an example of a conditional, a very simple conditional. It's if and else.
And you'll notice that the assignment - it's really cool. In Python, you can assign multiple values at a time, so this assigns a 0 to a and assigns 1 to b. So that makes a nice little test for the conditional. If a is less than b, which it is, then it will print this. You'll also notice, in Python, you do the replacement, and this is Python 3; Python 2 works differently. In Python 3, these curly braces will be replaced by the values in the format, and we'll get more into how this works exactly, and some of the details of it, later on when we talk about strings.
But in fact this format is a method of the string object. Any place where you have a string, it allows you to do these kinds of replacements; it's very useful. So, when we run this - and we'll get this one because a is less than b, and unless we were to change these values and then maybe we could get this one. So let's go ahead and run it, and I'll run it here in Eclipse, and I'll select Python Run, and there we go. a is less than b, and see there is the value 0 and 1.
If I change this, if I make this say a 5, and I save that and run it, a is not less than b anymore. So the syntax of the conditional is, if, and then you have the condition, and you have a colon. This is how blocks work in Python. You'll see this same type of syntax and loops and functions and classes, where you have the colon and then you have code that's indented. There aren't any braces signifying a block of code.
Blocks are actually called suites in the Python documentation, and they simply consist of code that is indented under the level of whatever it is that's controlling it. So here we are controlling it with a conditional, and we have the colon, and then we have this code indented. Now four spaces of indenting is considered standard in Python. It's actually not required. I can make this one space and do the same down here and it would still work, save and run, but four is traditional.
So we are just going to go ahead and go with the tradition here and save that and run it, and there we have it. Python also has conditional expressions. If you are familiar with any C-based languages, you'll be familiar with something that might look like this, where you would say print, and you would have a condition a < b and a question mark, and then one value, and a colon, and then another value. If the condition were true, you would get the first one; if the condition were false, you would get the second one.
Python has something like this, but it works very differently. Here, you would say "foo" if a < b else "bar". If I save that and run it, then we get bar because a is not less than b. If I put this back to a 0, like we had it originally, and save and run, now we get foo because a is less than b. So this is how conditional expressions work in Python. A lot of the syntax that you will see here is a little bit different than in other C-based languages.
And that's because the designers of Python, they didn't consider themselves be beholden to tradition, and instead they wanted to do things that seem to flow better or make sense in some way. And actually, this does have its own set of reason in that you have print "foo" just like you would have if you didn't have a condition. You only really care about having the condition if you have another value. So you have this whole if-else all in one place, and it's spelled out; it's not punctuation marks that might just be hard to remember or hard to read.
It's very clear: "foo" if a < b else "bar"; it's kind of readable. So that is how conditions work in Python 3. You have the traditional if-else, and you have the conditional expressions.
- A Python 3 quick start for experienced developers
- Creating functions and objects
- Using Python's built-in objects and classes
- Repeating code with loops and iterators
- Understanding and using conditional expressions
- Creating sequences with generators
- Reusing code with objects and libraries
- Handling errors with exceptions
Skill Level Beginner
Q: The installation process for the PyDev Eclipse plug-in doesn’t work as described in the videos. What should I do?
A: The version of Eclipse used in the recording of Python 3 Essential Training was Eclipse 3.5.2 (Galileo SR2) and the version of PyDev was 1.5.7. Soon after recording, the Eclipse project was updated to version 3.6, called Helios. As of this writing, the current version of Eclipse is 3.6.1 (Helios SR1).
Around the same time as these updates were being released, the PyDev project was updated as well. As of this writing, the current version of PyDev is 1.6.2. If you are using these most recent versions, the procedure for installing Eclipse itself has not changed, but the process documented in the videos for installing PyDev may not work.
We will be releasing new versions of the videos soon, but the author has posted a document describing the new installation procedure at:
Q: How do you install the pydev-interpreter in Eclipse Indigo on Mac OS X Lion? It's significantly different than what's shown in the video "Installing Python 3 and Eclipse for Mac."
A: Since this course was publishing, there have been upgrades to Mac OS X and Eclipse. In this scenario, after installing Python and Eclipse and the Pydev interpreter, there is a different directory to go to when modifying the preferences. As instructed in the movie (at around 6:40) restart Eclipse and then go to Eclipse > Preferences and drill down to Pydev > Interpreter - Python in the sidebar. Click New and in the Select interpreter dialog that opens, click Browse.
The Open dialog box will open, but does not appear to display your hard drive. You must press and hold the Command+Shift+Period keys. This will display all hidden files on your system. Navigate to the new path ~/Library/Frameworks/Python.framework/versions/3.2/bin/python 3.2 and click Open.
You should be able to proceed normally from there.