DEV Community

Cover image for A basic comparison between Python and Javascript - part 1
Marcos Nolasco
Marcos Nolasco

Posted on

A basic comparison between Python and Javascript - part 1

Hi there! If you know or study Python and/or Javascript, this guide could help you! It could at least reinforce a little your understanding of their syntax.
Python and Javascript are relativily new languages (not that much), and they're are 2 of the most used programming languages in the world. Above I'll shortly compare their:

  • Declaration of variables and functions
  • Data Types

Declarations

To work with static or dynamic data, we must know how to properly allocate and change those values.

Variables

Python

Python has no command for declaring a variable. It is created when some value is assigned to it, thus it is direct and simple. Cause there is no command for a variable declaration, there is no problem in reassigning them to another value or other type of value.

name = 'Marcos'
age = 28
age = "twenty eight"
Enter fullscreen mode Exit fullscreen mode

Javascript

There are diferents ways to declare a variable in Javascript, according to its purpose. The 3 must common ways are using the keywords var (older, mainly used until 2015, accepts value reassignment), let (used to declare mutable data), or const (used to declare imutable data). All variables must also be declared with a name, called identifier.

var race = 'human'
const hobby = "Gaming"
let rank = 10
Enter fullscreen mode Exit fullscreen mode

Functions

To do a particular task that may be repetitive, we use a block of code called function. It does nothing until it is 'called' (invoked, or used) somewhere else. It can be declared with or without arguments. Arguments are data that we can pass into a function.

Python

To declare a function with Python we must use the def keyword, and all lines of interest for its scope must be indented.

def simpler_function():
  simple_data = "anything you want"
  return simple_data + "is on the other side of fear"

simpler_function()

def dynamic_function(any_argument):
  return any_argument + "is on the other side of fear"

dynamic_function("anything you want")
Enter fullscreen mode Exit fullscreen mode

Javascript

A JavaScript function is defined with the function keyword. Its scope is placed inside curly brackets.

function getResult() {
  result = 42
  return result
}

getResult()

function getAnswer(problem) {
  return 42
}

getAnswer('Universe and everything else')
Enter fullscreen mode Exit fullscreen mode

Data Types

Programming is all about data dealing, so it's important to understand which kind of data each language works with, and how they do it.

Numbers

Python

Basic numbers in Python are of 2 types: integers (0, negative and positive whole numbers without a fractional part), and floats (negative and positive numbers with a frational part denoted by the deciman symbol .)

integer_value = 8
float_value = 8.0
Enter fullscreen mode Exit fullscreen mode

Javascript

Javascript has only one type of number. They can be written with or without decimals.

let integer_number = 3
let irrational_number = 3.14
Enter fullscreen mode Exit fullscreen mode

String (texts)

Python

Any kind of data between single or double quotation marks.

passion = "I love to discover new things."
Enter fullscreen mode Exit fullscreen mode

Javascript

Exactly the same as Python strings

const desire = "I wanna travel all around the world!"
Enter fullscreen mode Exit fullscreen mode

Boolean

Python

Only have two values, written with the first letter capitalized: True or False.

Javascript

Also only the same two values, but written all in Small Case letters: true or false.

Containers

Python

The 3 most used types of data containers in Python are tuples, lists, and dictionaries.

Tuple

An immutable sequence of ordered data. After it is declared, cannot be increased or decreased, and neither its items can be changed. In its declaration, it is used a sequence of data between round brackets.

imutable_data = ("any", 'kind of data', True, 8)
Enter fullscreen mode Exit fullscreen mode
List

Created using square brackets, it is mainly used to store ordered multiple items in a single variable. Lists are mutable, as we can modify their items (using their index), add new items (using append keyword), and delete any of them (using remove keyword).

fruits = ["apple", 'orange']
fruits.append("grape")
fruits.remove('apple')
Enter fullscreen mode Exit fullscreen mode
Dictionary

Dictionaries are used to store data values in key:value pairs. It's mutable, but do not allow duplicated keys. It is declared within curly brackets, and a key is separated from its value by the comma symbol (:). The values in dictionary items can be of any data type.

pets = {
  "cats": 2,
  "dogs": False,
  "names": ["Mira", "Andromeda"]
}
Enter fullscreen mode Exit fullscreen mode

Javascript

There are 2 basic data containers in Javascript: Arrays and Objects

Arrays

Its main structure is exactly the same as Python's List: a special variable, which can hold multiple values of different types, ordered between square brackets. To add new items, we use the method push(), and to remove we use pop() method.

const biggestWishes = ["freedom", "happiness", "love", "imortality"]
biggestWishes.push("wealthy")
biggestWishes.pop("imortality")
Enter fullscreen mode Exit fullscreen mode
Objects

They are written with curly brackets, to contain multiple key:value pairs, separated by commas.
Don't allow duplicated keys. The values for each key can be of any data.

``` const comparison = {
languages = ['Python', 'Javascript' ],
level = 'initial',
topics = 2
}




## Final Considerations
There are many other topics, so I intended to write others to also basic and short comments about them. I have no interest to provide solid knowledge with this article; for that, I suggest Google it, there are bazillions of complete and comprehensive guides for each language. Go for it!
But if anyone could suggest any modification or wanna ask questions, please, reply, I'll take a look ASAP. (=
Enter fullscreen mode Exit fullscreen mode

Top comments (0)