Python VS JavaScript
Javascript Python cheatsheet
Tom Tarpey edited this page on May 26, 2019 · 24 revisions
Contents
Versions
JavaScript
The standard defining JavaScript (JS) is ECMAScript (ES). Modern browsers and NodeJS support ES6, which has a rich feature set. Older browsers might not support all ES6 features.
The website caniuse.com will show which browsers support specific JS features.
Python
Python 3.x is the current version, but there are a number of packages and sites running legacy Python 2.
On some systems, you might have to be explicit when you invoke Python about which version you want by running python2
or python3
. The --version
command line switch will tell you which version is running. Example:
Using virtualenv
or pipenv
can really ease development painpoints surrounding the version. See Development Environments, below.
Development Environments
JavaScript
For managing project packages, the classic tool is npm
. This is slowly being superseded by the newer yarn
tool. Choose one for a project, and don't mix and match.
Python
For managing project packages and Python versions, the classic tool is virtualenv
. This is slowly being superseded by the newer pipenv
tool.
Running Programs
JavaScript
Running from the command line with NodeJS:
In a web page, a script is referenced with a <script>
HTML tag:
Python
Running from the command line:
Comments
JavaScript
Single line:
Multi-line comments:
You may not nest multi-line comments.
Python
Single line:
Python doesn't directly support multi-line comments, but you can effectively do them by using multi-line strings """
:
Semicolons
JavaScript
Javascript ends statements with semicolons, usually at the end of the line. I can also be effectively used to put multiple statements on the same line, but this is rare.
Javascript interpreters will let you get away without using semicolons at ends of lines, but you should use them.
Python
Python can separate statements by semicolons, though this is rare in practice.
Whitespace, blocks
JavaScript
Whitespace has no special meaning. Blocks are declared with squirrely braces {
and }
.
Python
Indentation level is how blocks are declared. The preferred method is to use spaces, but tabs can also be used.
Functions
JavaScript
Define functions as follows:
An alternate syntax for functions is growing increasingly common, called arrow functions:
Python
Define functions as follows:
Python also supports the concept of lambda functions, which are simple functions that can do basic operations.
Arithmetic Operators
JavaScript
Operator | Description |
---|---|
| Addition |
| Subtraction |
| Multiplication |
| Division |
| Modulo (remainder) |
| Pre-decrement, post-decrement |
| Pre-increment, post-increment |
| Exponentiation (power) |
| Assignment |
| Addition assignment |
| Subtraction assignment |
| Multiplication assignment |
| Division assignment |
| Modulo assignment |
Python
The pre- and post-increment and decrement are notably absent.
Operator | Description |
---|---|
| Addition |
| Subtraction |
| Multiplication |
| Division |
| Modulo (remainder) |
| Exponentiation (power) |
| Assignment |
| Addition assignment |
| Subtraction assignment |
| Multiplication assignment |
| Division assignment |
| Modulo assignment |
Variables
Javascript
Variables are created upon use, but should be created with the let
or const
keywords.
var
is an outdated way of declaring variables in Javascript.
Python
Variables are created upon use.
Data Types
JavaScript
Multi-line strings:
Parameterized strings:
JS is weakly typed so it supports operations on multiple types of data at once.
Python
Multi-line strings:
Parameterized strings:
Python is generally strongly typed so it it will often complain if you try to mix and match types. You can coerce a type with the int()
, float()
, str()
, and bool()
functions.
Arrays/Lists
JavaScript
In JS, lists are called arrays.
Arrays are zero-based.
Creating lists:
Accessing:
Length/number of elements:
Python
In Python, arrays are called lists.
Lists are zero-based.
Creating lists:
Accessing:
Length/Number of elements:
Slices
In Python, we can access parts of lists or strings using slices.
Creating slices:
Starting from the end: We can also use negative numbers when creating slices, which just means we start with the index at the end of the array, rather than the index at the beginning of the array.
Tuples
Python supports a read-only type of list called a tuple.
List Comprehensions
Python supports building lists with list comprehensions. This is often useful for filtering lists.
Objects/Dicts
JavaScript
Objects hold data which can be found by a specific key called a property.
Creation:
Access:
Python
Dicts hold information that can be accessed by a key.
Unlike objects in JS, a dict
is its own beast, and is not the same as an object obtained by instantiating a Python class.
Creation:
Access:
Dot notation does not work with Python dicts.
String Formatting
JavaScript
Converting to different number bases:
Controlling floating point precision:
Padding and justification:
Parameterized strings:
Python
Python has the printf operator %
which is tremendously powerful. (If the operands to %
are numbers, modulo is performed. If the left operand is a string, printf is performed.)
But even %
is being superseded by the format
function.
Tons of details at pyformat.info.
Also see printf-style String Formatting for a reference.
Booleans and Conditionals
JavaScript
Literal boolean values:
Boolean operators:
Operator | Definition |
---|---|
| Equality |
| Inequality |
| Strict equality |
| Strict inequality |
| Less than |
| Greater than |
| Less than or equal |
| Greater than or equal |
The concept of strict equality/inequality applies to items that might normally be converted into a compatible type. The strict tests will consider if the types themselves are the same.
Logical operators:
Operator | Description |
---|---|
| Logical inverse, not |
| Logical AND |
` |
The not operator !
can be used to test whether or not a value is "truthy".
Example:
Python
Literal boolean values:
Boolean operators:
Operator | Definition |
---|---|
| Equality |
| Inequality |
| Less than |
| Greater than |
| Less than or equal |
| Greater than or equal |
Logical operators:
Operator | Description |
---|---|
| Logical inverse, not |
| Logical AND |
| Logical OR |
The not
operator can be used to test whether or not a value is "truthy".
Example:
for
Loops
for
LoopsJavaScript
C-style for
loops:
for
-in
loops iterate over the properties of an object or indexes of an array:
for
-of
loops access the values within the array (as opposed to the indexes of the array):
Python
for
-in
loops over an iteratable. This can be a list, object, or other type of iterable item.
Counting loops:
Iterating over other types:
while
Loops
while
LoopsJavaScript
C-style while
and do
-while
:
Python
Python has a while
loop:
switch
Statement
switch
StatementJavaScript
JS can switch on various data types:
Python
Python doesn't have a switch
statement. You can use if
-elif
-else
blocks.
A somewhat clumsy approximation of a switch
can be constructed with a dict
of functions.
if
Conditionals
if
ConditionalsJavaScript
JS uses C-style if
statements:
Python
Python notably uses elif
instead of else if
.
Classes
JavaScript
The current object is referred to by this
.
Pre ES-2015, classes were created using functions. This is now outdated.
JS uses prototypal inheritance. Pre ES-2015, this was explicit, and is also outdated:
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; it's just that the details are hidden from the developer.
JS does not support multiple inheritance since each object can only have one prototype object. You have to use a mix-in if you want to achieve similar functionality.
Python
The current object is referred to by self
. Note that self
is explicitly present as the first parameter in object methods.
Python 2 syntax:
Python 3 syntax includes the new super()
keyword to make life easier.
Python supports multiple inheritance.
Last updated