Show Menu
Cheatography

Variables: JS vs Python Cheat Sheet by

This cheat sheet is for JS developers transforming to Python.

Variables

Javascript
Python
Declar­ation
using the var, let, or const keywords
Declar­ation
Created simply by assigning a value. No explicit declar­ation is required.
Dynami­cally typed
The type of a variable can change during runtime.
Dynami­cally typed
Allow variables to hold values of different types throughout their lifetime.
Scoping
Variables have functi­on-­level scope, meaning they are accessible within the function they are defined in. Variables declared with var are also hoisted, allowing them to be accessed before their actual declar­ation.
Scoping
Variables have block-­level scope, meaning they are accessible within the block they are defined in. Python does not have variable hoisting.
Variables in JavaScript typically follow camelCase naming convention
(e.g., myVari­able, firstName, totalA­mount).
Variables in Python typically follow snake_case naming convention
(e.g., my_var­iable, first_­name, total_­amo­unt).
Variables can be declared without an initial value, and they will have the value undefined until assigned a value. variables do not have default values.
Variables need to be assigned an initial value when they are created. Otherwise, a NameError will occur. variables have default values of None for non-pr­imitive data types, and 0 for primitive data types.
Variables declared outside of any function or block have global scope and can be accessed from anywhere in the code.
Variables declared outside of any function or block have global scope, but accessing them from within a function requires the global keyword.
The null and undefined values are different. null indicates that a variable has no value, while undefined indicates that a variable has not yet been defined.
There is only one value for "no value", which is None
JavaScript has a dynamic type system, which means that the type of a variable can change at runtime.
Python has a static type system, which means that the type of a variable is known at compile time.
variables can be assigned values using the equal sign (=)
variables can be assigned values using the equals sign (=) or the colon (:)
variables are mutable by default. This means that their values can be changed after they are assigned.
variables are immutable by default. This means that their values cannot be changed after they are assigned.
two variables are equal if they have the same value, regardless of their type
two variables are equal if they have the same type and the same value.
primitive data types: undefined, Boolean, String, Number, BigInt, Symbol, and null
primitive data types: int, float, bool, and str
type coercion is automatic. This means that if you try to perform an operation on two values of different types, the values will be automa­tically converted to the same type before the operation is performed.
type coercion is not automatic. You must explicitly convert values to the desired type before performing an operation on them.

Strings

Javascript
Python

Array or List

Javascript
Python
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Web Programming Cheat Sheet
            Python 3 Cheat Sheet by Finxter