Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Whitespace in Expressions and Statements

When used properly, whitespace can help group things logically and make your code easier to read. In this lesson, you’ll learn how to use whitespace effectively as well as see when you should avoid adding it to your code.

You’ll learn how to use whitespace around binary operators, if statements, function calls, iterables, and when combining multiple operators in one statement. The lesson uses examples like the one below to show you what is recommended as well as patterns to avoid:

Python
# Recommended
y = x**2 + 5
z = (x+y) * (x-y)

# Not Recommended
y = x ** 2 + 5
z = (x + y) * (x - y)

00:00 When we talk about whitespace, we’re talking about the spaces placed between operators such as plus or minus signs or logical comparisons. Like blank lines, whitespace can be used to group things logically, but don’t use it too much, where things are spaced out all over a line.

00:15 In this video, you’ll learn when to use whitespace and a couple of places where you shouldn’t. In general, you should use whitespace around binary operators unless you’re setting a default value in a function like this.

00:37 This looks good. This does not. So let’s put that back. But if you’re setting something like x equal to 5 you don’t want to have it all bunched together like that.

00:50 Just put some whitespace before and after the equal sign. This applies to different types of assignment operators as well, so if you did something like x += 1, go ahead and do that.

01:02 If you’re going to do a comparison like x == y or z != y, and especially around the more readable comparisons, like x is None, or y not in lst.

01:20 This should be pretty clear because if you were to not use whitespace, now you’ve got an issue, where your code is not going to do what you want it to. This applies to your Booleans as well, so something like and and or.

01:42 If you have multiple operators in a statement, you should only add whitespace around the operator with the lowest priority.

01:53 The interpreter won’t care, but to a human reading it can make things easier to understand. Let’s take a look at something like y = x**2 + 5, or even something like z = (x+y) * (x-y). To a human, it becomes very easy to see that this section gets manipulated, and then this section, and then both sections are brought together. And likewise, here, we add this, then we subtract this, and then multiply those pieces together.

02:28 What you want to avoid is adding whitespace everywhere. So if you end up with something like this, now things are a bit too spaced out. This applies to if statements with multiple conditions as well, so something like if x > 5 and x % 2 == 0: print('something').

02:52 This might make it a little hard to see where this and actually comes into play. If you get rid of this whitespace and only leave it around the and, you can tell that you evaluate this and then you evaluate this, and then both of these sections must be True for this print statement to actually occur. And you’re not required to do this, but one thing that you should really make sure you do is use the same amount of whitespace before and after the operators. Never do something like this, where you have whitespace on one side and not on the other, or only on one side here and not on the other.

03:24 This makes things very confusing to read, and it’s not acceptable.

03:35 If you’re doing slices in a list, treat the colon operator (:) with the lowest priority. So something like this would be valid, just like something like this would be valid.

03:55 If you’re performing an extended slice, make sure that both colons have the same amount of whitespace around them. You don’t want to have something like this or like that, because now we start getting confused. Finally, if you omit a slice parameter, don’t have any whitespace there before the bracket. And this is okay.

04:18 There are a couple other places where you don’t want to add whitespace. We just talked about it with lists before the closing bracket—another place you don’t want to use whitespace is after the opening bracket.

04:31 So something like this, you would not want to do. And there’s that trailing whitespace that we also don’t want. Make sure that you don’t put any whitespace before a comma—so instead of this, do that—and also not before the opening parentheses of a function call.

04:52 So this is something you’d want to do, that is something that you don’t want to do. Let me fix that too, while we’re here. Similar to functions, if you’re going to be indexing or slicing a list, don’t put a space before the opening bracket.

05:10 And if you have a trailing comma before a closing parentheses, like in a tuple,

05:17 don’t have a space after the trailing comma.

05:25 Finally, you don’t want to use whitespace to align assignment operators.

05:37 This is okay, but something like this is not what you want to do. Remember that this is Python and not JavaScript. While this may seem to improve readability, let’s say you have to come back and change the name of a variable to something even longer. Now instead of changing this one line, you’ve got to go back and fix everything else here, and that’s not something you want to do.

06:07 The last place that you really don’t want whitespace is at the end of a line. This is also known as trailing whitespace, where if you have the end of a line of code and you just add a space.

06:19 This can cause a variety of errors that will break your code, and those little spaces can be very difficult to track down. You might note why I configured in my editor to show space characters as these little dots here. Let me get rid of that before I forget about it.

06:34 And that’s it! You’ve got some tips now on how to use whitespace effectively. If you try to think about things logically and group things as they happen, you’ll be on a good track to using whitespace the right way.

06:52 In the next video, we’ll cover some programming recommendations to make your code a little bit easier to read and a lot more consistent. Thanks for watching.

Become a Member to join the conversation.