From Script to Snake - JavaScript to Python

ismaelenriquez

Ismael Enriquez

Posted on July 1, 2024

From Script to Snake - JavaScript to Python

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

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 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

Press play and it should run your code

Control flow

To run Python code in your terminal, use:

python3 <File Name>
Enter fullscreen mode Exit fullscreen mode

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

Javascript

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

Javascript Control Flow

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

In Python, f"Hello, {name}!" is equivalent to using backticks with ${name} in JavaScript.

Javascript Function

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

Javascript Loop

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!

💖 💪 🙅 🚩
ismaelenriquez
Ismael Enriquez

Posted on July 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related