Something i found interesting from learning Python
Mutable!
In Python, lists are mutable, and when you pass them as arguments, any change inside the function reflects outside.
Validation
1 | char.isdigit() # bool |
set
1 | a = set([1, 2, 3]) |
Set Operations
Operation | Method Name | Operator |
---|---|---|
Difference | set1.difference(set2) |
set1 - set2 |
Union | set1.union(set2) |
set1 \| set2 |
Intersection | set1.intersection(set2) |
set1 & set2 |
Symmetric Difference | set1.symmetric_difference(set2) |
set1 ^ set2 |
- ✅ Cleaner
- ✅ Faster to type
- ✅ Interviewer-friendly (they love when you know this)
extended slicing
1 | # extended slicing |
strip()
1 | text = " example string with spaces \n" |
🗺️ Roadmap Overview
This roadmap is structured to build my proficiency in Python and strengthen my understanding of fundamental data structures and algorithms. It is divided into several phases, each focusing on specific topics and associated problems.
Phase 1: Python Fundamentals
Before diving into problem-solving, ensure i'm comfortable with Python's syntax and core concepts:
- Data Types and Structures: Lists, Tuples, Dictionaries, Sets
- Control Flow: Loops, Conditional Statements
- Functions and Modules: Defining functions, Importing modules
- List Comprehensions and Lambda Functions
Resources:
- Python Official Documentation
- LeetCode's Explore Python Section
Phase 2: Arrays and Strings
These are foundational topics that frequently appear in interviews.
- Two Sum: LeetCode Problem #1
- Best Time to Buy and Sell Stock: LeetCode Problem #121
- Valid Anagram: LeetCode Problem #242
- Product of Array Except Self: LeetCode Problem #238
- Longest Substring Without Repeating Characters: LeetCode Problem #3
Phase 3: Two Pointers and Sliding Window
Techniques that optimize traversal of arrays and linked lists.
- Valid Palindrome: LeetCode Problem #125
- Container With Most Water: LeetCode Problem #11
- 3Sum: LeetCode Problem #15
- Minimum Size Subarray Sum: LeetCode Problem #209
- Longest Repeating Character Replacement: LeetCode Problem #424
Phase 4: Hashing and Hash Maps
Efficient data retrieval and storage techniques.
- Two Sum: LeetCode Problem #1
- Group Anagrams: LeetCode Problem #49
- Top K Frequent Elements: LeetCode Problem #347
- Happy Number: LeetCode Problem #202
- Longest Consecutive Sequence: LeetCode Problem #128
Phase 5: Linked Lists
Understanding linked lists is crucial for many algorithmic problems.
- Reverse Linked List: LeetCode Problem #206
- Merge Two Sorted Lists: LeetCode Problem #21
- Linked List Cycle: LeetCode Problem #141
- Remove Nth Node From End of List: LeetCode Problem #19
- Reorder List: LeetCode Problem #143
Phase 6: Trees and Graphs
Explore hierarchical data structures and their traversal algorithms.
- Maximum Depth of Binary Tree: LeetCode Problem #104
- Same Tree: LeetCode Problem #100
- Invert Binary Tree: LeetCode Problem #226
- Binary Tree Level Order Traversal: LeetCode Problem #102
- Number of Islands: LeetCode Problem #200
Phase 7: Dynamic Programming
Techniques for solving complex problems by breaking them down into simpler subproblems.
- Climbing Stairs: LeetCode Problem #70
- Coin Change: LeetCode Problem #322
- Longest Increasing Subsequence: LeetCode Problem #300
- House Robber: LeetCode Problem #198
- Unique Paths: LeetCode Problem #62
Phase 8: Advanced Topics
Delve into more complex data structures and algorithms.
- Implement Trie (Prefix Tree): LeetCode Problem #208
- Design Add and Search Words Data Structure: LeetCode Problem #211
- Word Search II: LeetCode Problem #212
- Merge Intervals: LeetCode Problem #56
- Insert Interval: LeetCode Problem #57
Additional Resources
- LeetCode 75 Study Plan: A curated list of 75 essential problems for interview preparation. citeturn0search2
- Top Interview 150 Study Plan: A comprehensive set of 150 classic interview questions. citeturn0search1
- NeetCode's Blind 75: A popular list of algorithm practice problems with video
🧩 LeetCode Practices
997. Find the Town Judge
Key Insight
Identify the town judge by tracking trust relationships: the judge is trusted by everyone else but trusts no one.
Python Implementation
1 | class Solution(object): |
Python-Specific Learnings
- List Initialisation Use a list to track trust scores for each person.
- Loop Structure Iterate over trust pairs to adjust scores.
- Return Value Check for the person with a trust
score of
n-1
.
Edge Cases
- Single person with no trust relationships → returns 1
- No person satisfies the judge conditions → returns -1
Complexity
- Time: \(O(T + n)\), where \(T\) is the length of the trust list
- Space: \(O(n)\)
Why This Works
- The judge has a trust score of
n-1
because= they are trusted by everyone else and trust no one.
3432. Count Partitions with Even Sum Difference
Problem: Count partitions where the difference between the sum of left and right partitions is even.
Learning Journey
- Initial approach with itertools:
1
2
3
4# Using accumulate for prefix sums
prefix_sum_from_left = list(itertools.accumulate(nums))
prefix_sum_from_right = list(itertools.accumulate(reversed(nums)))
prefix_sum_from_right.reverse()
Key Insight: While clean, straightforward, this creates 2 extra arrays (O(n) space) and reverses twice
First naive solution: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19class Solution:
def countPartitions(self, nums: List[int]) -> int:
# prefix_sum_from_left = []
prefix_sum_from_left = list(itertools.accumulate(nums))
prefix_sum_from_right = list(itertools.accumulate(reversed(nums)))
prefix_sum_from_right.reverse()
# print("prefix_sum_from_left", prefix_sum_from_left)
# print("prefix_sum_from_right", prefix_sum_from_right)
count = 0
for i in range(1, len(nums)):
print("i", i)
if (prefix_sum_from_left[i-1] - prefix_sum_from_right[i]) % 2 == 0:
count += 1
return count
- Optimised approach:
1
2
3
4total_sum = sum(nums) # Single O(n) operation
for num in nums[:-1]: # O(n) iteration
prefix_sum += num
suffix_sum = total_sum - prefix_sum # O(1) calculation
Optimised solution: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class Solution:
def countPartitions(self, nums: List[int]) -> int:
# Calculate total sum to avoid computing it multiple times.
total_sum = sum(nums)
print("total_sum", total_sum)
prefix_sum = 0
count = 0
# We only need to check up to n-1 for partitioning.
# for i in range(0, len(nums)-1):
for i in nums[:-1]:
prefix_sum += nums[i]
current_oposite = total_sum - prefix_sum
# print("prefix_sum", prefix_sum)
# print("current_oposite", current_oposite)
# Check if the difference between left and right partition sums is even.
if ((prefix_sum - current_oposite) % 2) == 0:
count += 1
return count
Optimisation: Reduced space complexity from \(O(n)\) → \(O(1)\) by calculating suffix on the fly
Complexity Deep Dive
Approach | Time | Space | Real-World Impact |
---|---|---|---|
Initial | O(n) | O(n) | 2x memory usage for large datasets |
Optimised | O(n) | O(1) | Handles 1M+ elements efficiently |
Why This Matters in Interviews
- Shows progression from "make it work" → "make it efficient"
- Demonstrates understanding of space-time tradeoffs
- Highlights ability to optimize mathematical operations
Common Follow-Up Questions
- "How would you handle negative numbers in the array?"
- Current solution works (sum properties hold), but test edge cases
- "What if the array contains zeros?"
- Valid partition points still work, need to verify with examples
- "Can you solve this with constant space without calculating total
sum first?"
- Challenge: Requires different mathematical approach
Related Interview Topics - Prefix sum variations (2D arrays, circular arrays) - Space-time complexity tradeoff decisions - Mathematical pattern recognition (even/odd properties) - Edge case identification (empty arrays, single elements)
Similar Problems - Split Array with Equal Sum - Partition Equal Subset Sum - Find Pivot Index
1848. Minimum Distance to the Target Element
Problem: Find minimum absolute difference between indices containing target value and given start index.
1 | class Solution: |
Key Learnings:
enumerate()
: Efficiently track both index and value during iteration- More Pythonic than
range(len(nums))
- Clearer intent when needing both index and value
- More Pythonic than
min()
: Built-in function for maintaining minimum value- More efficient than manual comparisons
- Handles edge cases implicitly
float('inf')
: Initialize with infinite value pattern- Common algorithm technique for minimization problems
- Guarantees first valid value will replace it
Complexity:
- Time: \(O(n)\) - Single pass through array
- Space: \(O(1)\) - Constant extra space
88. Merge Sorted Array
Problem: Merge nums2 into nums1 in-place while maintaining sorted order.
Key Insight Utilise 3-pointer technique starting from the end of both arrays to avoid overwriting nums1 values.
Python Implementation
1 | class Solution: |
Python-Specific Learnings 1. In-place
Modification Directly modifies nums1 without extra space 2.
Pointer Manipulation Negative indices work naturally in
Python for reverse traversal 3. Slice Assignment
nums1[:p2 + 1] = ...
efficiently copies remaining elements
->
nums1[: pointer_nums2 + 1] = nums2[: pointer_nums2 + 1]
Complexity - Time: \(O(m + n)\) - Single pass through both arrays - Space: \(O(1)\) - No additional data structures used
...
🎯 to be added more ...
LeetCode Grind Plan 🔥
- learn the basics of python3 such as
- syntax
- data types
- builtin functions
Python note
- Uses
snake_case
PascalCase
for class names""" ... """
to do multiple lines comments like/* */
in js- Use explicit
is
forNone
comparisons:if x is None
1
2
3
4
5
6
7
8
9
10# 🚨 Dangerous (might give false positives)
def process_data(data=[]):
if not data: # Could be None, empty list, 0, False, etc
print("No data received")
# ✅ Explicit check
def process_data(data=None):
if data is None: # Only triggers for None
data = []
# process data... - Prefer list comprehensions over loops where readable
1
2
3
4
5
6
7
8
9
10
11
12# Traditional loop
squares = []
for num in range(10):
if num % 2 == 0:
squares.append(num ** 2)
# List comprehension (same result)
squares = [num ** 2 for num in range(10) if num % 2 == 0]
# Nested example (still readable)
matrix = [[1,2], [3,4], [5,6]]
flattened = [num for row in matrix for num in row]
Set
1 | # simply just |
Array
Map
1 | # Create a dictionary |
below are outdated notes, to be refined later
Variables
1 | print() |
1 | type() |
1 | // # this forces the output to be integer |
Type Conversion: The process of converting one data type to another data type is called type conversion.
In Python, we can perform two types of type conversion. 1. Implicit
Type Conversion - Here, Python automatically converts one data type to
another in order to avoid data loss. > e.g. 1 * 1.23 = 1.23 as there
is no data loss 2. Explicit Type Conversion - Here, the user can convert
the data type of a variable to the required data type by using the
in-built functions int(), float(), str(), etc. > e.g.
"Hello " + str(123)
Data Structures
List
1 | list = [0,1,2,3] |
first N items in list
1 | print(list[0:3]) # Exclusive 3 - incl:excl |
Last item
1 | print(list[-1]) |
Remove last and return
1 | list.pop() |
Add to last
1 | list.append(4) |
Tuples
1 | storage = (32,64,128,256) |
1 | print(storage[1]) |
Tuples are immutable
Dictionary (Map)
1 | # creating a dictionary |
1 | type(attributes) |
1 | # creating a dictionary for storing data. |
1 | # keys of a dictionary |
1 | # values of a dictionary |
update({key: value})
1 | dictionary={1: "USA", 2: "India", 3: "China"} |
pop(key)
1 | dictionary={1:"USA", 2:"India", 3:"China"} |
Conditional Statements
... nth much 1
2
3
4
5
6
7
8
9price = 900
# define the budget price
budget = int(input('Enter your budget(in dollars): '))
# if-else statement
if price <= budget:
print('Congrats! You can buy the Iphone')
else:
print('Sorry! The mobile price is more than your budget')
Looping Statements
1 | print(range(6)) |
1 | print(list(range(6))) |
1 | print(list(range(2,6))) |
1 | print(list(range(6, 14, 2))) # Iterating by 2 |
List Comprehensions
Example 1: from: 1
2
3
4
5
6
7discounted_price_list=[]
for x in price_list:
discounted_price = x - (x*(5/100))
discounted_price_list.append(discounted_price)
print(discounted_price_list)1
2discounted_price_list = [x - (x*(5/100)) for x in price_list]
print(discounted_price_list)
Example 2: from: 1
2
3
4
5
6
7
8
9within_budget = []
for x in discounted_price_list:
if x <= budget:
within_budget.append('Yes')
else:
within_budget.append('No')
print(within_budget)1
2
3
4
5
6# asking for customer's budget
budget = int(input('Enter your budget(in dollars): '))
# creating a list of Yes/No based on budget and discounted prices
within_budget = ['Yes' if x <= budget else 'No' for x in discounted_price_list]
print(within_budget)
Functions
lambda functions
1 | dis_price_lambda = lambda discount : 900 - ( 900 * (discount / 100) ) |
Wow ...
1 | def say(message, times): |
1 | (lambda x: (x+2)*5/2)(4) |
Due to the division operation in the expression, which produces a floating-point result in Python 3. Use
//
instead, if you want to perform floor division.
*args and **kwargs
from: 1
2
3
4
5
6
7
8
9
10
11
12
13def total_amount(price1, price2, price3, price4, price5):
"""
This function takes the price of five phones ordered
and returns the total order amount.
"""
# computing the total order amount
total = price1 + price2 + price3 + price4 + price5
# return the total amount
return total
print('Total order amount:', total_amount(700, 599, 650, 900, 820))
# Total order amount: 36691
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17def total_amount(*args): # can be renamed to *prices
"""
This function takes the prices of phones ordered
and returns the total order amount.
"""
total = 0
# computing the total order amount
for arg in args:
total += arg
# return the total amount
return total
print('Total order amount:', total_amount(700, 599, 650, 900, 820))
# Total order amount: 3669
print('Total order amount:', total_amount(700, 599, 650, 900, 820, 630, 520, 799, 999, 840))
# Total order amount: 7457
1 | def customer_net_spend(*prices, discount=0.0, **kwargs): |
kwargs is like a %{key: value} in Elixir
1 | def order_summary(*prices, **additionals): |
Invalid Syntax
1 | def my_function(a, b, **kwargs, *args): |
because *args (for variable-length positional arguments) must be placed before **kwargs (for variable-length keyword arguments).
1 | def my_function(a, b, *args, **kwargs): |
1 | def fun(A, B=30): |
## Some findings
|
Comparison
Aspect | Python | JavaScript |
---|---|---|
Syntax & Code Structure | Clean, minimal syntax: Uses indentation and colons to define code blocks instead of curly braces, leading to cleaner and more readable code (Why I moved from JavaScript to Python for LeetCode - DEV Community). No semicolons are required and there is less boilerplate (e.g. no parentheses around if conditions) (Why I moved from JavaScript to Python for LeetCode - DEV Community). Python also offers syntactic sugar like list comprehensions, multiple assignment, and tuple unpacking that make code more concise and expressive (Why I moved from JavaScript to Python for LeetCode - DEV Community). | C-style syntax: Uses curly braces { }
to denote code blocks and typically uses semicolons to terminate
statements (though semicolons are optional in modern JS) (Why
I moved from JavaScript to Python for LeetCode - DEV Community).
This results in a bit more verbose structure and punctuation compared to
Python (Why
I moved from JavaScript to Python for LeetCode - DEV Community).
JavaScript’s syntax is flexible (e.g. arrow functions, object literals,
destructuring) but generally requires more symbols, which can make
simple logic appear slightly more cluttered than Python. |
Pros (Coding Interviews) | Readable and quick to write: Python’s simple, English-like syntax lets you implement solutions quickly – it often feels like writing pseudocode (Why JavaScript is actually a great LeetCode/Interview language - DEV Community) (Which language is best for interviews?). It has a rich set of built-in functions and data structures (lists, dictionaries, sets, etc.) that simplify common tasks without extra code (Which language is best for interviews?). Python is widely used on LeetCode and in interviews, so many interviewers are familiar with it, and there are plenty of solution resources available in Python. | Versatile and fast: JavaScript works for both
frontend and backend (Node.js), so using it in interviews can be
advantageous if you’re applying for web development roles (Which
language is best for interviews?). Its syntax is relatively simple
(no explicit types) and far less verbose than Java or C++, which means
you can still code and iterate quickly under time pressure. The V8
engine gives JavaScript very high execution speed, so performance is
rarely a bottleneck for algorithmic problems (Why
JavaScript is actually a great LeetCode/Interview language - DEV
Community). Additionally, JavaScript has handy built-in methods
(like Array.prototype.map , filter ,
reduce ) that can make code succinct and show off elegant
problem-solving when used appropriately. |
Cons (Coding Interviews) | Performance and speed limits: Python’s runtime speed is slower than some other languages, which can lead to timeouts on extremely large test cases if the solution isn’t optimal (Which language is best for interviews?) (Why JavaScript is actually a great LeetCode/Interview language - DEV Community). It also has higher memory overhead for data structures (e.g. a list or dict may use more memory than a similar structure in C/C++ or JS) (Node.js vs Python: Selecting the Ideal Backend Tech for 2024). Additionally, Python’s significant whitespace (indentation) means a simple indent mistake can cause a syntax error – this is easy to fix with practice, but it’s a gotcha for those not used to it. | Limited data structures & subtle quirks:
JavaScript has fewer native data structures for algorithms – for
example, it lacks a built-in heap/priority queue, so you might need to
implement one manually if needed (Why
JavaScript is actually a great LeetCode/Interview language - DEV
Community) (Which
language is best for interviews?). Some language quirks can also
cause bugs: JavaScript’s loose typing and type coercion (e.g.
"5" + 2 vs "5" - 2 ) can lead to unexpected
behavior if you’re not careful. And unlike Python, certain errors won’t
stop execution (e.g. out-of-bounds array access just yields
undefined instead of throwing an error), so logic bugs
might not be immediately obvious. Historically, algorithm communities
had fewer JS solutions (though this is improving), meaning slightly less
community guidance for tricky problems compared to Python. |
Code Readability & Conciseness | Highly readable: Python’s enforced indentation and uncluttered syntax make code easy to follow. It generally requires fewer lines to solve a problem than JavaScript, thanks to features like list comprehensions and the lack of extra symbols (Why I moved from JavaScript to Python for LeetCode - DEV Community). In practice, Python code is often considered more concise and closer to pseudocode, which helps others (including interviewers) understand your solution quickly (Python Vs Javascript: What are the Differences? in 2025). | Fairly readable (with more syntax): JavaScript code includes braces and semicolons, which add some visual noise. However, its syntax is familiar to many developers and is still relatively succinct compared to languages like Java. Modern ES6+ features (arrow functions, destructuring, etc.) have improved JavaScript’s conciseness, but Python’s syntax is typically shorter for equivalent logic (Python Vs Javascript: What are the Differences? in 2025). Clear formatting and naming in JS are important to approach Python’s level of readability. |
Built-in Data Structures & Libraries | “Batteries included”: Python offers a wide range of
built-in data structures – lists, dictionaries (hash maps), sets, tuples
– each optimized for different use cases (Why
I moved from JavaScript to Python for LeetCode - DEV Community). It
also has modules like collections (providing deque,
Counter, etc.) and heapq for heaps, which are extremely
handy for interview problems involving queues, stacks, or priority
queues. Many algorithms can be implemented using Python’s standard
library (e.g. sorting with sorted() , binary search with
bisect ), reducing the amount of code you must write. This
richness means Python often has a ready-made tool for common interview
tasks (Why
I moved from JavaScript to Python for LeetCode - DEV Community) (Why
I moved from JavaScript to Python for LeetCode - DEV
Community). |
Basic structures only: JavaScript historically had
just arrays (for lists) and objects (for key-value maps) as built-ins
(Why
I moved from JavaScript to Python for LeetCode - DEV Community). ES6
added Map and Set classes for dictionaries and
sets, but there is still no native deque or heap structure (Why
JavaScript is actually a great LeetCode/Interview language - DEV
Community). You can use arrays to simulate stacks/queues (with
push/pop and shift ), but operations like
removing from the front of an array are not as efficient as Python’s
collections.deque (Why
I moved from JavaScript to Python for LeetCode - DEV Community).
Similarly, to get a priority queue, you would have to use an array and
sort it or implement a heap manually. JavaScript’s standard library has
fewer algorithm-specific utilities, so interview solutions may require
writing more helper code from scratch (libraries exist but usually
aren’t available in coding test environments). |
Execution Speed & Memory Usage | Slower execution, higher memory: Python is generally slower at runtime because it’s interpreted (CPython has no JIT). For very large inputs or heavy computations, a Python solution might run close to the time limit – there are anecdotes of hitting timeouts in Python even with optimal algorithms (Why JavaScript is actually a great LeetCode/Interview language - DEV Community). In contrast, JavaScript’s JIT-compiled engine often runs the same algorithm faster. Python’s structures also tend to use more memory (each object has overhead), so a large list/dict can consume more memory than an equivalent structure in JS (Node.js vs Python: Selecting the Ideal Backend Tech for 2024). The upside is that Python’s high-level operations (big integers, slicing, etc.) are handled for you (convenience at some cost). In most interview problems, if your algorithmic complexity is good, Python is fast enough, but it’s something to keep in mind for edge cases. | Fast JIT execution, lower memory: JavaScript executes very fast under modern engines – its JIT compilation and optimizations let it approach the speed of lower-level languages like Java/C++ (Why JavaScript is actually a great LeetCode/Interview language - DEV Community). This means if you write an efficient algorithm in JS, you’re unlikely to hit performance issues on coding platforms. Memory usage for JavaScript’s data types is generally lower than Python’s for the same workload, since primitive types (numbers, booleans) are lightweight. Both Python and JS have garbage collection, so memory is managed for you, but JS’s overall memory footprint for a large data structure tends to be a bit smaller (Node.js vs Python: Selecting the Ideal Backend Tech for 2024). In short, JavaScript provides better raw performance, which can be a confidence boost when handling big inputs in interviews. |
Ecosystem & Job Market Relevance | Versatile ecosystem: Python has a massive ecosystem in areas like web backends (Django/Flask), data science, machine learning, automation, etc. It’s often ranked just behind JavaScript in overall popularity, reflecting its broad use across industries (Programming languages with the highest labor demand 2024 : r/AskProgramming). In interview practice, Python is extremely common – many competitive programming and LeetCode discussions use Python, so a wealth of learning resources is available. For job hunting, knowing Python opens up roles in AI/ML, data analysis, scientific computing, and backend development (Python vs JavaScript: What to Choose in 2024 - Monarch Innovation). Many companies (especially in those domains) allow or encourage Python in interviews. (For front-end roles, Python is less directly relevant, but those interviews might focus on system design or algorithms where Python can still be used.) | Dominant for web: JavaScript is arguably the most in-demand programming language today, powering the vast majority of web applications (Programming languages with the highest labor demand 2024 : r/AskProgramming). Its ecosystem (Node.js, NPM, and frameworks like React/Angular) makes it indispensable for front-end and full-stack development (Python vs JavaScript: What to Choose in 2024 - Monarch Innovation). In coding interviews, JavaScript is commonly used if you’re targeting a front-end or full-stack position, and companies hiring for those roles will expect proficiency in it. While historically algorithm interview communities were dominated by Python/Java/C++, JavaScript has become very popular as more candidates from web backgrounds use it. Job-market wise, JavaScript skills are highly marketable – it’s often said to be “absolutely dominating” job postings, with Python in second place (Programming languages with the highest labor demand 2024 : r/AskProgramming). So choosing JS for interviews is wise if it aligns with the job’s tech stack. |
Debugging & Error Handling | Clear errors: Python tends to fail fast and loud.
It is strongly typed at runtime, so invalid operations (like adding
incompatible types or accessing an out-of-range index) immediately raise
exceptions with informative tracebacks (Why
are using exceptions more acceptable in python than javascript - Stack
Overflow). This helps with debugging during interviews, since
mistakes are caught early rather than silently producing wrong results.
Python uses try/except blocks for error handling;
exceptions are a normal part of control flow for unusual conditions
(e.g. catching a KeyError instead of checking existence
every time). Overall, Python’s error messages are usually clear
(pointing out the exact issue and location), which makes it
straightforward to fix bugs in your code. |
Silent failures possible: JavaScript uses
try/catch for exceptions similarly, but many errors won’t
throw exceptions by default. The language often “glosses over”
issues – for example, using a non-existent array index just
gives undefined and keeps running, rather than throwing an
error (Why
are using exceptions more acceptable in python than javascript - Stack
Overflow). This means a bug can fail silently, and you have to
carefully watch for unexpected undefined/NaN values. Type
coercion can also cause subtle issues without any error (e.g.
"5" * 2 becomes 10 in JS, whereas in Python
"5" * 2 would throw a TypeError). In practice, debugging
JavaScript might involve more manual checks or console logs to pinpoint
logic errors. On the plus side, modern debuggers (browser DevTools,
Node.js inspector) are very powerful for stepping through code when you
have that environment. In interview platforms, you’ll primarily rely on
test cases, so in JS it’s important to add your own validations or use
strict equality (=== ) to avoid tricky coercion bugs. |
Transitioning from JavaScript to Python | Adapting to Python: If you’re moving from JS to
Python, focus on Python’s idioms and differences. Embrace whitespace –
Python uses indentation instead of {} braces, and a missing
colon or wrong indent will cause errors, so practice writing neatly
indented code. Get used to Python syntax for common constructs (e.g.
for x in list instead of C-style index loops,
and /or instead of
&& /| | ,
and True /False instead of
true /false ). Leverage Python’s powerful
built-ins and libraries rather than coding everything manually – for
instance, use collections.Counter for frequency counts or
heapq for a priority queue (in JS you might have had to
write these from scratch). Also, be mindful that Python won't implicitly
convert types; you'll need to convert types explicitly (e.g. use
str() to convert a number to a string) where JavaScript
might do it for you, but this strictness prevents certain bugs. One big
advantage of switching is the abundance of resources – many tutorials
and solution discussions are in Python, so you can directly study those
instead of translating from JS (Why
I moved from JavaScript to Python for LeetCode - DEV Community).
Practice plenty of problems in Python and read Pythonic solutions to
adjust your coding style and avoid common pitfalls. |
Mindset shift: As a JS developer, you already
understand core programming concepts, but avoid carrying over
JavaScript-specific habits. Don’t rely on JS truthy/falsy quirks or
loose typing in Python – Python will enforce correctness (e.g. 0 vs
None vs False are distinct). Be ready to write
out data structure manipulations using Python's methods (for example,
my_list.append(x) instead of array.push(x) ).
In short, transitioning from JS is about unlearning JS-specific syntax
and embracing Python’s way of doing things. (Transitioning from
Python to JavaScript would involve the opposite – learning to use
braces/semicolons, managing type coercion, etc. – but that’s outside our
scope here.) |
JavaScript vs Python Core Concepts
Concept | JavaScript Example | Python Example & Notes |
---|---|---|
Variables | let x = 10; const y = 20; |
x = 10 (dynamic typing)y = 20
(reassignable) |
Constants | const PI = 3.14; |
PI = 3.14 (by
convention)from math import pi (built-in
constants) |
Functions | function add(a, b) { return a + b; } |
def add(a, b): return a + b Indentation defines blocks |
Data Types | Dynamic typing | Dynamic typing:s = "text" n = 3.14
(type hints optional: n: float = 3.14 ) |
Collections | const arr = [1,2]; const obj = {key:1}; |
arr = [1,2] (list)d = {"key": 1}
(dict)t = (1,2) (tuple)s = {1,2}
(set) |
Zero Values | undefined , null |
None (single null-like value)No undefined concept |
Error Handling | try/catch |
try/except EAFP: "Easier to ask forgiveness than permission" |
Concurrency | Promise , async/await |
asyncio ,
async/await threading ,
multiprocessing |
Memory | Automatic GC | Automatic GC Reference counting + cycle detection |
Build | node script.js |
python script.py Interpreted, no compilation step |