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.
In JavaScript, you're familiar with declaring and using variables.
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:
# Variables (no keyword needed)
name = "Alice"
name = "Bob"
# Constants (convention: UPPERCASE)
PI = 3.14159 # No true constants
# Multiple assignments
x, y, z = 1, 2, 3Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
// 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;You may be used to different syntax or behavior.
Python doesn't require let/const keywords
You may be used to different syntax or behavior.
Python booleans are True/False (capitalized)
You may be used to different syntax or behavior.
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.
let name = "Alice";name = "Alice"2. No True Constants
Python has no const keyword. By convention, UPPERCASE names indicate constants, but they can still be changed.
const MAX_SIZE = 100;MAX_SIZE = 100 # Convention onlyCommon 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 //
Key Takeaways
- No keywords needed for variables
- UPPERCASE for constants (convention only)
- Multiple assignment with tuple unpacking