Learn Python: A Beginner’s Guide for JavaScript Developers

Rana Ibrar
8 min readMar 19, 2023

--

Do you need to learn Python for your current job, for the job you want, or just for fun? Welcome to the Python-JavaScript crossover world, and welcome to this article.

Python programming language is designed to be easy to understand and use by everybody. It is also used for web development. This is where we can start to compare its applications to the applications of JavaScript

This article will help you quickly speed up with Python, the programming language designed for beginners and everybody else. It will also explain what declaring a variable means, what a function is, what’s a class and an object, and how to write Python apps without blowing your mind

Although there are many differences between JavaScript and Python, learning the basic syntax will help you get started if you’re a JavaScript Developer.

Let’s begin!

Python Syntax

Declaring variables in Python is super easy. Keywords such as JavaScript’s “var”, “let” and “const” are unnecessary.

a = ‘a’
x = 5
c = False

x = 5

You can change the variable type by assigning a value of a different type to it:

x = 5 # x is a Integer type

x = ‘Hello’ # x is now a String

Unlike JavaScript, variables in Python are always block scoped.

Booleans are capitalized in Python

Semicolons in pythons are only used if we use multiple statements on the same line, otherwise, it is not used to terminate statements

Operators Types

· We use the type() function to check the type of an object in Python

· We use the typeof operator in JavaScript.

· typeof in JavaScript is only convenient when you want to check if a function exists

The graphical description of their syntax:

Arithmetic operators in Python and JavaScript:

Both Python and JavaScript share most of the arithmetic operators.

In addition to the division operator, Python also has a floor division operator.

