Tom Tarpey edited this page on May 26, 2019 · 24 revisions
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 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.
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.
For managing project packages and Python versions, the classic tool is virtualenv
. This is slowly being superseded by the newer pipenv
tool.
Running from the command line with NodeJS:
In a web page, a script is referenced with a <script>
HTML tag:
Running from the command line:
Single line:
Multi-line comments:
You may not nest multi-line comments.
Single line:
Python doesn't directly support multi-line comments, but you can effectively do them by using multi-line strings """
:
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 can separate statements by semicolons, though this is rare in practice.
Whitespace has no special meaning. Blocks are declared with squirrely braces {
and }
.
Indentation level is how blocks are declared. The preferred method is to use spaces, but tabs can also be used.
Define functions as follows:
An alternate syntax for functions is growing increasingly common, called arrow functions:
Define functions as follows:
Python also supports the concept of lambda functions, which are simple functions that can do basic operations.
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
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 are created upon use, but should be created with the let
or const
keywords.
var
is an outdated way of declaring variables in Javascript.
Variables are created upon use.
Multi-line strings:
Parameterized strings:
JS is weakly typed so it supports operations on multiple types of data at once.
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.
In JS, lists are called arrays.
Arrays are zero-based.
Creating lists:
Accessing:
Length/number of elements:
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 hold data which can be found by a specific key called a property.
Creation:
Access:
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.
Converting to different number bases:
Controlling floating point precision:
Padding and justification:
Parameterized strings:
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.
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:
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
not
Logical inverse, not
and
Logical AND
or
Logical OR
The not
operator can be used to test whether or not a value is "truthy".
Example:
for
LoopsC-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):
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
LoopsC-style while
and do
-while
:
Python has a while
loop:
switch
StatementJS can switch on various data types:
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
ConditionalsJS uses C-style if
statements:
Python notably uses elif
instead of else if
.
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.
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.