Javascript vs Python Syntax Cheatsheet

Kevin Botero
Apr 26 · 9 min read

I was looking for a quick way to learn Python after I’ve learned Ruby and Javascript. I needed a quick cheat sheet with some syntax differences and high-level concept differences of the two languages.

Often, when I would come across Python code online, I would be able to make out the logic — like I’m sure most programmers would after learning Javascript. Writing code in python? That’s a different story.

This cheat sheet serves as high-level comparisons between the two languages to get you started on the track to learning Python after Javascript or just wanting to improve your understanding of Python code on online documentation. Bookmark this page to keep it handy! Happy programming.

Note: This is written with the latest version of Python at the time of writing- Python 3.

Whitespace / Blocks

Javascript

Whitespace is Javascript has no meaning. Blocks of code are declared with braces { }. Indentation is used for readability.

Python

In Python, the amount of whitespace tells the computer what is part of a function and what is not part of that function. You can use space or tab. Without proper indentation, you will get an error.

Commenting

Javascript

Single line:

Multi-line:

Python

Single line:

Multi-line:

Python doesn't have multi-line comments, but you can use its multi-line string option.

Printing to Terminal

Javascript

Python

Variables

Javascript

Javascript has several ways to declare a variable using “let” or “const”, depending on whether the variable needs to be updated in the future (“var” can also be used, but it’s an outdated way of declaring variables).

Python

Arithmetic Operators

The arithmetic operators between Javascript and Python are identical — except for one. Python is missing the pre-decrement/ post-decrement, and pre-increment/ post-increment operators.

Javascript extra operators:

Python extra operator:

One thing to note- since Python treats all numbers as floats (decimal numbers), you can use the double division sign // to get an integer. This is useful for finding indexes.

Literal Boolean Values

Javascript

JS literal booleans use the lowercase version.

Python

PY literal booleans use the uppercase version.

Boolean Operators

The boolean operators between Javascript and Python are identical — except for two extra JS operators. Python is missing the strict equality/inequality operators.

Javascript extra operators:

Example of strict equality/inequality:

With Python, since we don’t have strict equality/inequality operators, this means we would need to convert the data to the same type before making comparisons.

Logical Operators

Javascript

Python

Python’s logical operators are read friendly

If Conditionals

Most notable difference: Python uses “elif” instead of “else if”.

Javascript

Python

Arrays / Lists

  • In JS, they are called arrays. In Python, they are called lists.
  • Python: 2D Lists: heights = [ [“Noelle”, 61], [“Ava”, 70], [“Sam”, 67], [“Mia”, 64] ]

Javascript

Creating:

Accessing:

Length/Number of elements:

Adding/removing items

Python

Creating:

Accessing:

Length/Number of elements:

Adding/removing items

Bonus: How to remove from a 2D list

Tuples

Python supports a read-only type of list called a tuple. It’s immutable — you can’t change or modify it.

List Methods

Additional ways of working with lists

Insert:

A list method to insert an element from a specific index of a list

Javascript .splice()

Python .insert()

Pop:

A list method to remove an element from a specific index or end of the list.

Javascript .pop()

In Javascript, this method can only be used to remove the last element on the list.

Python .pop()

In Phython, this method can be used to remove an element from specific index, or the last element on the list.

Slicing:

A list method to extract only a portion of the list.

Javascript .slice()

Python [start : end]

In Python, we perform slicing using the following syntax: [start: end]. start is the index of the first element that we want to include in our selection.end is the index of one more than the last index that we want to include.

Slicing in Python is very flexible. If we want to select the first n elements of a list or the last n elements in a list, we could use the following code:

For our fruits list, suppose we wanted to slice the first three elements.

We can also do all but n last elements of a list:

Sorting:

A method and a built-in function to sort a list. Both Javascript and Python have the .sort() method that will sort either alphabetically or numerically.

Javascript .sort()

Note: If numbers are sorted as strings, “25” is bigger than “100”, because “2” is bigger than “1”.

Python .sort() / .sorted()

.sort() Sort modifies the list directly.

.sort() also provides us the option to go in reverse easily. Instead of sorting in ascending order, we can do so in descending order.

.sorted() is different from .sort() because it comes before a list, instead of after. And, It generates a new list instead of modifying one that already exists.

Bonus for Python:

.range()

A built-in python function to create a sequence of integers. The function range() takes a single input, and generates numbers starting at 0 and ending at the number before the input.

This creates a range object. If we want to use this object as a list, we need to convert it using a built-in function called list().

We can also call range() with two inputs, and create a list that starts at a different number.

We can also call range() with three inputs, and create a list that “skips” numbers. For example, we’ll start at 1 and skip in increments of 10 between each number until we get to 100:

.count()

Unlike Javascript, Python has a built-in way to count occurrences of an item in a list without having to write a function for it.

This method even works to count element appearances in a 2D list.

For Loops

Iteration.

Javascript

Python

We can use the range() function to count:

Or, we can iterate over types:

While Loops

While the condition is true.

Javascript

Python

Functions

Defining functions.

Javascript

Arrow functions

Python

Strings

Strings in Python and Javascript both use single and double-quotes. String methods do differ between the two. Here are a few examples:

Javascript .toUpperCase() / toLowerCase()

.toUpperCase() returns the string with all uppercase characters.

.toLowerCase() returns the string with all lowercase characters.

Python .upper() / .lower()

.upper() returns the string with all uppercase characters.

.lower() returns the string with all lowercase characters.

You can see a list of available Python string methods here.

Object / Dictionaries

In Javascript, objects hold data that can be found using a key called a property. In Python, these key/value pairs are called dictionaries.

Javascript

Access:

Python

Access:

Classes

A class is a template for a data type. Both Javascript & Python uses theclass keyword with a capitalization convention.

Javascript

Modern JS introduced the class keyword and a syntax more familiar to most other OOP languages. Note that the inheritance model is still prototypal inheritance.

Access:

Python

Python has a class constructor method that is called “dunder” (ex: __init__()). This stands for double underscores.

Access:

You can access all of an object’s class variables with object.variable syntax.

Note: There’s another dunder method called __repr__(). This is a method we can use to tell Python what we want the string representation of the class to be.

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store