Python represents the floor division with (//).

In JavaScript, we don’t have a particular floor division operator. Instead, we call the Math.floor() method

Divide down in JavaScript and in python

Comments in Python

Comments are critical to writing clean code.

Let’s see how you can use them in Python and JavaScript:

· In Python, we use a hashtag (#) to write a comment.

· In JavaScript, we write two slashes (//) to start a single-line comment

In Python: # Comment

In JavaScript: // Comment

Data Types

Let’s see the Python and JavaScript data types.

· JavaScript has six primitive data types: undefined, Boolean, String, Number, BigInt, and Symbol

· Python has the following built-in data types str :(text type), int, float, complex (Numeric types), dist, tuple, range :(sequence type), dict :(Mapping type), set, frozenset: (Set Types), bool :(Boolean Type), bytes, byte array, :(Binary Types).

💡 Note: In python string Literals we use single (‘ ’)or double (“ “) quotation marks.

Let’s Understand with an example!

Print(“Hello”)

Print(‘Hello’)

Output:

Hello

Hello

It means Hello with the double quotation and single have the same output.

Assign a string to a variable

To assign a variable we use the (=) sign.

A= “Hello”

Print(a)

Numeric data types

Python has three types of numeric data types which include: int (integers),float (floating-point numbers), and complex. Each one of them has its own properties, characteristics, and applications.

In contrast, JavaScript has two numeric types: Number and BigInt. Integers and floating-point numbers are both considered to be of type Number.

X=1 #int

Y= 2.8 #float

Z = 1j #complex

None vs. null

In Python, there is a particular value called None that we typically use to indicate that a variable doesn’t have a value at a particular point in the program.

In JavaScript, null is the equivalent of None.

None and Null in python and in JavaScript

Control structures

Conditions of use can be based on specific criteria but by default can be ignored by the user. True or False.

Let’s look at the difference between them in Python and JavaScript!

If statement

· In JavaScript, you must use curly brackets to indicate the condition.

· while in python it relies on indentation to indicate which lines of code are conditional codes.

if-else condition

Multiple conditions

For multiple conditions

· We use elif keyword followed by the condition in Python. After that, we write a colon ( :) and indent the code on the next line.

· After the condition in Javascript we will write keywords else if. After the condition is complete, we use curly braces.

Multiple condition statement

No switch

“No switch in Python?? You read it right there’s no switch in Python. So no switch party over!

For Loop and while loop in Python

let’s see how we can define different types of loops in Python

For Loop

For loops in Python are more concise and easier to understand than in JavaScript.

In Python, we write the keyword for followed by the name of the loop variable, the keyword in, and a call to the range() function specifying the necessary parameters. Then, we write a colon (:) following the body of loop indented.

Let’s understand with an example! ✨

fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
print(x)

While Loop

While loops are very similar in Python and JavaScript.

Python uses the keyword while followed by the condition, a colon (:), and in a new line, the body of the loop (indented). Then, we write a colon (:) followed by an indented block

The syntax is very similar in javascript. But we surround the condition with parentheses and the body of the loop with curly braces

Let’s understand with an example:

i = 1
while i < 6:
print(i)
i += 1!

Functions

A function is a group of related statements that takes on a single task. It helps us break our code into smaller and modular chunks. Functions are important to write, maintain, and read programs. The syntax is similar in Python and JavaScript.

Using Python, we write the keyword def followed by the name of the function, and within parentheses the parameters list. After this, we write a colon (:) and the body of the function.

Functions example code

💡 Tip: In Python, a function can only take a certain number of arguments. If you call a function with a greater number of arguments than it expects, an exception will occur.

For Example:

>>> def foo(x, y):

print(x, y)

>>> foo(2, 4, 7)

Traceback (most recent call last):

File “<pyshell#3>”, line 1, in <module>

foo(2, 4, 7)

TypeError: foo() takes 2 positional arguments but 3 were given

Object-oriented programming in Python

Like javascript, python uses a programming pattern called object-oriented programming, which models concepts using classes and objects. This is a flexible, powerful paradigm where classes represent and define concepts.

Python has support for OOP with classes and classical inheritance, unlike JavaScript which has prototypes with prototypal inheritance.

let’s see how you can create and use the main elements of this programming paradigm.

Creating a class

To create a class we use Class keyword both in python and JavaScript. The only difference is that:

· In Python, after the name of the class, we write a colon (:)

· In JavaScript, we surround the content of the class with curly braces ({})

Class Myclass:

X = 5

Print(Myclass)

Output:

<class’ — -main — -Myclass’>

💡 Tip: In Python and JavaScript, class names should start with an uppercase letter

Class syntax

__init__Function ()

· __init__ is one of the reserved methods in Python. it’s known as a constructor. This method can be called when an object is created from the class, and access is required to initialize the attributes of the class

· In JavaScript, the constructor method is called constructor and it has a parameters list as well

💡 Tip: In Python, we use the Self keyword to refer to the instance while in JavaScript we use this keyword.

Object Methods in Python

In Python, we define methods with the def keyword followed by their name and the parameters list within parentheses. This parameters list starts with the self parameter to refer to the instance that is calling the method. At the end, we write a colon (:) and the body of the method indented.

Example: Method in a Python Class

Class person:

Def — -init — -(self, name, age):

Self.name = name

Self.age = age

Def myfunc(self)

Print(“Hello my name is “ + self.name)

P1 = person(“john”,36)

P1.myfunc()

Output:

Hello my name is john

💡 Note: In JavaScript, methods are defined by writing their name followed by the parameters list and curly braces.

Class Instances

To create instances of a class:

· In Python, we write the name of the class and pass the arguments within parentheses.

my_circle = Circle(5, “Red”)

· In JavaScript, we need to add the new keyword before the name of the class.

my_circle = new Circle(5, “Red”);

Resources

There is a lot more to Python than what’s in this article. I highly recommend you check out the Python docs for tutorials and details about other language features.

And remember, the best way to learn a language is to write it, a lot. And start practicing building some projects, So get to coding!

Resources are used to write this article include www.freecodecamp.org, www.python.org, and https://javascript.info/

Conclusion

Learning python and being a JavaScript developer is full of fun! Now get yourself ready and build exciting projects using python.

Thanks for reading and stay tuned to this blog! I hope you liked this article and found it interesting!

--

--

Rana Ibrar

Computer scientist and study abroad (USA) mentor, helping people to achieve their goals