Python | JavaScript |
---|---|
String immutable | String immutable |
Integer whole numbers Float decimals |
Number - includes NaN and Infinity |
Bigint - for integer values larger than 2^53 or less than -2^53 - in Jan 2020, not fully supported by all browsers |
|
Boolean - True and False are capitalized |
Boolean - true and false are lowercase |
Dictionary | Object - includes Array, null, RegExp, Math, Map, WeakMap, Set, WeakSet, Intl, Reflect, WebAssembly, and JSON |
undefined | |
Function - includes Date, Promise, ArrayBuffer, DataView, Proxy, and Error - arguments is an array-like object available inside of a function |
|
Symbol | |
List: similar to an array | |
Tuple: similar to a list, but immutable and order matters e.g (1, 3, 20) could represent hours, minutes, and seconds |
Python | JavaScript | |
---|---|---|
check type with | type() | typeof |
to a string | str() | data.toString() or type coercion by concatenation |
to a number | parseInt() - expects a string and parses up to the first non-digit and returns whatever it parsed - The second param, radix, is 10 by default Number() - will parse any type and try to convert it to a number - returns NaN if it can't convert it to a number |
Python | JavaScript | |
---|---|---|
equal | == |
=== |
not equal | != |
!== |
not | not |
! |
or | or |
|| |
and | and |
&& |
Python | JavaScript | |
---|---|---|
you can break out of while and for loops | yes | yes |
for loops start with... | 0, for x in range(5) iterates 0-4 pass two params to indicate a different starting point for x in range(1, 5) pass a third param to change the step for x in range(1, 5, 2) Note that 5 is not inclusive |
whatever is defined, for(var i = 0; i < 5; i++){} for - loops through a block of code a number of times for/in - loops through the properties of an object for/of - loops through the values of an iterable object |
Python | JavaScript | |
---|---|---|
define a function | def functionName(): |
function functionName() {} or () => {} or assign the function to a var to name it |
return value if no return value is specified | none | undefined |
what can be returned | multiple values | one value |
scoped self reference | self |
this |
Python | JavaScript | |
---|---|---|
check the length of a string | len(<string>) |
<string>.length |
iterate over each character in a string | for character in string |
|
access the char at index i starting at zero | <string>[i] |
|
access the substring at index i ending at index j-1 | <string>[i:j] If i is omitted, default is 0. If j is omitted, default is len(string). |
|
index of a char | <string>.index |
<string>.indexOf |
slice of a string | <string>[0:3] |
<string>.slice(0, 3) |
includes | if <substring> in <string> |
'<string>'.includes('<substring>') |
lowercase | <string>.lower() |
|
uppercase | <string>.upper() |
|
remove white space | left: <string>.lstrip() right: <string>.rstrip() both: <string>.strip() |
|
the number of times substring is present in the string | <string>.count(<substring>) |
|
only numeric characters, returns boolean | <string>.isnumeric()) |
|
only alphabet characters, returns boolean | <string>.isalpha()) |
|
split | <string>.split(<delimiter>)) |
|
replace all occurrences returns a new string |
<string>.replace(<old>, <new>)) |
|
join a list of strings returns a new string |
<delimiter>.join(<list of strings>) |
Sequences (Lists/Tuples | Arrays)
Python | JavaScript | |
---|---|---|
return the length of a sequence | len(<list>) |
<array>.length |
iterate over each element in the sequence | for <element> in <sequence> |
|
check if an element is in a sequence | if <element> in <sequence> |
|
access the element at index i i starts at zero |
<sequence>[i] |
|
access the substring at index i ending at index j-1 | <sequence>[i:j] If i is omitted, default is 0. If j is omitted, default is len(string). |
|
iterate over both the indexes and the elements | for index, <element> in enumerate(<sequence>) |
|
add an item | <list>.append('item') |
<array>.push('item') |
insert an item | <list>.insert({index, 'item') |
- use list comprehension to create a list
[<expression> for <variable> in <sequence> if <condition>]
e.g.:[x for x in range(0, 101) if x % 3 == 0]
Lists || Array
Python | JavaScript | |
---|---|---|
return the length of a sequence | len(<list>) |
<array>.length |
replaces the element at index i with x | <list>[i] |
<array>[i] |
inserts x at the end of the list | <list>.append(x) |
<array>.push(x) |
inserts x at index i | <list>.insert(i, x) |
|
returns the element a index i, also removing it from the list | <list>.pop(i) |
|
removes the first occurrence of x | <list>.remove(x) |
|
sorts the items | <list>.sort() .sort modifys the list, use sorted(<list>, <key>) to return a new list |
|
reverses the order of items | <list>.reverse() |
|
removes all the items | <list>.clear() |
|
creates a copy of the list | <list>.copy() |
|
appends all the elements of other_list at the end of list | <list>.extend(other_list) |
Dictionaries | Objects
Python | JavaScript | |
---|---|---|
delete a property | delattr(<obj>, <property>) and del <obj>.<property> |
delete <obj>.<property> |
key names | can be any immutable data type: numbers, booleans, stings, or tuples | strings or symbols |
iterates over each key in the dictionary | for key in <dictionary> |
|
iterates over each key,value pair in the dictionary | for key, value in <dictionary>.items() |
|
checks whether the key is in the dictionary | if key in <dictionary> |
|
accesses the item with key key of the dictionary | <dictionary>[key] |
|
sets the value associated with key | <dictionary>[key] = value |
|
returns the element corresponding to key, or default if it's not present | <dictionary>.get(key, default) |
|
returns a sequence containing the keys in the dictionary | <dictionary>.keys() |
|
returns a sequence containing the values in the dictionary | <dictionary>.values() |
|
updates the dictionary with the items coming from the other dictionary. Existing entries will be replaced; new entries will be added. | <dictionary>.update(<other_dictionary>) |
|
removes all the items of the dictionary | <dictionary>.clear()) |
Python | JavaScript | |
---|---|---|
print properties and methods of a class | dir(<type or class>) |
|
print properties and methods of a class including arguments and function definitions | help(<type or class>) |
|
create a class without properties | class <capitalized class name> pass |
|
create a class with properties | class <capitalized class name> color = '' |
|
create a class with a method | class <capitalized class name> def <function_name>(self): |
|
constructor | class <capitalized class name> def __init__(self, <additional params>): |
|
create a print friendly message for an object | class <capitalized class name> def __self__(self): |
|
Docstring | """a brief text in the first line of the function definition that explains what a function does wrapped in triple quotes""" |
- instance variables: Variables that have different values for different instances of the same class
- if you try to print an object, it will print the location in the computer's memory
- docstring is a brief text that explains what something does
- always initialize mutable attributes in the constructor
Python | JavaScript | |
---|---|---|
import a module to use, e.g. random, datetime, timedelta... | import <module> |
Python | JavaScript | |
---|---|---|
comment out code | # |
// or for long sections /* comment */ |
else if conditions | elif |
else if |
end param inside of print | acts like a line break in a print statement | NA |
max recursive function calls the language allows | 1000 |