JS vs Python - Variables
Muhamad Amir Azmi
Posted on June 5, 2021
I have been doing web development for a year now and my main language is Javascript. But recently I got a new job offers that plays around Python and Django for their back end. So, I write this article as a note for me to remember the basics of Python and some comparison with my main language, Javascript.
Variables
Python has very simple way to define a variable compared to JS. In Javascript, to define a variable, a keyword need to include in front of variable name to make sure a variable is changeable or not.
Primitive Data Type
The primitive data type for both language pretty much similar:
- String
- Int
- Float
- Boolean
Javascript
// Variables
// const --> cannot change the value of a variable
const car = 'honda'; // string
const number = 20; // number/integer
const price = 20.89; // float
const isValid = true; // boolean
// let --> can change and update later
let car2 = 'toyota'; // string
let number2 = 43; // number/integer
let price2 = 23.89; // float
let isValid2 = false; // boolean
// return an error because cannot reassign
// to const variable
car = 'bmw'
// does not return an error because let
// can be reassigned
price2 = '18.38
Python
# Variables
# python does not have assignment keyword
# and variables can be reassigned
van = 'volkswagen'; # string
age = 5; # int
mileage = 9823.08; # float
is_good = True; # bool
# (the bool in Python must be capitalize, True/False)
# does not return an error because
# all primitive data type variable can be reassigned
van = 'car'
Multiple Assignment
Python and Javascript can do the multiple assignments but they have different approach. The multiple assignments very useful for one line assignment. The code might seems more clean. Usually I'll do multiple assignments for global or top line in the file.
Javascript
For Javascript, the variable name and value must be couple together and each variable separated with ', '.
// Multiple assignment
// const --> cannot change after initial assignment
const van = 'toyota', year = 2021, kilometers = 238.38, isBroken = true
// let --> can change after initial assignment
let car = 'honda', seats = 4, price = 2384.23, isNew = false
// Reassignment for let variable
seats = 8 // not throw an error
Python
For python, all variables name are group together on the left side and the variables value are group on the right side.
# Multiple assignment
car, seats, mileage, is_new = ('bmw', 4, 3843.49, True)
# The line above can be presented as below:
car = 'bmw'
seats = 4
mileage = 3843.39
is_new = True
Type Conversion or Casting
Both language has their own way to convert primitive data type from one to another. Typically type conversion used to convert int/number to string, string to number/int or float and float to string.
To convert the string to int/number, the string must be a string of number first.
Javascript
// Type Conversions
let seats = '4' // string of number
let cc = '2.5' // string of float number
let price = 28.7 // float number
// return 4 in number/int data type
parseInt(seats)
+seats
// return 2.5 in float
parseFloat(cc)
+cc
// The '+' symbol will convert the
// string into number or float based on the string value.
// return '28.7' in string
price.toString()
// return '28.700' in string with 3 decimal places
price.toFixed(3)
// The toFixed(digits) method can be used
// with the float data type only.
// So, if the value is in string data type,
// convert to parseFloat first and
// then using toFixed() to add decimal places.
Python
As for python, the term for type conversion is casting. Here are some example for casting in Python.
# Casting
seats = '4' # string of number
cc = '2.5' # string of float number
price = 28.7 # float number
# return int 4
int(seats)
# return float 2.5
float(cc)
# return string '28.7'
str(price)
Check Variable Type
Checking the type of variable quite crucial when doing the conditionals. Some times, we want to execute certain code if the variable obtained from the input is in certain type.
Javascript
// Variable Type Checking
let shoe = 'vans'
console.log(typeof shoe) // return type string
Python
# Variable Type Checking
let seats = 4
print(type(seats)) # return int
Conclusion
These are some of examples and differences for variables in Python and Javascript. I know there have so many things related to basic variables for Python and Javascript, but here what I have been using and learnt so far.
In future, I'll regularly update the differences between both language so that, I understand more on both language and level up my skills even further.
Thank you for reading my article, this is my first article and I really appreciate if this article helps.
Posted on June 5, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.