All articles
Article 3 min read

A Python Primer for JavaScript Developers

Dive into Python's basics with insights from a JS developer perspective.

Introduction

As a JavaScript developer looking to expand your skills, understanding fundamental programming concepts can be incredibly beneficial. This article aims to introduce you to the core principles of Python by comparing these ideas to those in JavaScript. By the end, you'll have a solid foundation that bridges both languages and opens up opportunities for more versatile development.

Variables

In both JavaScript and Python, variables are used to store values. The syntax differs slightly between the two:

In JavaScript:

javascript
let age = 25;
console.log(age); // Output: 25

In Python:

python
age = 25
print(age) # Output: 25

Note how in Python, unlike JavaScript where variables are assigned values directly on the line with their declaration (though not required), you need to use an assignment operator (=).

Data Types

Understanding various data types is crucial for any programming language. Here’s a comparison of some basic data types in both languages:

In JavaScript:

javascript
let number = 42;
let string = "Hello World";
let boolean = true;

console.log(typeof(number)); // Output: "number"
console.log(typeof(string)); // Output: "string"
console.log(typeof(boolean)); // Output: "boolean"

In Python:

python
number = 42
string = "Hello World"
boolean = True

print(type(number)) # Output: <class 'int'>
print(type(string)) # Output: <class 'str'>
print(type(boolean)) # Output: <class 'bool'>

In JavaScript, using typeof is a common method to check the type of a variable. In Python, each data type has its own built-in function (type() in this case) for checking.

Control Structures

Control structures like loops and conditionals are fundamental to writing any kind of software. Let’s compare these elements:

In JavaScript:

javascript
let i = 0;
while (i < 10) {
    console.log(i);
    i++;
}

In Python:

python
i = 0
while i < 10:
    print(i)
    i += 1

Python’s += is a shorthand way to increment an integer variable.

Functions

Functions allow you to encapsulate blocks of code that can be reused. Here are basic function examples in both languages:

In JavaScript:

javascript
function greet(name) {
    console.log(`Hello, ${name}!`);
}

greet('World'); // Output: "Hello, World!"

In Python:

python
def greet(name):
    print(f"Hello, {name}!")

greet("World")
# Output: Hello, World!

Python uses indentation to define the structure of blocks, making code easier to read.

Conclusion

This primer has shown you how JavaScript developers can transition into Python with a few comparisons. Understanding these fundamentals not only helps in building robust applications but also bridges knowledge between two major programming languages. Whether it’s for web development, data analysis, or backend services, mastering both will open up new opportunities and capabilities in your tech career.

Keep practicing and exploring more advanced features of each language to expand your skillset!