A JavaScript Developer's First Thoughts on Python...
Mekhi C. Tudor
Posted on November 29, 2024
TABLE OF CONTENTS
- Learning Python with a Foundation in JavaScript
- Transitioning from JavaScript to Python
- Discovering the Power of Counter()
- Final Thoughts
Learning Python with a Foundation in JavaScript
Having spent the past year building a strong foundation in JavaScript, I decided to expand my skill set by exploring Python. Over the last 14 days, I have focused on deepening my understanding of Python and its potential for software development, particularly with frameworks like Flask and TensorFlow. My goal has been to leverage my existing knowledge of programming to establish a solid grasp of Python before delving into its rich ecosystem of frameworks and libraries.
Transitioning from JavaScript to Python
To start, my initial focus was on understanding Python’s syntax and conventions. Coming from seven months of working primarily with JavaScript, I found Python surprisingly refreshing. Its clean and readable syntax felt almost like writing in plain English, which was a stark contrast to JavaScript’s more complex quirks.
The basics I tackled during the first week included:
Variable declarations: Python’s variable declarations were straightforward, though it required a mental shift from JavaScript’s
let
andconst
.String manipulation: Python, similar to JavaScript, offers intuitive methods like
.join()
,.split()
, and f-strings, which felt like a streamlined version of JavaScript’s template literals.Working with data structures: Python’s lists (similar to JavaScript arrays), dictionaries (analogous to JavaScript objects), and sets opened up new approaches to problem-solving.
Discovering the Power of Counter()
One of the features I’ve encountered so far is Python’s Counter
from the collections
module. Counter
is an elegant way to tally the frequency of elements in any iterable data structure, creating something akin to a frequency object in JavaScript.
In JavaScript, comparing the equality of two objects requires a more nuanced approach due to how objects are stored in memory. Unlike Python, which compares objects by content, JavaScript stores an object’s key-value pairs as references. This means that two objects are only considered equal if they reference the same location in memory.
In JavaScript:
function areObjectsEqual(obj1, obj2) {
// Check if both are the same reference
if (obj1 === obj2) {
return true;
}
// Check if either is null or not an object
if (obj1 == null || obj2 == null || typeof obj1 !== "object" || typeof obj2 !== "object") {
return false;
}
// Check if both objects have the same number of keys
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
// Check if all keys and their values are the same
for (const key of keys1) {
if (!keys2.includes(key) || !areObjectsEqual(obj1[key], obj2[key])) {
return false;
}
}
return true;
}
// Example usage
const objA = { a: 1, b: { c: 2 } };
const objB = { a: 1, b: { c: 2 } };
console.log(areObjectsEqual(objA, objB)); // true
const objC = { a: 1, b: { c: 3 } };
console.log(areObjectsEqual(objA, objC)); // false
To evaluate whether two objects contain equivalent data, I would have to iterate through the keys of one object, verify their presence in the other, and ensure the corresponding values are identical. This process involves manually checking the equality of data stored in distinct memory locations.
Python’s design emphasizes simplicity and elegance, making it a powerful tool for me to write clean, efficient code. This simplicity not only enhances readability but also reduces time and cognitive overhead when solving complex problems.
In Python:
def are_objects_equal(obj1, obj2):
# Check if both are dictionaries
if not isinstance(obj1, dict) or not isinstance(obj2, dict):
return False
# Use Counter to compare the dictionaries
return Counter(obj1) == Counter(obj2)
# Example usage
obj_a = {'a': 1, 'b': 2, 'c': 3}
obj_b = {'c': 3, 'b': 2, 'a': 1}
obj_c = {'a': 1, 'b': 2, 'c': 4}
print(are_objects_equal(obj_a, obj_b)) # True
print(are_objects_equal(obj_a, obj_c)) # False
A prime example of this is the Counter
method from Python’s collections
module, which streamlines tasks like frequency counting. I’ve found this method particularly invaluable for tackling algorithmic challenges on platforms like LeetCode, where identifying element frequencies is a recurring requirement in array and hashing problems. Its intuitive syntax and out-of-the-box functionality significantly improve problem-solving speed, allowing me to focus more on strategy than implementation details.
Final Thoughts
Learning Python with a background in JavaScript has been a smooth experience. Python’s simplicity and powerful standard library make it an excellent tool for tackling algorithmic challenges and more. Over the next few weeks, I plan to continue exploring Flask and Django, leveraging Python’s potential for backend development.
For now, I’m just enjoying how Python simplifies many of the common tasks that often feel verbose or tedious in JavaScript. This experience has broadened my perspective on programming languages and how they approach problem-solving in unique ways.
Posted on November 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.