Skip to main content

3. Syntax and Language Features

German CoccaNovember 22, 2024About 5 minPythonJavaScriptArticle(s)blogfreecodecamp.orgpythonpyjavascriptjs

3. Syntax and Language Features 관련

How to Learn Python for JavaScript Developers [Full Handbook]

As a developer with experience in JavaScript, you likely know how versatile the language is, especially when it comes to web development. JavaScript powers both frontend and backend development (thanks to Node.js) and has grown to become one of the m...

How to Learn Python for JavaScript Developers [Full Handbook]
As a developer with experience in JavaScript, you likely know how versatile the language is, especially when it comes to web development. JavaScript powers both frontend and backend development (thanks to Node.js) and has grown to become one of the m...

While both JavaScript and Python are dynamically typed, high-level languages, they have distinct syntax rules and language features that can affect code readability, structure, and maintenance.

This section highlights some of the core syntactical differences and introduces language features that will be especially relevant for a JavaScript developer learning Python.


Comparison of Syntax Simplicity and Readability

One of Python’s main selling points is its clear, readable syntax. Often described as “executable pseudocode,” Python emphasizes simplicity, aiming for code that’s easy to write and, perhaps more importantly, easy to read.

Unlike JavaScript, which uses braces ({}) to define code blocks, Python uses indentation to enforce structure, which naturally encourages clean, organized code.

Example: Hello World and Simple Loops

In both languages, the "Hello, World!" example highlights the difference in syntax:

print("Hello, World!")

Python’s built-in print function makes printing straightforward without additional syntax. In JavaScript, console.log performs the same task but requires a more explicit object-method format.

Now, consider a simple loop that prints numbers from 0 to 4:

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

The difference here is striking. Python’s for loop with range() is compact and highly readable, while JavaScript’s loop uses a more complex syntax with initialization, condition, and increment clauses. This is a minor but illustrative example of Python’s design philosophy: code should be intuitive and easy to follow.


Data Types and Variable Declaration

Both JavaScript and Python are dynamically typed, meaning that you don’t need to specify variable types explicitly. But there are differences in variable declaration and type handling that are worth noting.

Variable Declaration

JavaScript requires let, const, or var to declare variables. The use of let and const in modern JavaScript helps manage scope and constancy of variables, with const enforcing immutability.

In Python, there is no need to specify let, const, or var – you simply assign a value to a variable, and Python infers the type based on the value.

let age = 25;  // Using 'let' for a block-scoped variable
const name = "Alice";  // Using 'const' for an immutable variable

Type Checking and Conversion

Python’s type-checking system is more consistent, while JavaScript sometimes has quirky behavior due to type coercion, where values of different types are implicitly converted for comparison. For example:

console.log(0 == "");  // true due to type coercion
console.log(0 === ""); // false due to strict equality

Python does not allow implicit type coercion, reducing potential bugs related to unexpected type behavior. If type conversion is needed, Python requires explicit casting.


Working with Primitive Data Types

JavaScript and Python share some primitive types but also have unique types and handling:


Data Collections: Lists, Tuples, Sets, and Dictionaries

Both JavaScript and Python offer various data structures to handle collections, but Python has built-in types that allow for more specific data handling.

Lists and Arrays

Python’s list type is analogous to JavaScript’s array, but it’s more versatile, as Python lists can store elements of different types and support built-in functions for manipulation. In contrast, JavaScript arrays are specialized objects with numerical indices.

my_list = [1, "apple", 3.14]

Tuples

Python offers tuple as an immutable version of a list, useful when data should not be modified. JavaScript has no direct equivalent, though const can create a similar effect by enforcing immutability.

my_tuple = (1, "apple", 3.14)

Sets

Both languages offer a set data type for collections of unique elements. Python has set, while JavaScript uses Set.

my_set = {1, 2, 3}

Dictionaries and Objects

Python’s dict and JavaScript’s objects are both key-value structures, but they differ in design and functionality.

In Python, dictionaries are optimized for hashable keys, whereas JavaScript objects are more flexible but can lead to type-related issues when keys are non-string values.

my_dict = {"name": "Alice", "age": 25}

Control Structures: Conditionals and Loops

Both Python and JavaScript have similar control structures, such as if, for, and while loops. But Python's syntax is simplified due to its reliance on indentation.

Conditionals

if age > 18:
    print("Adult")
else:
    print("Minor")

Python’s syntax avoids the braces used in JavaScript, relying on indentation to signify code blocks. This makes code look cleaner but enforces strict formatting, which can be a learning curve for JavaScript developers.

Loops

for i in range(5):
    print(i)
count = 0
while count < 5:
    print(count)
    count += 1

Key Takeaways: