Table of Contents
- Introduction
- Names
- Syntax
- Methods, Functions, and Properties
- Getting Over It…
- Benefits To Python
- Conclusion
Introduction
Hello there! If you decided to not read the author, I’m Madison! Fun fact about me, I love Javascript; I just spent a whole year mastering the language, I can speak it like I speak English. The ability to translate your thoughts into code within seconds warms your heart! However, one day I decided to learn a new language: Python. Honestly, I just can’t help but to critique it as if I am the coding master. (I am not…)
Names
On my journey of learning, I started off small; Just a simple Codecademy to help me learn the basics. That’s when the horrors of subtle differences came to haunt me…
Now, in Javascript, we have my favorite data structure: arrays! All the beautiful built-in methods, what’s not to love? But you know what it’s called in Python? A list.
// Javascript
const myArray = [1,2,3]
# Python
my_list = [1,2,3]
And look! They are the exact same thing! I know it’s a different language, but it would be so much simpler for the coding world if we all had the same names for everything.
Syntax
Before I even started coding, I thought all languages were notorious for needed a semicolon at the end of every line; Turns out, everyone uses whatever symbols they want! Even I thought it was weird when a ternary operator uses a colon in JS, but apparently Python is obsessed with them.
# Python
if donation >= 1000:
print("You've achieved gold status")
elif donation >= 500:
print("You've achieved silver donor status")
else:
print("You've achieved bronze donor status")
Everywhere I look I find colons; now I have to press shift a lot more often.
Additionally, now I have to get used to writing everything in snake case. What if I wanted a long, descriptive variable name? Now I have to do double the work just to type it.
// Javascript
const superLongVariableNameToExpressHowMuchILoveCamelCase = true
# Python
super_long_variable_name_to_express_how_much_i_dislike_snake_case = True
You can also see how in JS boolean values are in lowercase, but in Python they’re uppercase? I told you guys shift would be used a lot more.
JS also allows me to add an extra layer of protection to my variables: const, let, or var helps me define the “rules” of the variable, but Python loves the thrill of risks I see…
Methods, Functions, and Properties
This one is enough to make my blood boil…
// Javascript
const myArray = [1,2,3]
console.log(myArray.length) // logs 3
# Python
my_list = [1,2,3]
print(len(my_list)) # prints 3
Length in JS is a property, but in Python it’s a function? What is the meaning of this?!
Not only does Python loooove to switch up names and data types, they also love to change functionality. Look at this…
// Javascript
const myArray = [1,2,3]
myArray.pop()
console.log(myArray) //[1,2]
# Python
my_list = [1,2,3]
my_list.pop(2)
print(my_list) # [1,2]
Now, I won’t lie to you, in Python, if .pop() is not given a value, it will remove the last index just like JS. Regardless, I now need to remember this additional functionality instead of having a separate method that can do the job just as well.
Getting over it…
Now that we got that out of our system, in all seriousness, Python is just as amazing as JS. Everything has pros and cons to it; So as much as I love JS, I need to accept that nothing will ever compare.
Learning a new language is actually the best thing you can do! Yes, it will be stressful, some parts may be annoying and tedious, but in the end it’ll be so worth it. Everyday you should be working towards expanding your knowledge, and Python has done exactly that for me!
Benefits To Python
Actually, there are some benefits to using Python versus JS; If you think JavaScript has a lot of built-in methods, you should look at Python methods…
// Javascript
function topKFrequent(nums, k) {
const freqMap = {};
for (const num of nums) {
freqMap[num] = (freqMap[num] || 0) + 1;
}
const sorted = Object.entries(freqMap)
.sort((a, b) => b[1] - a[1])
.map(entry => parseInt(entry[0]));
return sorted.slice(0, k);
}
console.log(topKFrequent([1, 1, 1, 2, 2, 3], 2)); // Output: [1, 2]
# Python
from collections import Counter
def top_k_frequent(nums, k):
return [item for item, _ in Counter(nums).most_common(k)]
print(top_k_frequent([1, 1, 1, 2, 2, 3], 2)) # Output: [1, 2]
See, here we are finding the most frequent numbers in the given array/list. In JS, we would have to use a frequency object to store the count of each number, then use multiple array methods to find and return the k most frequent numbers.
However, in Python, we can import a counter, which converts our list into an object, storing each numbers’ frequency. Then, utilizing the .most_common() method which returns our object as a list of tuples of the k most frequent numbers and their corresponding values. item for item, _
allows us to “destructure” our result returning only the most frequent numbers as a list.
While the Python version may be more complicated to understand, it provides a pretty concise and more efficient way to solve this problem.
Conclusion
Like I had said before, learning a new language could be pretty annoying (trust me I know…), but there are so many benefits to it! There is a lot of interesting functionality that Python gives you access to, so I recommend trying to learn the language. Just remember my blog when you run into a slight difference between two languages!
Top comments (0)