Python for JavaScript Devs: Empowering JS Developers to Soar

Welcome JavaScript Developers to the world of Python! Whether you’re a seasoned programmer or just starting out, Python’s intuitive syntax and versatility make it an excellent addition to your skill set. In this guide, we’ll guide you through the basics of setting up Python and getting ready to write your first lines of code.

While I won’t cover the fundamental aspects of Python, such as syntax, variables, control flow, and functions, extensively in this post, our focus will be on contrasting the dissimilarities in Python syntax with JavaScript. Furthermore, we’ll explore any commonalities that may exist between these two languages.

How to Install Python

Before you begin writing code in Python, you’ll need to have Python installed on your computer. Let’s see how to do it on Windows, Linux, and MacOS.

On Linux

  1. Check the current version installed: python --version
  2. Optionally, If you see python2 is installed, you may uninstall it as it is no longer supported has reached its end of life.
  3. To uninstall python 2 on:
    • Debian/ Ubuntu : sudo apt remove python2
    • Fedora: sudo dnf remove python2
    • CentOS: sudo yum remove python2

Now, it’s time Install Python 3

  1. Update the package list (optional, but recommended):
    • Ubuntu/Debian : sudo apt update
    • Fedora: sudo dnf update
  2. Install Python 3:
    • Ubuntu/Debian: sudo apt install python3
    • Fedora: sudo dnf install python3

Note: Python 3 does not backward compatible with its Python 2.x version. If you decided to keep python 2 in your Ubuntu/Debian  systems, you can install the python-is-python3 package. This will male python 3 the default verion in your system

To install python-is-python3  run On Debain/Ubuntu: sudo apt-get install python-is-python3 -y

On Windows

Using Microsoft store( Recommended )

  1. Launch the Microsoft Store on your Windows system. 
  2. Search for Python and proceed to install the most recent version (e.g., Python 3.11). 
  3. Opting for the Microsoft Store ensures automatic updates, and if you ever decide to uninstall Python, the process is straightforward.

Using Python installer

  1. Visit the official Python website at https://www.python.org/.
  2. Navigate to the “Downloads” section.
  3. Choose the latest version of Python (e.g., Python 3.11.4 at the time of writing this post) for Windows.
  4. Download the installer and run it.
  5. Make sure to check the box that says “install launcher for all users” as it is recommended. 
  6. Also, check the box that says “Add Python to PATH” during installation so that you can run Python from the command line.

Using WSL

WSL stands for Windows Subsystem for Linux. By installing WSL, you enable the execution of a Linux command-line interface within the Windows environment.  You can install Python on WSL and utilize within WSL.

  1. install WSL
  2. Then, installing Python is the same way you install Python on Linux

On macOS

Using Homebrew (Recommended)

  1. Install Homebrew (if not already installed)
  2. Install Python 3: brew install python
  3. Verify the installation: python3 --version

Using Python installer

  1. Download the Installer: Visit the official Python website at https://www.python.org/downloads/macos/ and download the latest version of Python for macOS.
  2. Run the Installer: Open the downloaded package (a .dmg file) and run the installer. Follow the prompts to install Python.
  3. Add Python to PATH (Optional): During the installation process, there might be an option to “Add Python to PATH.” Ensure this option is selected to make Python accessible from the Terminal.
  4. Verify the installation: Open Terminal and run:python3 --version

It’s important to note that macOS typically comes with a pre-installed version of Python (usually Python 2). The steps above install a separate instance of Python 3. If you’re planning to use Python 3 for development, it’s recommended to use the steps above to install Python 3 and make it the default version for your projects.

Your first Python program

Now that you have Python installed, let’s write your first Python program: the classic “Hello, World!” example.

  1. Open a text editor or an Integrated Development Environment (IDE) such as Visual Studio Code, PyCharm, or IDLE (Python’s built-in IDE).
  2. Type the following code: print(“Hello, World!”)
  3. Save the file with a .py extension (e.g., hello.py).
  4. Open a terminal or command prompt.
  5. Navigate to the directory where you saved your hello.py file.
  6. Type python hello.py on the terminal / command prompt and press Enter.

Python REPL ( Read-Eval-Print Loop )

Python REPL is an interactive programming environment commonly used for dynamic testing and experimenting with code in programming languages. Developers can quickly write and execute Python code snippets, evaluate expressions, and see immediate results without needing to create a separate script or file.

To start the Python REPL, you typically open a terminal or command prompt on your computer and type python or python3, depending on your system configuration. This will launch the Python interpreter, and you’ll see the Python prompt (>>>) indicating that you can start entering Python code.

>>> print("Hello, world!") //then Press Enter

