A

Python

python programming-languages data-science machine-learning backend scripting automation

Python

Python is a high-level, interpreted programming language created by Guido van Rossum in 1991. Known for its clean, readable syntax and "batteries included" philosophy, Python has become one of the most popular languages in the world. For JavaScript and TypeScript developers, Python offers a gentler syntax and an incredibly rich ecosystem, particularly for data science, machine learning, and automation.

What is Python?

Python emphasizes code readability and developer productivity. Its design philosophy, summarized in "The Zen of Python," values:

  • Beautiful is better than ugly
  • Explicit is better than implicit
  • Simple is better than complex
  • Readability counts
# Python's clean, readable syntax
def greet(name):
    return f"Hello, {name}!"

message = greet("Alice")
print(message)  # Hello, Alice!

Python for JavaScript/TypeScript Developers

What You'll Recognize

If you know JavaScript/TypeScript, Python will feel familiar:

  • Dynamic typing (like JavaScript)
  • First-class functions
  • List comprehensions (similar to array methods)
  • Object-oriented capabilities
  • Interpreted language (no compilation step)
# Functions as values (like JavaScript)
add = lambda a, b: a + b
result = add(5, 3)  # 8

# List comprehensions (like array methods)
numbers = [1, 2, 3, 4, 5]
doubled = [n * 2 for n in numbers]  # [2, 4, 6, 8, 10]

What's Different

Whitespace Matters

Python uses indentation instead of braces:

# Python
def check_age(age):
    if age >= 18:
        return "Adult"
    else:
        return "Minor"

# JavaScript
function checkAge(age) {
  if (age >= 18) {
    return "Adult";
  } else {
    return "Minor";
  }
}

No Semicolons

Python doesn't use semicolons to end statements (though you can):

name = "Alice"
age = 25
print(name, age)

Different Object Syntax

Python uses dictionaries instead of plain objects:

# Python dictionary
user = {
    "name": "Alice",
    "email": "[email protected]",
    "age": 25
}

print(user["name"])  # Alice

# JavaScript object
const user = {
  name: "Alice",
  email: "[email protected]",
  age: 25
};

console.log(user.name);  // Alice

Strong Typing (with Type Hints)

Modern Python supports optional type hints (like TypeScript):

# Python with type hints
def greet(name: str) -> str:
    return f"Hello, {name}!"

age: int = 25
users: list[str] = ["Alice", "Bob", "Charlie"]

Why Python?

1. Incredibly Readable

Python code often reads like English:

# Check if user is active and has verified email
if user.is_active and user.email_verified:
    send_welcome_email(user)

# List comprehension
even_numbers = [n for n in range(10) if n % 2 == 0]

2. Rich Ecosystem

Python has libraries for everything:

Data Science & Machine Learning:

  • NumPy, Pandas (data manipulation)
  • Matplotlib, Seaborn (visualization)
  • Scikit-learn (machine learning)
  • TensorFlow, PyTorch (deep learning)

Web Development:

  • Django (full-featured framework like Rails)
  • Flask (minimal, like Express)
  • FastAPI (modern, async, type-safe)

Automation & Scripting:

  • Selenium (web automation)
  • Beautiful Soup (web scraping)
  • Requests (HTTP client)

DevOps:

  • Ansible (infrastructure automation)
  • Fabric (deployment automation)

3. Beginner-Friendly

Python's gentle learning curve makes it a popular first language:

# Read a file and count words
with open('document.txt') as f:
    text = f.read()
    word_count = len(text.split())
    print(f"Word count: {word_count}")

4. Versatile

Python is used across many domains:

  • Web development (Django, Flask, FastAPI)
  • Data science (Jupyter, Pandas)
  • Machine learning (TensorFlow, PyTorch)
  • Automation (scripts, testing)
  • Scientific computing (NumPy, SciPy)
  • Game development (Pygame)
  • Desktop apps (PyQt, Tkinter)

5. Great for Prototyping

Python's concise syntax makes it excellent for rapid development:

# Fetch and parse API in just a few lines
import requests

response = requests.get('https://api.example.com/users')
users = response.json()

for user in users:
    print(f"{user['name']} - {user['email']}")

Where Python Excels

Data Science & Analytics

Python dominates data science. Jupyter notebooks make exploration interactive:

import pandas as pd
import matplotlib.pyplot as plt

# Load and analyze data
df = pd.read_csv('sales.csv')
monthly_sales = df.groupby('month')['revenue'].sum()

# Create visualization
monthly_sales.plot(kind='bar')
plt.title('Monthly Revenue')
plt.show()

Machine Learning & AI

Most ML frameworks are Python-first:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Train a model
X_train, X_test, y_train, y_test = train_test_split(X, y)
model = RandomForestClassifier()
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

Automation & Scripting

Python excels at automating repetitive tasks:

# Rename all files in a directory
import os

