As a developer coming from a JavaScript background, I've found it challenging to adjust to Python's syntax and structure. The differences, especially the lack of curly braces and reliance on indentation, can be confusing.
To make my transition smoother, I created this cheat sheet comparing common JavaScript and Python syntax. It serves as a quick reference to help navigate the key differences and similarities between the two languages.
I hope this resource will make it easier for anyone else facing the same challenges in learning Python.
Concept | JavaScript | Python |
---|---|---|
Variable Declaration | let x = 10; |
x = 10 |
Data Types |
String , Number , Boolean , Object , Array
|
str , int , float , bool , list , dict
|
Function Definition | function myFunc() {} |
def my_func(): |
Function Call | myFunc(); |
my_func() |
Conditional Statements | if (x > 10) {} |
if x > 10: |
Loops | for (let i = 0; i < 5; i++) {} |
for i in range(5): |
While Loop | while (x < 10) {} |
while x < 10: |
Array/List | let arr = [1, 2, 3]; |
arr = [1, 2, 3] |
Object/Dictionary | let obj = { key: 'value' }; |
obj = {'key': 'value'} |
Accessing Elements |
arr[0] , obj.key
|
arr[0] , obj['key']
|
String Concatenation | let str = 'Hello ' + 'World'; |
str = 'Hello ' + 'World' |
Template Literals | `Hello ${name}` |
f'Hello {name}' |
Comments | // This is a comment |
# This is a comment |
Exception Handling | try { } catch (e) { } |
try: ... except Exception as e: |
Importing Modules | import { myFunc } from './module'; |
from module import my_func |
Class Definition | class MyClass {} |
class MyClass: |
Class Instantiation | let obj = new MyClass(); |
obj = MyClass() |
Arrow Functions | const myFunc = () => {} |
my_func = lambda: ... |
Boolean Logic |
&& , || , !
|
and , or , not
|
Indentation vs. Curly Braces
JavaScript:
function greet(name) {
if (name) {
console.log("Hello, " + name + "!");
} else {
console.log("Hello, World!");
}
}
greet("Alice");
Python:
def greet(name):
if name:
print("Hello, " + name + "!")
else:
print("Hello, World!")
greet("Alice")
Whitespace Sensitivity
JavaScript:
if (true) {
console.log("This is true.");
} else {
console.log("This is false.");
}
Python:
if True:
print("This is true.")
else:
print("This is false.")
In Python, if you accidentally use inconsistent indentation, you will get an IndentationError
:
if True:
print("This is true.")
print("This will cause an error.") # Incorrect indentation
Data Structures
Javascript
// Array
let fruits = ["apple", "banana", "cherry"];
// Object
let person = {
name: "Alice",
age: 30,
isStudent: false
};
// Set
let uniqueNumbers = new Set([1, 2, 3, 1, 2]);
Python
# List
fruits = ["apple", "banana", "cherry"]
# Dictionary
person = {
"name": "Alice",
"age": 30,
"is_student": False
}
# Set
unique_numbers = {1, 2, 3, 1, 2}
Function Definitions
JavaScript
// Function Declaration
function add(a, b) {
return a + b;
}
// Arrow Function
const multiply = (a, b) => a * b;
Python
# Function Definition
def add(a, b):
return a + b---
---
# Lambda Function
multiply = lambda a, b: a * b
Error Handling
JavaScript
try {
let result = riskyFunction();
} catch (error) {
console.error("An error occurred:", error);
}
Python
try:
result = risky_function()
except Exception as e:
print("An error occurred:", e)
Modules and Packages
JavaScript
// Importing a module in Node.js
const fs = require('fs');
// Using ES6 module syntax
import { readFile } from 'fs';
Python
# Importing a module
import math
# Using a specific function from a module
from math import sqrt
Type System
JavaScript
// Dynamically typed
let value = 42; // number
value = "Hello"; // string
Python
# Dynamically typed
value = 42 # int
value = "Hello" # str
If you have any suggestions or additional comparisons that could improve this cheat sheet, please feel free to share your thoughts in the comments.
Thank you for reading!
Top comments (1)
One more thing: don't expect python modules to work like js modules or it will drive you mad haha