Python and JavaScript: A comparison of the language basics

In this table I will compare some basic programming syntax and conventions between the Python and JavaScript programming languages.

Programming element Python JavaScript
Creating a variable that contains an int: age = 25 var age = 25;
Creating a variable that contains a float: probability = 0.62 var probability = 0.62;
Creating a variable that contains a string:
Python supports single, double, and triple quotes.
state = 'North Dakota'
state = "North Dakota"
state = """North Dakota"""
var state = "North Dakota";
Denoting string literals:
Python uses an ‘r’ in front of the string.
path = r'\path\to\some\files'
path = r"\path\to\some\files"
path = r"""\path\to\some\files"""
No string literals. Special characters must be escaped.
String Concatenation: greeting = "Hello" + " world"

greeting = str1 + str2

var greeting = "Hello" + " world";

var greeting = str1 + str2;

Creating a list or array: fruit_list = ['apple', 'banana', 'cherry'] var fruit_list = ["apple", "banana", "cherry"];
Getting the length of a list or array: length = len(fruit_list) var length = fruit_list.length;
Python dictionary vs. JavaScript object:
fruit = {'a':'apple', 'b':'banana', 'c':'cherry'} var fruit = { "a": "apple", "b": "banana", "c": "cherry" };
Getting keys from a dictionary or object: fruit_keys = fruit.keys() var fruit_keys = Object.keys(obj);

var fruit_keys = [index for (index in fruit)];

Getting values from a dictionary or object: fruit_values = fruit.values() var fruit_values = [fruit[index] for (index in fruit)];
Adding key-value pairs to a dictionary or object: fruit['o'] = 'orange' fruit["o"] = "orange";

var o = "o";
fruit.o = "orange";

Deleting key-value pairs from a dictionary or object: del fruit['c'] var key = "c";
delete fruit[key];delete fruit["c"];

delete fruit.c;

Python was invented in the late 1980s (1989) by Guido van Rossum. Ideas for Python were adopted from Modula-3 and Lisp.

JavaScript first appeared in web browsers in 1995 and was written by Brendan Eich, who worked for Mosaic Netscape, while creating the Netscape browser code named “Mozilla”. JavaScript was influenced by programming languages such as Self and Scheme, and was originally named LiveScript. It is thought that it was rebranded “JavaScript” as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new programming language “Java”. However, JavaScript and Java are completely distinct programming languages with little in common.

 

Leave a Reply