for filename in os.listdir('./images'):
    if filename.endswith('.jpg'):
        new_name = filename.lower().replace(' ', '_')
        os.rename(f'./images/{filename}', f'./images/{new_name}')

Web APIs

FastAPI makes building APIs incredibly easy:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    name: str
    email: str

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.post("/users")
async def create_user(user: User):
    return {"user": user, "status": "created"}

Web Scraping

from bs4 import BeautifulSoup
import requests

response = requests.get('https://example.com')
soup = BeautifulSoup(response.text, 'html.parser')

# Extract all links
links = [a['href'] for a in soup.find_all('a', href=True)]

Python vs JavaScript/TypeScript

Aspect JavaScript TypeScript Python
Type System Dynamic Static (optional) Dynamic (with optional hints)
Performance Fast (JIT) Same as JS Slower (interpreted)
Concurrency Event loop Same as JS Threading, asyncio
Syntax C-like C-like Whitespace-based
Package Manager npm/pnpm/yarn Same as JS pip, poetry, conda
Runtime Browser, Node.js Same as JS CPython, PyPy
Learning Curve Moderate Moderate Gentle
Use Cases Full-stack web Full-stack web Data/ML/scripting/backend
Deployment Easy (JS runs everywhere) Compiles to JS Requires Python runtime

Common Use Cases

Use Node.js/TypeScript when:

  • Building full-stack web apps (same language everywhere)
  • Frontend development (React, Vue, etc.)
  • Real-time applications (WebSockets)
  • Performance matters (V8 is fast)
  • You need async I/O at scale

Use Python when:

  • Data science, analytics, or visualization
  • Machine learning or AI projects
  • Scientific computing
  • Automation and scripting
  • Rapid prototyping
  • Backend APIs (especially with FastAPI)
  • Working with existing Python ecosystem

Learning Python from JavaScript

Syntax Comparison

Variables

// JavaScript
const name = "Alice";
let age = 25;

# Python
name = "Alice"
age = 25

Functions

// JavaScript
function add(a, b) {
  return a + b;
}

# Python
def add(a, b):
    return a + b

Arrow Functions / Lambdas

// JavaScript
const double = (x) => x * 2;

# Python
double = lambda x: x * 2

Arrays / Lists

// JavaScript
const numbers = [1, 2, 3, 4, 5];
numbers.push(6);
const doubled = numbers.map(n => n * 2);

# Python
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
doubled = [n * 2 for n in numbers]

Objects / Dictionaries

// JavaScript
const user = {
  name: "Alice",
  email: "[email protected]"
};

# Python
user = {
    "name": "Alice",
    "email": "[email protected]"
}

Loops

// JavaScript
for (let i = 0; i < 10; i++) {
  console.log(i);
}

numbers.forEach(n => {
  console.log(n);
});

# Python
for i in range(10):
    print(i)

for n in numbers:
    print(n)

Conditionals

// JavaScript
if (age >= 18) {
  console.log("Adult");
} else if (age >= 13) {
  console.log("Teen");
} else {
  console.log("Child");
}

# Python
if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teen")
else:
    print("Child")

Classes

// JavaScript
class User {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }

  greet() {
    return `Hello, ${this.name}!`;
  }
}

# Python
class User:
    def __init__(self, name, email):
        self.name = name
        self.email = email

    def greet(self):
        return f"Hello, {self.name}!"

Async/Await

// JavaScript
async function fetchUser(id) {
  const response = await fetch(`/api/users/${id}`);
  const data = await response.json();
  return data;
}

# Python
import asyncio
import aiohttp

async def fetch_user(id):
    async with aiohttp.ClientSession() as session:
        async with session.get(f'/api/users/{id}') as response:
            data = await response.json()
            return data

Python Frameworks

Web Frameworks

Django (Full-featured, like Ruby on Rails)

# Django Model
from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

# Django View
from django.shortcuts import render

def article_list(request):
    articles = Article.objects.all()
    return render(request, 'articles.html', {'articles': articles})

Flask (Minimal, like Express)

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def hello():
    return jsonify(message="Hello World")

@app.route('/users/<int:user_id>')
def get_user(user_id):
    return jsonify(id=user_id, name="Alice")

if __name__ == '__main__':
    app.run(debug=True)

FastAPI (Modern, async, type-safe)

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    description: str | None = None

@app.post("/items")
async def create_item(item: Item):
    return {"item": item}

Data Science

  • Pandas: DataFrames for data manipulation
  • NumPy: Numerical computing
  • Matplotlib/Seaborn: Data visualization
  • Jupyter: Interactive notebooks

Testing

# pytest
def test_addition():
    assert add(2, 3) == 5

def test_user_creation():
    user = User("Alice", "[email protected]")
    assert user.name == "Alice"

Python Gotchas for JS Developers

1. Indentation is Syntax

# This will error
def greet(name):
return f"Hello, {name}"  # IndentationError

# Correct
def greet(name):
    return f"Hello, {name}"

2. No === (Just ==)

