Photo by Hitesh Choudhary on Unsplash

Comparing Python and JavaScript: Data Types

Thomas Garza
3 min readMar 30, 2020

--

As you may have guessed by reading my past stories, I am familiar with writing JavaScript code. As an aspiring software developer, I understand that it is advantageous to be versed in more than one programming language. Another such language is Python, which we will dive into in this article.

Why learn Python though? Python is a good language to learn because, Python is readable (like JavaScript), compatible with major platforms and systems, has a robust standard library, and has many open source frameworks and tools (what other language has that? … uh ...JavaScript).

Okay, what should be learned first? Let’s take a look at how data is stored in different data types for Python. We’ll first create a variable.

Variables

// Variable x in JavaScriptlet x = 2;# Variable x in Pythonx = 2  #no keyword needed, can't start with number, no space or dash

Strings

In both languages, strings are identified by quotes (single or double).

// JavaScriptlet str = 'Hello World!'; // sing or double quotes work# Pythonstr = 'Hello World!' # single or double quotes work

Numbers

In JavaScript, numbers can be integers or floats (have decimals). In Python an integer is a specific number type and a float is another specific number type.

// JavaScriptlet x = 1    // number
let y = 1.2 // number
# Pythonx = 1 # integer
y = 1.2 # float

Boolean

In both languages booleans exist, but in Python the boolean must be capitalized.

// JavaScriptlet bool1 = true;
let bool2 = false;
# Pythonbool1 = True # must be capitalized
bool2 = False # must be capitalized

Arrays (or Lists in Python)

In JavaScript, arrays are ordered list of (can be, but not necessary) various data types housed in brackets ([]). Python also has an ordered list of (can be, but not necessary) various data types houses in brackets ([]), but they are called lists.

// JavaScriptlet arr = [ 1, 'two', true ];# Pythonlist = [ 1, 'two', true ]  

Objects (or Dictionaries in Python)

In JavaScript, objects are un-ordered list of key/value pairs. Python has an un-orderd list of key/value pairs, but in Python they are called dictionaries.

// JavaScriptlet monthObj = { "Jan": 1, "Feb": 2, "Mar": 3 };# Pythonmonth_dict = { "Jan": 1, "Feb": 2, "Mar": 3 }

Functions

In JavaScript, functions are objects that are blocks of code that can be run when invoked. In Python, functions are objects that are blocks of code that can be run when invoked. Both are invoked by calling the function name followed by parentheses (greeting(), see functions below).

// JavaScriptfunction greeting() {
console.log("Hello World!");
}
# Pythondef greeting(): # keyword is def, colon before code block
print("Hello World") # no curly braces, indentation for code block

Conclusion

Superficially looking at these two languages, Python and JavaScript data types have some similar structures. Being able to pickup Python after learning JavaScript seems like a very achievable goal. Please note, the above is not an exhaustive list of all the data types, but just some examples to get started with. Perhaps in the next article, I will look into comparing loops and if-else statements, among other things.

--

--

Thomas Garza

Former Project Lead currently studying Full Stack Development