JS
PY

JavaScript to Python

10 lessons

Progress0%
1Variables and Constants2Functions3Arrays vs Lists4Objects vs Dictionaries5Classes and OOP6Modules and Imports7Array Methods vs Comprehensions8Error Handling9Async Programming10File I/O
All Mirror Courses
JS
PY
Variables and Constants
MirrorLesson 1 of 10
Lesson 1

Variables and Constants

Declaring and using variables

Introduction

In this lesson, you'll learn about variables and constants in Python. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with declaring and using variables.

PY
In Python:

Python has its own approach to declaring and using variables, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
# Variables (no keyword needed)
name = "Alice"
name = "Bob"

# Constants (convention: UPPERCASE)
PI = 3.14159  # No true constants

# Multiple assignments
x, y, z = 1, 2, 3

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
// Variables with let (can change)
let name = "Alice";
name = "Bob";

// Constants with const (cannot change)
const PI = 3.14159;

// Multiple assignments
let x = 1, y = 2, z = 3;
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Python doesn't require let/const keywords

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Python booleans are True/False (capitalized)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Python uses # for comments instead of //

Step-by-Step Breakdown

1. No Keywords Needed

Python variables are created simply by assigning a value. No let, const, or var.

JS
JavaScript
let name = "Alice";
PY
Python
name = "Alice"

2. No True Constants

Python has no const keyword. By convention, UPPERCASE names indicate constants, but they can still be changed.

JS
JavaScript
const MAX_SIZE = 100;
PY
Python
MAX_SIZE = 100  # Convention only
Common Pitfall
Python constants are just a naming convention. The language won't prevent reassignment.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Python doesn't require let/const keywords
  • Python booleans are True/False (capitalized)
  • Python uses # for comments instead of //
Common Pitfall
Don't assume Python works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • No keywords needed for variables
  • UPPERCASE for constants (convention only)
  • Multiple assignment with tuple unpacking
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Python to practice these concepts.
OverviewNext