DEV Community

Cover image for Key differences between python and javascript
Joshua Wainaina
Joshua Wainaina

Posted on

Key differences between python and javascript

Key differences between python and JavaScript

Introduction

Python and JavaScript are important languages that are used in web development. Python is a language that can be used in the backend development of a web application. JavaScript on the other hand is a language that can be used in both the frontend and backend development of a web application.

However, despite the difference in development part that each language plays, the two languages have other big differences that we are going to look into detail in this article.

Real World application of Python and JavaScript.

Python is mostly used in scientific and specialized applications as it is also used in web development.

JavaScript is widely used in web development.

Syntax, Sentactical and Functional differences between Python and JavaScript

Python and Js have different Syntax.
Python and JavaScript have some key differences in terms of the syntax used in performing various activities.

Code blocks in python and JavaScript

In Python, when several lines of code are indented, then they are said to be of the same code block.

Example

if  t  > 10:

     print (t)
Enter fullscreen mode Exit fullscreen mode

In JavaScript, notwithstanding, wavy supports are utilized rather than space to bunch explanations that have a place with a similar code block.

Let's look at an example.

if (t>10)

{

    console.log(t)

}
Enter fullscreen mode Exit fullscreen mode

Definition of variables

When defining a variable in python, a variable name is written followed by an equal (=) sign. Then a value is assigned to the variable. i.e

<variable_name > = value

x = 56
Enter fullscreen mode Exit fullscreen mode

In JavaScript, a keyword var is added before the variable name and a semicolon is used to terminate the statement i.e

var <variable_name>  = value;

eg  var k=34;

Enter fullscreen mode Exit fullscreen mode

Variable Naming Conventions

In Python, the snake_case naming style is used. The names should be in lowercase and separated by an underscore as shown below.

first_name
Enter fullscreen mode Exit fullscreen mode

In JavaScript, lowerCamelCase is used. The variable name starts with a lowercase letter and every other new word starts with an uppercase letter.

myFirstName
Enter fullscreen mode Exit fullscreen mode

Constants in Python and JavaScript.

Constants

In Python, constants are written in uppercase format separated by an underscore. ie. CONSTANT_NAME.

For example:

PASS_RATE = 4
Enter fullscreen mode Exit fullscreen mode

In JavaScript, a keyword const is added before the constant name and a semicolon is used to terminate the statement. ie. const CONSTANT_NAME = value;

const AGE=56;
Enter fullscreen mode Exit fullscreen mode

Data types and values in Python and JavaScript

1) Numeric data types

In python, we have three numeric sorts that will assist us with accomplishing exact estimations for logical purposes. These are :

  1. int.
  2. float.
  3. complex.

In JavaScript, we have two numeric types i.e Numbersand BigInt. The two whole numbers and coasting point numbers are viewed as type numbers.

  1. numbers
  2. bigint

None and null in python and JavaScript

In python, when a variable doesn't have a worth at a specific point in the program, it is alloted an extraordinary worth called None.

In JavaScript, the same worth is null which addresses the shortfall of any item esteem.

The undefined value

In python, one needs to allocate an underlying worth to the variable. One cannot declare a variable without an initial value.

In Javascript, we have a unique worth that is allocated consequently. When a variable is declared without assigning an initial value, it prints out undefined as shown below;

var k; // should print undefined
Enter fullscreen mode Exit fullscreen mode

Primitive data types

