Python Syntax - A Beginner’s Guide for JavaScript Developers
Posted — tutorial, python, javascript, coding, technical
I've been wanting to pick up another programming language for a while. This week, I took Codecadmy's Python course. It is absolutely true when people say that once you learn one programming language, it is easier to pick up others. The core concepts of programming, breaking down a project and working through it methodically, only has to be learned once. While there is a lot to learn with a new language, these simple syntax differences may help you get started if you are a JavaScript Developer looking to learn Python.

via GIPHY
Defining Variables
Variables are declared automatically when they are assigned. Keywords such as JavaScript's "var", "let" and "const" are unnecessary.
Two things worth noting here:
- Semicolons are not used to terminate statements, but they can be used if you want multiple statements on the same line.
- Booleans are capitalized in Python.
Easy money so far, right?

via GIPHY
Fun with Functions
If you're like me, it may take a while to get used to function syntax. Instead of using curly brackets ( {} ) to define function blocks, Python uses white space. The "def" key word is used to define a function, and colons are used to indicate the start of a program block.
More stuff worth noting:
- The convention in Python is to define functions and variables using joined_lower casing (classes use StudlyCaps casing).
- Indent using 2 spaces
- Parentheses are not required for the conditional statements.
- The "print" keyword is used to print to the console.
- To make a single-line comment, use "#".
Wait, it gets better!

via GIPHY
Range (and Lists)
Range is an amazing shorthand tool used to loop through items! It generates a list of numbers. If you provide a single number, like range(x), then a list will be generated with a range of 0 through x-1 (the stop number is not inclusive). So, range(10) will create a list range from 0–9. Range can get more complex and is definitely something worth reading about if you're interested in Python development.
As you can see, range is a powerful tool. Writing for i in range(10) in Python is the same as writing for (var i = 0; i < 10; i++)
in JavaScript.
Another amazing Python feature is that STRINGS ARE AUTOMATICALLY CONVERTED INTO LISTS OF CHARACTERS! What does this mean, exactly? In the code snippet above, we have the variable word set to "hello" on line 11. That variable can automatically become ["h", "e", "l", "l", "o"]
.

via GIPHY
Lists are similar to JavaScript arrays in functionality, meaning that you can access items by their index and loop through them with both mutable and immutable functions. Just think of the possibilities!
Conclusion
We've barely scratched the surface here. If you're a JavaScript Developer ready to learn Python, Codecademy's free course and Stack Overflow are great places to start. Happy coding!
Subscribe to be the first to get the latest from Leo!
Share This Article
About the Author: Leo Yockey

Leo Yockey (he/him/his) is the Founder of MangoBass Consulting. He is also a co-founder of Hackertunity and an advocate for underrepresented technologists. If you enjoy his writing, you can see more on his personal blog or twitter.