Getting familiar with Python as a JavaScript programmer
Hello JavaScript programmers let’s become a python programmer within the next 30 minute (Not really) :)
1. First Program in Python :
print("Hello World")
As in JavaScript we use console.log() function to print anything on terminal/console in python we have to use print() function .
2. Variable in Python:
name = "shadin"
print(name)
like javaScript we don’t need to write data type to declare a variable , but unlike javaScript there is no special keyword to define a variable , just define a name , that’s it…
3. Concatenation in Python:
firtsName = "Solaiman"
lastName = "Shadin"
fullName = firstName + lastNameprint(fullName) //solaiman Shadin
like javascript python also is + to concat string
4. String Formatting :
okay if you are familiar with C ,you will find some familiarity ,Python uses C-style string formatting to create new, formatted strings. The “%” operator is used to format a set of variables enclosed in a “tuple” (a fixed size list), together with a format string, which contains normal text together with “argument specifiers”, special symbols like “%s” and “%d”.
firtsName = "Solaiman"
lastName = "Shadin"print("my name is %s %s" , % (firstName, firstName) )
5 . List in Python :
Lists are very similar to arrays of javaScript
names = ['shadin', 'mazlan', 'mamun']
print(names[0])
6 . Conditional Statement in Python :
if age => 18:
print("You can vote")
else:
print("Sorry, you can't)
in python in any kind of code block, python used indentation tab/4space instead of curry braces .
Bonus : one thing you have to keep in mind in python , lowercase boolean (true/false) is not valid , in python boolean will be capitalized (True/False)
7. for loop in ython :
names = ['shadin', 'sahin', 'mazlan', 'mamun']
for name in names
print(name)
As you used for in loop of javascript to traverse an array , here in python there is also a for in loop available, so we can use it to traverse a python list .
8. While loop in python:
count = 0while count<5:
print(count)
count += 1
Note : we can’t use increment/decrement operator in python insend we used addition and assignment operation .
9. Define function in Python :
def my_function():
print("Hello From My Function!")
As you already familiar with Python’s block syntax , just you have to remember your ES5 regular function and just instead of function keyword replace by def keyword that’s it you are good to go …
10 . Dictionaries in Python :
Okay you can match your most familiar JavaScript object to Python Dictionary . It’s key value pair like JS object :
person = {
firstName : "Solaiman",
lastName : "Shadin",
profession : "Web developer"
}print(person[firstName])
You can get property value by square bucket notation