Home >Web Front-end >JS Tutorial >Fullstack Development: Learning Python as JavaScript Developers
I have been working as a frontend developer for more than 8 years, and in the last 2 years, I decided to rethink my career and how I can grow. I found that frontend technology changes frequently: different frameworks, concepts, and the gap between React class components and hooks. I realized that all of this is just abstraction used to express business needs and personal vision. From this point, I decided to change my career path and become a full-stack developer slightly.
As we all know, frontend development is all about JavaScript nowadays, which is why I decided to learn Node.js and its main frameworks. All frontend developers encounter Node.js in one way or another, and as a Senior Frontend Developer, you should be able to write basic endpoints in Node.js with Express or another library. After 2 years of active development on the Node.js side, when my job became 50/50 between frontend and backend, I discovered that most projects don’t limit themselves to just one language.
Node.js is not the ideal tool for everything, especially when you’re working in a larger company. Different languages provide different solutions or are more optimal for solving specific business cases. This is why I started researching what I could learn as my second backend language and how I could utilize it in the future.
I want to share my experience and why I decided to stick with Python after trying to learn Rust (mostly not for web development), Swift (which is primarily a mobile-first solution), and Golang. Here, you’ll find out why I believe Python is a great opportunity for frontend developers to learn and how to start using it.
Nowadays, AI is something that everyone is talking about. Mentioning it in an interview as part of your experience always gives you extra points. Almost all companies are trying to incorporate AI into their products, and Python goes hand in hand with AI and Machine Learning. By learning Python, you not only gain the opportunity to write web applications with frameworks like Django, Flask, and FastAPI, but you can also start working with Machine Learning and AI services.
On one hand, learning more complex languages like Rust, Go, or Elixir is a good idea if you want to become a better programmer. However, from a career perspective, it's not an easy transition to becoming a backend developer with a completely different language that you're less familiar with.
Python is a dynamically typed programming language. As a JavaScript developer who has spent a significant part of your career in a similar environment, this shouldn't intimidate you, as you already know what kinds of issues to expect in the code.
With Python, you can not only start writing web applications but also leverage your skills in AI-related fields, which gives Python a significant advantage as a second language.
The learning curve was straightforward. In Python, you need to learn some basic concepts for sure. If you have experience with JavaScript it shouldn't be a big deal.
Here is the code example in Python:
import random def guess_the_number(): number_to_guess = random.randint(1, 100) attempts = 0 guessed = False print("Welcome to the Number Guessing Game!") print("I'm thinking of a number between 1 and 100. Can you guess what it is?") while not guessed: user_guess = int(input("Enter your guess: ")) attempts += 1 if user_guess < number_to_guess: print("Too low! Try again.") elif user_guess > number_to_guess: print("Too high! Try again.") else: print(f"Congratulations! You guessed the number {number_to_guess} in {attempts} attempts.") guessed = True guess_the_number()
I don’t think you’ll find anything too complex here. Even if you haven’t learned Python before, you can understand almost all the lines without reading the documentation.
The biggest difference you’ll notice is that Python uses indentation to define code blocks instead of curly braces. That might seem strange, and I still find it a bit unusual, but after a while, you get used to it, and reading code becomes easier.
Apart from that, many concepts in Python are similar to those in JavaScript. Instead of console.log, you can use print, and for string interpolation, you can add f at the beginning of the string and use almost the same syntax as in JavaScript.
Here is the JavaScript version of the code above:
function guessTheNumber() { const numberToGuess = Math.floor(Math.random() * 100) + 1; let attempts = 0; let guessed = false; console.log("Welcome to the Number Guessing Game!"); console.log("I'm thinking of a number between 1 and 100. Can you guess what it is?"); while (!guessed) { const userGuess = parseInt(prompt("Enter your guess: "), 10); attempts++; if (userGuess < numberToGuess) { console.log("Too low! Try again."); } else if (userGuess > numberToGuess) { console.log("Too high! Try again."); } else { console.log(`Congratulations! You guessed the number ${numberToGuess} in ${attempts} attempts.`); guessed = true; } } } guessTheNumber();
You can learn a lot of different concepts in Python. I showed the most confusing for me as a JavaScript developer.
As a JavaScript developer, you might be familiar with how to use code blocks with if/else and other operators. In most of the cases you just add {} and that’s it. Python's indentation-based system can be tricky.
Let’s see the JavaScript code:
if (role === "admin") { const posts = getDraftPosts() if (posts.length === 0) { throw NotFound() } return posts }
Python analog:
if role == "admin": posts = get_draft_posts() if posts.length == 0: raise NotFound() return posts
As you can see readability of blocks in Python could be challenging from the first view. This is why for me it was important to avoid deeply nested blocks because it is hard to read and easy to miss correct indention. Keep in mind that Python could attach your code to a wrong code block because of missed indention.
Python is a dynamic typing language but I was surprised to find Python built-in Types annotation.
def add(x: int, y: int) -> int: return x + y
You don’t need to install additional features, only what you need in Python *3.5 and above.*
Even more complex types could be described as equal to Typescript:
# enums from enum import Enum # import enum for built in lib class Season(Enum): # extend class to mark it as enum SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4 print(Season.SPRING.name) # SPRING print(Season.SPRING.value) # 1 # or generics def first(container: List[T]) -> T: return container[0] list_two: List[int] = [1, 2, 3] print(first(list_two)) # 1
For using these types you are not required to install something or transpile this code. This is something I missed in JavaScript, at least Node.js. I know Node.js is adding some basic types in the nearest version (see Medium post about node.js built-in types support) but it looks poor now if you compare it with Python.
JavaScript uses an event-driven, non-blocking model, while Python's Global Interpreter Lock (GIL) can be a confusing concept in multi-threaded programs.
The Python Global Interpreter Lock (GIL) is a mechanism that ensures only one thread executes Python code at a time. Even if your Python program has multiple threads, only one thread can execute Python code at a time due to the GIL.
With JavaScript, you can achieve multithreading with web workers, but in Python, you need to use additional libraries to accomplish this.
JavaScript's "multiple ways to do it" philosophy doesn't work as well in Python because Python adheres more closely to the concept "There should be one - and preferably only one - obvious way to do it."
In the JavaScript world, every company often creates its own code style guide, and it's fortunate if it follows basic JavaScript style recommendations. In reality, practices like using semicolons can vary widely, to the point where one project might use semicolons and another might not.
In Python, following Pythonic principles from PEP 8 (Python's style guide) is highly recommended. This guide outlines the basic rules of how to write Python code.
To write better code, it's essential to engage with the community and learn idiomatic Python practices that prioritize clarity and simplicity.
This part might also be confusing. In JavaScript, you can usually add a package manager and start installing dependencies without any issues. However, Python’s pip and virtual environments may be new concepts.
In Python, when using additional dependencies, it’s highly recommended to use a separate virtual environment. Installing dependencies with pip (the Python equivalent of npm in JavaScript) in your environment could potentially break system utilities or the OS itself, as the system Python interpreter (the one that comes pre-installed with your operating system) is used by the OS and other system utilities.
As a solution, you can create a virtual environment with venv module:
python -m venv myenv myenv\Scripts\activate # for windows source myenv/bin/activate # for Mac
After you enter the virtual environment you can install all dependencies without any problem or danger for your root environment.
Learning a new language is always challenging. I started learning Python basics on an online platform, where I also completed some small projects. Here is the plan I used during my learning process:
You can find a lot of help in Reddit communities or Discord servers while learning. I’m mostly a Reddit user and would suggest finding subreddits for Python and the framework you decide to use for your first application.
Remember to use the official documentation. In my opinion, it looks overwhelming, and most of the time, I try to find related articles if I get stuck on a concept.
Make sure to read PEP 8 — Style Guide for Python Code, where you can find basic rules on how to write Python code.
As I reflect on my journey from a JavaScript developer to embracing Python, I have no regrets. This transition has opened up exciting opportunities, particularly in the realms of AI and machine learning, which I now leverage extensively in my projects, especially on the backend.
Looking ahead, the possibilities with Python are vast. Whether it's web development, data science, automation, or delving deeper into AI and machine learning, Python provides a powerful and versatile foundation to build on and explore new horizons.
The above is the detailed content of Fullstack Development: Learning Python as JavaScript Developers. For more information, please follow other related articles on the PHP Chinese website!