From Script to Snake - JavaScript to Python
Tags: python, learning, javascript, tutorial
Published: 2024-07-01 22:30:09

## Table Of Contents

- Introduction
- Popularity
- How to Setup On VSCode
- Control flow
- Variables
- Functions
- Loops
- Conclusion

## Introduction

Are you a programmer who knows JavaScript but wants to learn Python? Here's an easy guide to dive into Python. Python is a powerful and versatile programming language used for web development, automating tasks, data analysis, and notably, artificial intelligence and machine learning. Known for its simplicity and readability, Python is accessible for beginners yet robust enough for complex projects. Its versatility has solidified its position as one of the most popular languages in 2024. Follow me as I transition from JavaScript to Python and explore machine learning.

## Popularity

![Python popularity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezcdg36gxe7ldpmybmiz.png)

Python is known for its ease of use and flexibility, making it ideal for beginners and experienced developers alike. Its popularity is underscored by:

- **Extensive Libraries:** Python boasts a wide array of libraries and frameworks that simplify tasks such as data analysis, web development, and machine learning.
- **Industry Adoption:** Python is trusted by major tech companies like Google, Netflix, Uber, and Dropbox.
- **Career Opportunities:** Python's widespread use opens many job opportunities, with salaries ranging from $66,000 to $164,000 in the US.

## How to Setup On VSCode

1. **Download Python:**
   - Visit [Python](https://www.python.org) and download the latest version of Python for your operating system.
   - During installation, ensure to check "Add Python to PATH".

2. **Open VSCode:**
   - After installing Python, open Visual Studio Code (VSCode).

3. **Install the Python Extension in VSCode:**
   - If you haven't installed the Python extension yet, VSCode will prompt you to install it when you create a Python file (*.py).
   - Alternatively, you can install the Python extension from the Extensions view in VSCode.

4. **Write and Run Python Code:**
   - Start coding Python directly in a .py file.
   - Run your Python code in VSCode.

![VSCode setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjoi0hjkt80pz5o0vnu2.png)
### Press play and it should run your code

## Control flow

To run Python code in your terminal, use:

```
python3 <File Name>
```

### Now, let's get into the syntax of Python and compare it with JavaScript to highlight their similarities and differences.

## Variables

In Python, variable declaration is straightforward and does not require keywords like `let` or `const` as in JavaScript. Variables are simply assigned using the syntax `variable_name = value`.


![Python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwx10tuugfeyozwkib73.png)

![Javascript](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jzqu6sh8lte2ffa1qrx.png)

## Control Flow

Control flow decides the order in which statements run based on conditions in programming. In Python, we use `if`, `else`, and `elif` along with `==` (equals) and `!=` (not equals) for comparisons.


![Python Control Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rwkhn4nmb5t54cnwbtv.png)


![Javascript Control Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz7i0cwa1aptnwsxdr39.png)

In JavaScript, curly braces `{}` are used to define code blocks, including if statements, loops, functions, and other structures where multiple statements need to be grouped together. JavaScript also uses `console.log()` for outputting text to the console.

## Functions:

In Python, functions are defined using `def` followed by the function name and parameters in parentheses. The function's code is indented to mark its scope.


![Python Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzzaib2c3e07c2cfu8u7.png)
### In Python, `f"Hello, {name}!"` is equivalent to using backticks with `${name}` in JavaScript.

![Javascript Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzqx8a89wwsfce9q3gab.png)

## Loops

Python, like JavaScript, uses `for` loops and `while` loops for iterating through data or repeating tasks. Unlike JavaScript, Python's `for` loop directly iterates over items of lists or strings without needing an explicit incrementer. Key functions in Python include:

- **len():** Used to determine the number of items in a sequence.

- **range():** Generates a sequence of numbers, commonly used with `for` loops when iterating a specific number of times.


![Python Loop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wr7o6bl67naa1rgehz7k.png)


![Javascript Loop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ckiksgkuvdayu2s798.png)

## Conclusion

Moving from JavaScript to Python opens up a world of opportunities with a versatile and powerful programming language. Learning these essential topics is key to tackling exciting projects. For those looking to improve their Python skills, starting with resources like Codecademy's Python 3 lessons can build a solid foundation. Additionally, platforms like LeetCode offer practical challenges to enhance coding proficiency in Python.

Thanks for reading!