# Python has only ==
if name == "Alice":
    print("Hi Alice")

# For identity checking, use 'is'
if value is None:
    print("Value is None")

3. Different Truthiness

# Falsy in Python: False, None, 0, '', [], {}, ()
if []:  # False
    print("This won't print")

if "":  # False
    print("This won't print either")

4. No undefined

Python has None instead:

value = None

if value is None:
    print("No value")

5. String Formatting

name = "Alice"
age = 25

# f-strings (Python 3.6+, preferred)
message = f"My name is {name} and I'm {age}"

# .format() method
message = "My name is {} and I'm {}".format(name, age)

# % operator (old style)
message = "My name is %s and I'm %d" % (name, age)

6. List/Dict Methods Don't Return

# JavaScript
const sorted = numbers.sort();  // Returns sorted array

# Python
numbers.sort()  # Sorts in place, returns None
sorted_numbers = sorted(numbers)  # Returns new sorted list

Package Management

pip (built-in)

# Install package
pip install requests

# Install from requirements.txt
pip install -r requirements.txt

# Freeze dependencies
pip freeze > requirements.txt

Poetry (modern, like npm/pnpm)

# Initialize project
poetry init

# Add dependency
poetry add fastapi

# Install dependencies
poetry install

# Run in virtual environment
poetry run python app.py

Virtual Environments

Python uses virtual environments to isolate project dependencies:

# Create virtual environment
python -m venv venv

# Activate (macOS/Linux)
source venv/bin/activate

# Activate (Windows)
venv\Scripts\activate

# Install packages
pip install requests pandas

# Deactivate
deactivate

Real-World Examples

REST API with FastAPI

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List

app = FastAPI()

class Todo(BaseModel):
    id: int
    title: str
    completed: bool = False

todos: List[Todo] = []

@app.get("/todos", response_model=List[Todo])
async def get_todos():
    return todos

@app.post("/todos", response_model=Todo)
async def create_todo(todo: Todo):
    todos.append(todo)
    return todo

@app.get("/todos/{todo_id}", response_model=Todo)
async def get_todo(todo_id: int):
    for todo in todos:
        if todo.id == todo_id:
            return todo
    raise HTTPException(status_code=404, detail="Todo not found")

Data Analysis with Pandas

import pandas as pd
import matplotlib.pyplot as plt

# Load data
df = pd.read_csv('sales.csv')

# Filter and aggregate
high_value = df[df['amount'] > 1000]
monthly_revenue = df.groupby('month')['amount'].sum()

# Statistics
print(df.describe())
print(f"Average sale: ${df['amount'].mean():.2f}")

# Visualization
monthly_revenue.plot(kind='bar')
plt.title('Monthly Revenue')
plt.ylabel('Revenue ($)')
plt.show()

Web Scraping Example

import requests
from bs4 import BeautifulSoup

url = 'https://example.com/articles'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

articles = []
for article in soup.find_all('article'):
    title = article.find('h2').text
    link = article.find('a')['href']
    articles.append({'title': title, 'link': link})

for article in articles:
    print(f"{article['title']}: {article['link']}")

When to Learn Python

Learn Python if:

  • You're interested in data science or machine learning
  • You want to automate tasks or write scripts
  • You're working with scientific computing
  • You need to build backend APIs quickly
  • You want an easy second language

Stick with JavaScript/TypeScript if:

  • You're focused on frontend development
  • You want one language for full-stack
  • You're building React applications
  • You need maximum performance (use Go or Rust instead)

Learning Resources

  1. Official Python Tutorial: docs.python.org/tutorial
  2. Real Python: Comprehensive tutorials and articles
  3. Automate the Boring Stuff with Python: Free book for practical Python
  4. Python Crash Course: Beginner-friendly book
  5. Kaggle: Learn Python for data science
  6. freeCodeCamp: Python certification

Python 2 vs Python 3

Always use Python 3 (Python 2 reached end-of-life in 2020):

# Python 2 (deprecated)
print "Hello"
result = 5 / 2  # 2 (integer division)

# Python 3 (current)
print("Hello")
result = 5 / 2  # 2.5 (float division)

Key Takeaways

  • Python is readable, versatile, and beginner-friendly
  • Dominates data science, machine learning, and automation
  • Slower than compiled languages but fast enough for most use cases
  • Huge ecosystem with libraries for everything
  • Great for prototyping and scripting
  • Different syntax from JavaScript but concepts translate well
  • Optional type hints provide TypeScript-like safety
  • Works well alongside JavaScript/TypeScript for specialized tasks
  • TypeScript - Another type-safe language for web development
  • Go - Alternative for high-performance backend services
  • Rust - Systems programming language
  • React - Can use Python for backend with React frontend

Python's strength is its versatility and ecosystem. For JavaScript/TypeScript developers, Python is an excellent complementary language—use JavaScript for frontend and full-stack web, use Python for data science, automation, and rapid backend development.

Last updated: October 16, 2025