Python have four primitive data types:

  1. Intergers.
  2. float.
  3. boolean (https://www.codesansar.com/python-programming/fundamental-or-primitive-data-types.htm#bool).
  4. strings.

On the other hand, JavaScript have six primitative data types:

  1. undefined.
  2. strings.
  3. Number.
  4. BigInt.
  5. Boolean.
  6. symbol.

Comments

Commenting is done for any reason. For example, one may comment on a line for future reference. <!--A commented line is not executed as part of the code. -->

1. Single-line comments

Comment spanning a single line. Look at the example below:
In python, a hashtag (#) is used to comment on a single line.

# this is a  single-line comment in python
Enter fullscreen mode Exit fullscreen mode

As shown above we have used a hashtag(#) to comment on a single line in Python.

In JavaScript double slashes (//) are used to comment on a single line.

//this is a single-line comment in JavaScript
Enter fullscreen mode Exit fullscreen mode

As shown above we have used double slashes (//) to comment on a single line in JavaScript.

2. Multiple line comments

These are comments spanning many lines.

When writing multiple comments in python, we start every single line with a hashtag (#) as shown below:

eg

# this is a   
# multiple lines 
# comment
# as used in Python`
Enter fullscreen mode Exit fullscreen mode

When writing multiple line comments in JavaScript, we use symbols (/*) to open and (*/) to close a comment as shown below:

/*
 This is  a multi-line comment
that span many lines
*/ 
Enter fullscreen mode Exit fullscreen mode

Built in data structures

1. Tuples

Tuples in Python are similar to lists but immutable.
They are utilized to store information that ought not be changed

In JavaScript, we don't have an inherent construction with such qualities.

2. List and Arrays-->

Records are utilized in python to store a progression of qualities in a similar information structure

number = [3, 4,5]
Enter fullscreen mode Exit fullscreen mode

An array is the equivalent version of this in JavaScript.

var number = [3,4,5]
Enter fullscreen mode Exit fullscreen mode

Comparing values and types

In Python the double equal (==) operator are used to compare if two values and their data types are equal -->

11 == 11 #True
11 == 10 #False
Enter fullscreen mode Exit fullscreen mode


javascript
As shown above, 11 == 11 is true, while11 != 10 is false.

In JavaScript, a triple equivalent ('===') administrator is utilized to look at if two qualities and their information types are equivalent.

5===5  //true
5==='5'  //false
Enter fullscreen mode Exit fullscreen mode

Logical Operators

In python, there are three logical operators:

  1. and
  2. or
  3. not

In JavaScript, the logical operators are also three:

  1. && -- logical and.
  2. || -- logical or.
  3. ! --logical not. ### Type Operators To check the type of object in python we use the (type()) function as shown below:
type(instance)
Enter fullscreen mode Exit fullscreen mode

To check the type of object in JavaScript we use type of operator as below:

type of instance
Enter fullscreen mode Exit fullscreen mode

Inputs and Outputs

Input is a function that is used to ask the user of the program a question whereby the user have to type the result of the asked question.

Output on the other hand is used to print the specific message that the user inputs.

Inputs

In python input() function is used to ask for user input.

Example

name = input (" Enter your name : ")
Enter fullscreen mode Exit fullscreen mode

In JavaScript, (if you are running your code on console ) it will display a small prompt with window. prompt (message) and assign the result to a variable.

var input =window.prompt ("enter a Number :")
Enter fullscreen mode Exit fullscreen mode

Outputs

In python we print a worth in the comfort with the print() work parsing the qualities inside the enclosure.

Example

num1 = input ("Enter the first  number : ")
num2 = input ("Enter a second  number : ")
num3 = input ("Enter a third number : ")
result= num1 + num2 + num3
print(result)
Enter fullscreen mode Exit fullscreen mode

In JavaScript, we print a value in the console with the print() function parsing the values within the parenthesis.

Example

console.log("My name is John:");
Enter fullscreen mode Exit fullscreen mode

Conditional statements

Conditional statements are used to act if a certain condition is true.

1. if statement

In python, indentation is relied upon to indicate the lines of code that belong to a conditional statement.

if condition:

     code
Enter fullscreen mode Exit fullscreen mode

Let's look at an example:

age = 10
if age < 18:
    print('kid you are!')
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the condition is enclosed in parenthesis and the code enclosed within curly braces which should also be indented as shown in the syntax below:

if (condition)

    {
        code
    }
Enter fullscreen mode Exit fullscreen mode

An example of a snippet would look like:

if (hour > 4) {
  task = "Clean the compound";
}

Enter fullscreen mode Exit fullscreen mode

if else statement

In python, a colon (:) is written after the else keyword.

if condition:

    if code

else :

    else code
Enter fullscreen mode Exit fullscreen mode

Let's look at an example:

age = 34
if age < 18:
    print('kid you are!')
else:
    print('You are not a kid')
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the code that belongs to the elseclause is enclosed in curly braces.

if (condition)

    {
        if code
    }

else 

    {
        else code
    }
Enter fullscreen mode Exit fullscreen mode

Let's look at an example.

var time = new Date().getHours();
if (time < 5) {
  task = "Cleaning the house";
} else {
  task = "Feeding the sheep";
}
Enter fullscreen mode Exit fullscreen mode

Multiple conditions

In python elif keyword is used when dealing with multiple conditions. After every condition we write a semicolon(:) and the code that belongs to the condition is indented in the next line.


if condition1:

    code

elif condition2:

    code

elif condition3:

    code

else:

    code
Enter fullscreen mode Exit fullscreen mode

Let's look at an example.

age = 40
if age < 18:
    print('kid you are!')
elif age >=18 and age < 29:
    print('You are a youngst!')
elif age >=29 and age < 35:
    print('You are middle aged!')
else:
    print('You are above middle age !')
Enter fullscreen mode Exit fullscreen mode

In JavaScript else if keyword is used when dealing with multiple conditions. The conditions are surrounded by parenthesis.

if(condition1)

    {
        //code
    }

else if(condition2)

    {
       // code
    }

else if (condition3)

    {
        //code
    }

else

    {
        //code
    }
Enter fullscreen mode Exit fullscreen mode

Let's look at an example.

var time = new Date().getHours();
if (time < 5) {
  task = "Cleaning the house";
} else if (time < 15) {
  task = "Feeding the sheep";
} else {
  task = "Watching a movie";
}
Enter fullscreen mode Exit fullscreen mode

Switch

Python does not have this type of built-in control structure i.e switch

In JavaScript, a switch is utilized to pick what happens depends on the worth of an articulation.

switch(expression)

    {
        case 1:
        code
        break;

        case 2:
        code
        break;

        case 3:
        code
        break;

        default:
        code
    }
Enter fullscreen mode Exit fullscreen mode

Let's look at an example.

switch (new Month().getMonth()) {
  case 1:
    month = "January";
    break;
  case 2:
    day = "April";
    break;
  case 3:
     day = "May";
    break;
  case 4:
    day = "June";
    break;
  case 5:
    day = "August";
    break;
  case 6:
    day = "September";
    break;
  default:
  No such a month;
}    
Enter fullscreen mode Exit fullscreen mode

Loops

A loop is a control structure that repeats a series of instructions until a specified condition is reached.

For Loops

We write a for loop in Python as shown below.

for x in range(k):

    code
Enter fullscreen mode Exit fullscreen mode

The syntax of writing a for loop in JavaScript is as follows.

for(var x = 1; x >n ; x++)

{

        code
}
Enter fullscreen mode Exit fullscreen mode

While Loop

It is a pre-test loop in which the condition is evaluated before the loop executes.

In Python, we compose the watchword while trailed by the condition, and afterward, a colon (:), and afterward the body of the circle in another line in an indented mode.


while condition: code 

Enter fullscreen mode Exit fullscreen mode

In JavaScript, the linguistic structure is the same. The distinctions are that we need to encompass the condition with enclosures and the body of the circle with wavy supports.

while(condition)

{

    code

}
Enter fullscreen mode Exit fullscreen mode

Do while loops

This is a post-test loop. The condition is evaluated after the loop executes

In python, we do not have such a control structure (do-while loop)

In JavaScript, this loop will always be executed
once.


do { code } while (condition); 

Enter fullscreen mode Exit fullscreen mode

Functions

A Function is a block of code that plays out a specific undertaking. In Python, we utilize a watchword def which is trailed by the name of the function. Then the parameters are enclosed in parenthesis and then a full colon immediately after the parameters are enclosed in parenthesis, after which an indented code is written.

 def function_name ( x1 , x2 , x3 , ...):
      code 
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the lone contrasts are that we characterize a capacity utilizing the 'work catchphrase 'and encompass the body of the capacity with 'wavy supports'.

function function_name (x1,x2,x3,...)

{

    code
}
Enter fullscreen mode Exit fullscreen mode

Object Oriented Programming(OOP)

OOP is the development of programs in terms of objects that interacts with each other.

Both Python and JavaScript do uphold Object-Oriented programming.

The syntax of defining a class in both Python and JavaScript is almost the same there is a slight difference.

In python a colon is written after the keyword class whereas curlybraces are used in Javascript.

Python

class Rectangle:

    code
Enter fullscreen mode Exit fullscreen mode

JavaScript

class Rectangle

{

    code
}
Enter fullscreen mode Exit fullscreen mode

Constructor and Attributes

A constructor is a member function of a class that executes automatically whenever an object is created.

In Python, a constructor that instates the new example is known as init with two driving and following highlights. This strategy is called consequently when an example of the class is made to introduce it's ascribes. Its boundaries list characterizes the qualities that we need to pass to make the example. This rundown begins with self as the principal boundary.

 class Rectangle:

        def _init _(self , length , width):
            self.length= length
            self.width=width 

Enter fullscreen mode Exit fullscreen mode

In JavaScript, a constructor strategy is called constructor and it has a rundown of boundaries as python has.

class Rectangle

{

    constructor(length,width){
        this.length=length;
        this.width=width;

    }
}
Enter fullscreen mode Exit fullscreen mode

NB

In python, we use self to refer to an instance of a class.
i.e


self.attribute= esteem 
Enter fullscreen mode Exit fullscreen mode

While in JavaScript we use this to allude to an occurrence of a class.
i.e


this.attribute=value; 

Enter fullscreen mode Exit fullscreen mode

Methods In Python and JavaScript.

We characterize strategies with the 'def watchword' trailed by the name of the technique and the boundaries list inside brackets. This boundaries list begins with the self boundary to allude to the occurrence that is calling the technique. After this rundown, we compose a colon (:) and afterward on another line the body of the technique indented.

 class Rectangle:

        def _init _(self , length , width):
            self.length= length
            self.width=width

        def calc_area(self):
             return self.length*width
Enter fullscreen mode Exit fullscreen mode

In javascript the syntax is as shown below.

class Rectangle

{

    constructor(length,width)
    {
        this.length=length;
        this.width=width;
    }

    calcArea()
    {
        return this.length*width;


    }

}
Enter fullscreen mode Exit fullscreen mode

Learning curve

Although these two languages, Python and Javascript, are have got the differences listed above but
In terms of learning and implementing, Python is much easier than JavaScript because its syntax is much easier to understand and also implementing.

Happy Coding

Top comments (1)

Collapse
 
joshua_wainaina_e8c1ad2f0 profile image
Joshua Wainaina

Perfect Insight