DEV Community

Cover image for Python for Javascript Newbies
Mary Alice
Mary Alice

Posted on

Python for Javascript Newbies

As a fledgling programmer, you quickly start exploring the endless possibilities—what other projects can I work on? What skills do I need to master? For me, all roads are pointing to Python. Can I really start learning a new programming language when I don't even have a mastery of JavaScript?

Scary Name 🐍 Friendly Language

Python is remarkably readable, and the variables feel strikingly similar to those in JavaScript. Proponents of Python laud the concise, clear, and readable syntax. The simplicity of its language makes it a popular first language for burgeoning developers who want to start coding immediately.

Now I wonder, will their similarities only confuse me? So I'm putting together my first Python primer—a simple cheat sheet designed to help me (and maybe you!) leverage our JavaScript knowledge to get a head start in Python.


This list will work backward: from the most similar to the drastic differences. Of course, I'll focus on the aspects I think will confuse me the most.

Straight Across Similarities

  • High Level Interpreted Programming Languages
  • Support functional and imperative paradigms
  • Lexical Scoping
  • Object Oriented

Primitive Data-Types

  • Strings: sequences of characters wrapped in quotes, immutable.
  • Numbers: Python differentiates integers (int) and decimals (float) vs. JavaScript's singular Number type.
  • Booleans: JavaScript has true and false, Python True, False, and a bool() function.
  • No Value: Python employs a simple None in place of JavaScript's null and undefined.

Functional Equivalents

Javascript Python
Cases 🐪 camelCase 🐪 🐍 snake_case 🐍
Console Logs console.log('hello') print('hello')
Pseudocode // single line # single line
And when you can't be concise /* multi - line*/ ''' multi - line '''
[collections] [array] [list]
{key - value structure} {object} {dict}

Conditionals

if...else if...else vs if…elif…else

This one had me laughing at first. Python takes simplicity to the extreme with "elif"...because "else if" is just too long.

So this JavaScript

let weather = 'rainy'

if (weather ==== "sunny") {
   console.log("It's a great day to go outside!");
} else if (weather === "cloudy") {
   console.log("It might be a good idea to carry an umbrella.");
} else if (weather === "rainy") {
   console.log("Don't forget your umbrella!");
} else {
   console.log("Be prepared for anything");
Enter fullscreen mode Exit fullscreen mode

Becomes this in Python

weather = 'rainy'

if weather == "sunny":
    print("It's a great day to go outside!")
elif weather == "cloudy":
    print("It might be a good idea to carry an umbrella, just in case.")
elif weather == "rainy":
    print("Don't forget your umbrella!")
else:
    print("Weather condition is unknown. Stay prepared for anything!")
Enter fullscreen mode Exit fullscreen mode

Notice anything else strange in the code above? Maybe some unfamiliar syntax?

'AND NOW FOR SOMETHING COMPLETELY DIFFERENT' graphic from Monty-Python's Flying Circus

This is where I know I'll get confused

Syntax

Python formatting relies on whitespace. Indentations take the place of semicolons and parentheses. Code block separated with new lines and colons, with curly braces relegated to the key-value {dict} mentioned above. Advocates say this all works in favor of Python's famous simplicity, but I fear this clean code lacks the clarity found in JavaScript.

Declaration

Although on its face a simple rule, I chose to put the differences in variable declaration last in this list. Initiating with var was the first thing I learned in JavaScript, so it feels unnatural leaving it (along with let and const) out of variable creation.

We'll still need to declare functions. In Python, JavaScript's function keyword is replaced with def.

function add(a, b) {
   return a + b;
 }
Enter fullscreen mode Exit fullscreen mode
def add(a, b):
   return a+ b
Enter fullscreen mode Exit fullscreen mode

Transitioning from JavaScript to Python offers a unique blend of familiarity and challenge. With its clear syntax and versatility, Python offers an approachable yet powerful language for developers, especially those looking to expand their coding toolkit. As I continue to explore, it’s reassuring to know that with a solid foundation in JavaScript, I'm well on my way to becoming a "snake charmer".

Sources

Python 101 for JavaScript Developers
How to Learn Python for JavaScript Developers
W3schools Python Booleans
W3schools Python Introduction
Image created with Canva

Top comments (1)

Collapse
 
ehsanahmadzadeh profile image
Amir Ehsan Ahmadzadeh

Great post! I love how approachable you've made Python for JavaScript devs. One tip to explore as you dive deeper: check out Python’s type annotations. They’re optional but can make your code more robust and readable, especially for larger projects. Keep up the awesome work!