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.
Table of Contents
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
- Check the current version installed:
python --version
- Optionally, If you see python2 is installed, you may uninstall it as it is no longer supported has reached its end of life.
- To uninstall python 2 on:
- Debian/ Ubuntu :
sudo apt remove python2
- Fedora:
sudo dnf remove python2
- CentOS:
sudo yum remove python2
- Debian/ Ubuntu :
Now, it’s time Install Python 3
- Update the package list (optional, but recommended):
- Ubuntu/Debian :
sudo apt update
- Fedora:
sudo dnf update
- Ubuntu/Debian :
- Install Python 3:
- Ubuntu/Debian:
sudo apt install python3
- Fedora:
sudo dnf install python3
- Ubuntu/Debian:
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 )
- Launch the Microsoft Store on your Windows system.
- Search for Python and proceed to install the most recent version (e.g., Python 3.11).
- Opting for the Microsoft Store ensures automatic updates, and if you ever decide to uninstall Python, the process is straightforward.
Using Python installer
- Visit the official Python website at https://www.python.org/.
- Navigate to the “Downloads” section.
- Choose the latest version of Python (e.g., Python 3.11.4 at the time of writing this post) for Windows.
- Download the installer and run it.
- Make sure to check the box that says “install launcher for all users” as it is recommended.
- 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.
- install WSL
- Then, installing Python is the same way you install Python on Linux
On macOS
Using Homebrew (Recommended)
- Install Homebrew (if not already installed)
- Install Python 3:
brew install python
- Verify the installation:
python3 --version
Using Python installer
- Download the Installer: Visit the official Python website at
https://www.python.org/downloads/macos/
and download the latest version of Python for macOS. - Run the Installer: Open the downloaded package (a .dmg file) and run the installer. Follow the prompts to install Python.
- 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.
- 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.
- Open a text editor or an Integrated Development Environment (IDE) such as Visual Studio Code, PyCharm, or IDLE (Python’s built-in IDE).
- Type the following code: print(“Hello, World!”)
- Save the file with a .py extension (e.g., hello.py).
- Open a terminal or command prompt.
- Navigate to the directory where you saved your hello.py file.
- 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:
Aspect | Python | JavaScript |
---|---|---|
Indentation | Whitespace-based | Curly braces {} |
Semicolons | Not required | Often required |
Variable Declaration | No explicit type declaration | Requires var, let, const |
Type Coercion (Automatic or manual conversion of one data type to another ) | Generally straightforward | Can lead to unexpected behavior |
Function Definitions | def keyword, clear indentation | function keyword, varied syntax |
Block-level Scoping | Indentation-based | Block-level scoping |
String Manipulation | Elegant string interpolation and formatting | Concatenation or template literals |
Implicit Line Continuation | Parentheses and operators | Semicolons or explicit \ continuation |
Parentheses Usage | Less common in conditionals and functions | Common in conditionals and functions |
Built-in Data Structures | Lists, dictionaries, sets | Similar structures with varied syntax |
Python | JavaScript |
---|---|
# 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); |
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
- 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.
- Data Analysis and Science: Python’s rich libraries, such as
NumPy
,Pandas
, andMatplotlib
, make it a popular choice for data analysis, scientific computing, and visualization. - Machine Learning and AI: Python offers libraries like
TensorFlow
,PyTorch
, andscikit-learn
, making it a top choice for machine learning, artificial intelligence, and deep learning projects. - Automation and Scripting: Python’s readability and cross-platform compatibility make it ideal for writing scripts and automating tasks, system administration, and data manipulation.
- Desktop Applications: Python can be used to develop cross-platform desktop applications using libraries like
PyQt
andTkinter
.
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!