The Python interpreter reads and evaluates the code you entered,  then prints the result of the evaluation to the console.

Hello, world!

After that, the REPL then waits for your next input. You can continue entering and executing code as many times as you want, effectively creating a loop of read-eval-print steps.

You can exit the Python REPL by typing exit() or pressing Ctrl + Z on Windows (or Ctrl + D  on Linux or macOS )

Python syntax compared to JavaScript

Syntax in programming refers to the set of rules that dictate how a programming language’s statements should be structured.

Python’s clean and readable syntax sets it apart from JavaScript in several ways, making it an attractive choice for many developers. Here’s how Python’s syntax compares favorably to JavaScript:

AspectPythonJavaScript
IndentationWhitespace-basedCurly braces {}
SemicolonsNot requiredOften required
Variable DeclarationNo explicit type declarationRequires var, let, const
Type Coercion
(Automatic or manual conversion
of one data type to another )
Generally straightforwardCan lead to unexpected behavior
Function Definitionsdef keyword, clear indentationfunction keyword, varied syntax
Block-level ScopingIndentation-basedBlock-level scoping
String ManipulationElegant string interpolation and formattingConcatenation or template literals
Implicit Line ContinuationParentheses and operatorsSemicolons or explicit \ continuation
Parentheses UsageLess common in conditionals and functionsCommon in conditionals and functions
Built-in Data StructuresLists, dictionaries, setsSimilar structures with varied syntax
PythonJavaScript
# Indentation and whitespace-based blocks
if True:
    print(“Indented block”)

# Semicolons not required
x = 5
y = 10

# Variable declaration and type coercion
sum_result = x + y
concat_result = “Sum: ” + str(sum_result)

# Function definitions
def greet(name):
    return “Hello, ” + name

# String manipulation and implicit line continuation
message = (
    “This is a long message that needs to be “
    “continued on the next line using parentheses.”
)

# Built-in data structures and iteration
numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
    total += num

print(concat_result)
print(greet(“Alice”))
print(message)
print(“Total:”, total)

// Curly braces for code blocks
if (true) {
    console.log(“Curly brace block”);
}

// Semicolons often required
var x = 5;
var y = 10;

// Variable declaration and type coercion
var sumResult = x + y;
var concatResult = “Sum: ” + sumResult.toString();

// Function definitions
function greet(name) {
    return “Hello, ” + name;
}

// String manipulation and implicit line continuation
var message = (
    “This is a long message that needs to be ” +
    “continued on the next line using parentheses.”
);

// Built-in data structures and iteration
var numbers = [1, 2, 3, 4, 5];
var total = 0;
for (var i = 0; i < numbers.length; i++) {
    total += numbers[i];
}

console.log(concatResult);
console.log(greet(“Alice”));
console.log(message);
console.log(“Total:”, total);
comparing two programs in Python and JavaScript

What are similarities with python and JavaScript

Let’s see if there is any similarities with with these languages

  • High-Level Languages: Both Python and JavaScript are high-level programming languages, meaning they provide abstractions that make coding more intuitive and less focused on hardware details.
  • Dynamic Typing: Both languages use dynamic typing, allowing variables to change types during runtime without explicit type declarations.
  • Interpreted Languages: Python and JavaScript are interpreted languages, meaning code is executed line by line without a separate compilation step.
  • Rich Standard Libraries: Both languages come with extensive standard libraries that provide a wide range of built-in functions and modules for various tasks.
  • String Manipulation: Both languages offer powerful string manipulation capabilities and support features like string interpolation and formatting.

What are the use cases of python

  1. Web Development (Backend): Python is commonly used for building web applications on the server-side. Frameworks like Django and Flask provide tools for developing robust and scalable web backends.
  2. Data Analysis and Science: Python’s rich libraries, such as NumPy, Pandas, and Matplotlib, make it a popular choice for data analysis, scientific computing, and visualization.
  3. Machine Learning and AI: Python offers libraries like TensorFlow, PyTorch, and scikit-learn, making it a top choice for machine learning, artificial intelligence, and deep learning projects.
  4. Automation and Scripting: Python’s readability and cross-platform compatibility make it ideal for writing scripts and automating tasks, system administration, and data manipulation.
  5. Desktop Applications: Python can be used to develop cross-platform desktop applications using libraries like PyQt and Tkinter.

Wrapping up

In summary, you’ve learned about Python from installing it on different systems to writing your first program. You’ve seen how Python’s neat style is different from JavaScript. Although they look different, both can help you create things. As you’ve explored Python’s many uses, like making websites and studying data, you’ve seen how handy it can be. With these new skills, you’re ready to try out Python for all sorts of cool projects!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top