Python for JavaScript Developers: A Quick Guide
JavaScript dominates the frontend, but Python rules AI, Data Science, and backend scripting. If you know JS, you already know 80% of Python. Here's the other 20%.
Syntax Comparison
Variables and Types
// JS
let name = "Amit";
const PI = 3.14;
let users = ["a", "b"];
let person = { name: "X", age: 20 };
# Python
name = "Amit" # No let/const
PI = 3.14 # Convention only
users = ["a", "b"]
person = { "name": "X", "age": 20 } # Dictionary
Functions
function greet(name = "World") {
return `Hello ${name}`;
}
def greet(name="World"):
return f"Hello {name}" # f-strings are like template literals
Loops
// JS
for (const user of users) {
console.log(user);
}
# Python
for user in users:
print(user)
Cool Python Features JS Doesn't Have (Yet)
List Comprehensions
Create lists in one line.
# Create squares of even numbers
numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers if x % 2 == 0]
# Result: [4, 16]
Tuples
Immutable lists.
point = (10, 20)
# point[0] = 5 <-- Error!
x, y = point # Destructuring works
Kwargs (Keyword Arguments)
def create_user(name, age, city="Delhi"):
pass
create_user(age=25, name="Rahul") # Order doesn't matter!
Tooling Ecosystem
| JavaScript | Python |
|---|---|
| npm/yarn | pip |
| package.json | requirements.txt / pyproject.toml |
| node_modules | venv (Virtual Environment) |
| Prettier | Black |
| Jest | Pytest |
| Express | FastAPI / Flask |
| React | Django (Templates) / Streamlit |
Virtual Environments (Crucial!)
In Node, node_modules is local. In Python, packages are global by default. This causes "Dependency Hell."
Always use a virtual environment:
# Create venv
python -m venv venv
# Activate it (Mac/Linux)
source venv/bin/activate
# Install packages locally
pip install requests
Async/Await
It looks almost identical!
import asyncio
async def fetch_data():
print("Fetching...")
await asyncio.sleep(1)
return "Data"
async def main():
data = await fetch_data()
print(data)
asyncio.run(main())
Why Learn Python?
- AI/ML: Access to PyTorch, TensorFlow, HuggingFace.
- Scripting: Automate server tasks easier than Node.
- Backend: FastAPI is incredibly fast and strictly typed (Pydantic).
It's a great second language to have in your arsenal.