From the course: Python for JavaScript Developers

Braces - Python Tutorial

From the course: Python for JavaScript Developers

Start my 1-month free trial

Braces

- [Instructor] When you look at Python code for the first time, one thing immediately stands out. Here I am in 03_01.py. Now I want you to look at this code. And let's head over to 03_01.js to look at similar code in JavaScript. So you can see these two functions, and yes, indentation is used in order to group together statements but that's only for readability and style. What's really being used is curly braces. In Python we don't use curly braces, we use a colon and indentation. So if we go back here to 03_01.py you'll see that line two ends with a colon followed by two statements that are grouped together by indentation. Similarly line seven ends with a colon and then followed by two statements grouped together by indentation. Now, this can be a little bit strange to get used to but believe me, it becomes like second nature. Once you grow accustomed to it you will appreciate how clean the code looks and how straightforward it is to look at. Really do give it a chance. Now in the beginning if you see strange behavior around scope for instance, just count your indentation to make sure everything is aligned. We usually indent using four spaces, and then if you wanted to go one block in you would do an additional four spaces. And then if you get an error that has to do with indentation, once again, go through the process and count how many times you've indented a specific statement. Now, this shouldn't be too much of a stretch because like I said, in JavaScript indentation is expected. Our linter is usually there to make sure that we indent properly and our fellow programmers also expect our code to be properly legible and indented. So go ahead and give this some time and pay extra attention to it because it's one of those gotchas and those things that really take a little time in order to settle in.

Contents