DEV Community

Rubin
Rubin

Posted on

6

Python for nodejs developers

A cheatsheet on python and nodejs equivalents for developers who use both or are planning a switch from one to another.
This covers the basics

Topic Node.js (JavaScript) Python
Boolean true, false True, False
Number Numbers (integers, floats) Numbers (int, float, complex)
String 'Single' and "Double" quotes 'Single' and "Double" quotes
Array Array literals, array methods List type, list methods
Object {} Object literals Dictionary type
Function function keyword, arrow functions def keyword, lambda functions
Type Check typeof operator isinstance() function
Interpolation ${variable} syntax f-strings (f"{variable}")
If/Else if (condition) {} else {} if condition: ... else: ...
Ternary condition ? true : false true_value if condition else false_value
For Loop for (init; condition; iteration) {} for variable in iterable: ...
While Loop while (condition) {} while condition: ...
Switch switch (expression) { case x: ... } switch case x: ...
Array Slicing array.slice(start, end) list[start:end]
Copying Array [...array], array.slice() list.copy()
Appending array.push(element) list.append(element)
Prepending array.unshift(element) list.insert(0, element)
Uint8 Arrays Binary data handling, streams Used for similar purposes
Array Iteration forEach, for...of loop for item in list: ...
Looping for...in loop for key in dictionary: ...
Mapping array.map(callback) map() function
Filtering array.filter(callback) filter() function
Reducing array.reduce(callback) reduce() function
Sorting array.sort(callback) sorted() function
Buffers Binary data handling, streams Used for similar purposes
Allocate Buffer Buffer.alloc(size) bytearray(size)
Endianness Native support Struct module for explicit endianness
Hex .toString(16) conversion hex() function
Compare .equals() method == or cmp() functions
Equals Buffer.equals() Direct equality check with ==
Maps Key-value pairs handling Used for similar purposes
Map Iteration map.forEach, for...of loop for key, value in map.items(): ...
Objects JavaScript objects, key-value pairs Python dictionaries
Default Values Default function parameters Default function arguments
Destructuring Destructuring assignment syntax Destructuring assignment syntax
Spread Operator Spread array elements Spread iterable elements
Rest Operator Collect function arguments Collect function arguments
Swapping [a, b] = [b, a] a, b = b, a
Classes ES6 class syntax class keyword
Constructors constructor() method init() method
Instantiation new ClassName() ClassName()
"this" Refers to current object Refers to instance within class
Files fs module for file operations Built-in file functions
Creating Files fs.writeFileSync(path, data) open(path, mode)
Opening Files fs.openSync(path, mode) open(path, mode)
Writing to Files fs.writeFileSync(path, data) write() method
Reading from Files fs.readFileSync(path, encoding) read() method
Closing Files Automatically closed after operations close() method
Deleting Files fs.unlinkSync(path) remove() method
File Descriptors Not commonly used Utilized for low-level I/O operations
JSON Built-in JSON object Built-in json module
Parsing JSON JSON.parse(jsonString) json.loads(jsonString)
Stringifying JSON JSON.stringify(jsonObject) json.dumps(jsonObject)
Asynchronous Programming Utilizes async/await syntax Utilizes async/await syntax
Errors Utilizes built-in Error object Utilizes built-in Exception classes
Try/Catch try { ... } catch (error) { ... } try: ... except Exception as error: ...
Exceptions throw new Error('Message') raise Exception('Message')
Regular Expressions Utilizes built-in RegExp object Utilizes built-in re module
Matching regexObj.test(string) or string.match(regex) re.match(regex, string)
Searching string.search(regex) re.search(regex, string)
Replacing string.replace(regex, replacement) re.sub(regex, replacement, string)
Splitting string.split(regex) re.split(regex, string)
Extracting Groups Capturing groups using parentheses Capturing groups using parentheses

Image of Bright Data

Feed Your Models Real-Time Data – Enhance your AI with up-to-the-minute data.

Utilize our live data feeds for dynamic model training, ensuring your AI systems are always ahead.

Access Real-Time Data

Top comments (0)

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay