Sitemap
Mustafa Onur Çelik

Mustafa Onur Çelik

Freelance Frontend Developer

Python for JavaScript Developers

3 min readJan 9, 2025
Press enter or click to view image in full size
Photo by MW on Unsplash

If you’re a JavaScript developer and want to dive into Python, this guide will help you get started quickly by drawing parallels between JavaScript and Python. We’ll start with the basics of project initialization and dependency management, then explore Python equivalents for common JavaScript functions, all with practical code examples.

1. Starting a Project: npm vs. pip

JavaScript (npm)

In JavaScript, you initialize a project with npm init and install dependencies using npm install. For example:

npm init -y
npm install express

Python (pip)

In Python, you use pip for dependency management and virtualenv to create isolated environments.

1. Create a virtual environment:

python -m venv myenv

2. Activate the environment:

• On Windows:

myenv\Scripts\activate

• On macOS/Linux:

source myenv/bin/activate

3. Install dependencies:

pip install flask

4. Save dependencies to a requirements file:

pip freeze > requirements.txt

5. Install dependencies from a requirements file:

pip install -r requirements.txt

2. Basic Syntax Differences

Hello, World!

• JavaScript:

console.log(“Hello, World!”);

• Python:

print(“Hello, World!”)

3. Variables and Data Types

JavaScript:

let name = “Onur”;
let age = 38;
let isDeveloper = true;

Python:

name = "Onur"
age = 30
is_developer = True

4. Functions

JavaScript:

function greet(name) {
return `Hello, ${name}`;
}
console.log(greet("Onur"));

Python:

def greet(name):
return f"Hello, {name}"
print(greet("Onur"))

5. Arrow Functions

Get Mustafa Onur Çelik’s stories in your inbox

Join Medium for free to get updates from this writer.

Python does not have arrow functions, but you can use lambda for concise anonymous functions.

JavaScript:

const add = (a, b) => a + b;
console.log(add(2, 3));

Python:

add = lambda a, b: a + b
print(add(2, 3))

6. Loops

JavaScript (for loop):

for (let i = 0; i < 5; i++) {
console.log(i);
}

Python (for loop):

for i in range(5):
print(i)

7. Arrays vs. Lists

JavaScript (Array):

const fruits = ["apple", "banana", "cherry"];
console.log(fruits[1]);

Python (List):

fruits = ["apple", "banana", "cherry"]
print(fruits[1])

8. Objects vs. Dictionaries

JavaScript (Object):

const person = { name: "Onur", age: 30 };
console.log(person.name);

Python (Dictionary):

person = {"name": "Onur", "age": 30}
print(person["name"])

9. Promises vs. Async/Await

JavaScript:

const fetchData = async () => {
const data = await fetch("https://api.example.com");
console.log(await data.json());
};
fetchData();

Python:

import asyncio
import aiohttp

async def fetch_data():
async with aiohttp.ClientSession() as session:
async with session.get("https://api.example.com") as response:
print(await response.json())
asyncio.run(fetch_data())

10. Common JavaScript Functions and Their Python Equivalents

JavaScript — — — — — — — — — — — — — -Python

Math.max(1, 2, 3)               ->   max(1, 2, 3)
Math.min(1, 2, 3) -> min(1, 2, 3)
arr.map(x => x * 2) -> [x * 2 for x in arr]
arr.filter(x => x > 2) -> [x for x in arr if x > 2]
arr.reduce((a, b) => a + b) -> sum(arr)
setTimeout(() => {}, 1000) -> import time; time.sleep(1)
JSON.stringify(obj) -> import json; json.dumps(obj)
JSON.parse(str) -> import json; json.loads(str)

11. File Handling

JavaScript:

const fs = require("fs");

fs.writeFileSync("example.txt", "Hello, World!");
const content = fs.readFileSync("example.txt", "utf8");
console.log(content);

Python:

with open("example.txt", "w") as file:
file.write("Hello, World!")

with open("example.txt", "r") as file:
content = file.read()
print(content)

12. Bonus

Example of a Flask app:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
return "Hello, World!"

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

Conclusion

This guide has introduced the key differences and similarities between JavaScript and Python, helping you leverage your existing JavaScript knowledge to learn Python efficiently. Start experimenting with Python by setting up small projects, and you’ll find it to be a versatile and powerful language for various use cases.

--

--