Locked lesson.
About this lesson
Formatting in Python is important! Let's look at lines and indentation, and single line If statements.
Exercise files
Download this lesson’s related exercise files.
Python Formatting.docx59 KB Python Formatting - Solution.docx
59 KB
Quick reference
Python Formatting
Python handles formatting simply.
When to use
Formatting is important in python.
Instructions
Remember to tab your code in conditional statements.
Remember that python ignores spaces.
Place multiple items on the same line by separating them with semi-colons ;
And place multiple lines items together using the back slash \
Hints & tips
- Python formatting is pretty straight forward!
- Remember tab spacing, semi-colons, and back slashes \
- 00:04 In this video I want to take just a couple of minutes and
- 00:06 talk about Python formatting.
- 00:08 And the reason why we're doing it now is we're starting to see with these
- 00:11 conditional statements different formatting things that we haven't
- 00:15 seen before.
- 00:15 So Python is very particular about spaces inside of blocks of code like this.
- 00:21 So here you see, like I mentioned, I just hit the tab key on my keyboard, and
- 00:26 it popped it over.
- 00:27 It really doesn't matter how far over you go, it just has to be consistent.
- 00:33 So, we could do print that, we could do, Hello there!,
- 00:37 whatever, and this will work as long as they're consistent.
- 00:42 Now, if we save this and write num greater than 5, Hello there.
- 00:47 Change this to 1, save and run it, num is not greater than 5, this still works.
- 00:52 So you'll notice these two blocks of code are indented differently.
- 00:56 I don't recommend that you do that you want to be consistent.
- 00:59 But in this case, it does work,
- 01:01 because as long as your specific blocks are indented the same, you're good.
- 01:06 So this is a block of code right here.
- 01:09 This is a second block of code.
- 01:10 And these two blocks of code don't have to be indented the same but
- 01:13 they really should be.
- 01:14 On the other hand if we went like this, and let's change this back to 6.
- 01:20 And if we save this, we're already getting error here, so
- 01:23 I think you know what's going to happen, boom, we get a big error.
- 01:26 Because inside of your block, your formatting,
- 01:29 your indentation has to be the same.
- 01:31 So just get in the habit of hitting the Tab key whenever you need to indent blocks
- 01:35 of code and you should be okay.
- 01:37 That's one sort of weird formatting thing in Python.
- 01:40 This is not the case for other programming languages.
- 01:42 JavaScript uses brackets and things like that to tell blocks of code.
- 01:48 Every programming language is different, Python uses these indentations, so
- 01:52 that's one thing.
- 01:53 The next thing I want to talk about is blank lines, and
- 01:55 this is sort of a formatting issue as well.
- 01:57 Python just completely ignores blank lines, so you could do this, right?
- 02:01 And this whole ifelse then it will still evaluate the same way,
- 02:05 Python just ignores all of these spaces that we've created, right?
- 02:09 Obviously you're not going to want to do that, but I find it useful to sort of,
- 02:13 use white space here in there to break apart blocks of code.
- 02:16 Just makes it easier to read, you might have a comment,
- 02:22 this is my conditional statement.
- 02:26 And in front of that you might want to add a little bit of space, whatever,
- 02:30 Python will ignore spaces, so that's handy to know.
- 02:33 So that's another formatting issue.
- 02:34 The last formatting thing I want to talk about is you can do a lot of stuff on one
- 02:38 line if you wanted to.
- 02:40 So we could say num_2 = 10.
- 02:45 Num_3 = 100.
- 02:49 And just keep going, and separate these things with semicolons.
- 02:53 Lost our colon, there we go.
- 02:55 Now, do I recommend that you do that?
- 02:56 No, probably not, but you can do it if you want to, Python allows that sort of thing.
- 03:02 Also, you can go, name = John,
- 03:11 Elder print(name), in fact we can just
- 03:16 remove all this, make it easier to read.
- 03:21 Save, run it again, okay, so John Elder.
- 03:23 So, we're adding multiple things using this backslash so
- 03:27 you can put things on multiple lines.
- 03:29 That's one formating thing that's kind of interesting.
- 03:31 So all on one line, you could use that.
- 03:34 Multiple lines, you can use that.
- 03:36 And you could see it smooshes out John Elder into name even though this is on
- 03:41 multiple lines.
- 03:42 Finally, very quickly, we can do one line if statements too.
- 03:47 So, Python will allow you to string these things along just like this,
- 03:52 we run this, num is greater than 5 all on one line.
- 03:55 I don't recommend that you ever do that because it's just hard to read, right?
- 03:58 A lot of times your if statement will have multiple things.
- 04:01 You're going to have another print, another print, whatever, and
- 04:05 you just don't want to put all those things on one line.
- 04:08 But in a pinch, you can do that, and
- 04:09 there are certain circumstances where you might want to do that.
- 04:12 So those are just some Python formatting issues that I wanted to address.
- 04:16 Pretty simple, we'll see these moving forward.
- 04:18 In the next video we're going to look at membership operators.
Lesson notes are only available for subscribers.