Python Basics: From JS to Py
Welcome to the exciting world of Python! As a JavaScript developer, you’re already equipped with a solid programming foundation. In this lesson, we’ll explore how Python’s syntax compares to JavaScript, highlighting the similarities and unique features that make Python stand out.
Indentation: Python’s Structural Cornerstone
One of the first things you’ll notice about Python is its use of indentation to define code blocks. Unlike JavaScript’s curly braces, Python relies on consistent indentation:
# Python
if x > 0:
print("Positive")
if x > 10:
print("Greater than 10")
// JavaScript
if (x > 0) {
console.log('Positive');
if (x > 10) {
console.log('Greater than 10');
}
}
This indentation-based structure contributes to Python’s renowned readability, aligning with its philosophy of “There should be one— and preferably only one —obvious way to do it.”
Variables and Data Types
Both Python and JavaScript are dynamically typed languages, meaning you don’t need to declare variable types explicitly:
# Python
x = 5
y = "Hello"
z = [1, 2, 3]
// JavaScript
let x = 5;
let y = 'Hello';
let z = [1, 2, 3];
However, Python uses None
instead of JavaScript’s null
for representing the absence of a value:
# Python
value = None
// JavaScript
let value = null;
Basic Operators
Many operators in Python are similar to JavaScript:
# Python
a = 5 + 3 # Addition
b = 10 - 2 # Subtraction
c = 4 * 2 # Multiplication
d = 16 / 4 # Division (always returns a float)
e = 17 // 3 # Integer division
f = 5 ** 2 # Exponentiation
One notable difference is Python’s //
operator for integer division and the **
operator for exponentiation, compared to JavaScript’s Math.floor()
and **
or Math.pow()
:
// JavaScript
let a = 5 + 3; // Addition
let b = 10 - 2; // Subtraction
let c = 4 * 2; // Multiplication
let d = 16 / 4; // Division
let e = Math.floor(17 / 3); // Integer division
let f = 5 ** 2; // Exponentiation (ES2016+)
Printing Output
In Python, we use the print()
function instead of JavaScript’s console.log()
:
# Python
print("Hello, World!")
name = "Alice"
age = 30
print(f"My name is {name} and I'm {age} years old.")
// JavaScript
console.log('Hello, World!');
let name = 'Alice';
let age = 30;
console.log(`My name is ${name} and I'm ${age} years old.`);
Python’s f-strings (formatted string literals) are similar to JavaScript’s template literals, allowing for easy variable interpolation.
Python’s Philosophy
Python’s design philosophy emphasizes code readability and simplicity. This is encapsulated in “The Zen of Python”, a collection of guiding principles for writing Python code. You can view these principles by running import this
in a Python interpreter.
Some key aspects of this philosophy include:
- Explicit is better than implicit
- Simple is better than complex
- Readability counts
These principles influence not just the language design but also how Python developers approach problem-solving.
Conclusion
In this lesson, we’ve scratched the surface of Python’s syntax and how it compares to JavaScript. We’ve seen how Python uses indentation for structure, shares dynamic typing with JavaScript, and offers similar yet distinct ways to perform basic operations and output.
As we move forward, you’ll discover that while Python and JavaScript share many conceptual similarities, Python’s unique features and philosophy open up new ways of thinking about and structuring your code.
In our next lesson, “Control Structures and Functions,” we’ll dive deeper into how Python handles flow control and function definitions, building on the foundation we’ve established here. Get ready to explore Python’s elegant approach to loops, conditionals, and function creation!