From JavaScript to Python – Studying a New Language Pt.1




Have you ever ever wished to be taught new programming language? What about Python? This can be a language that’s straightforward to be taught and common. You need to use it virtually in all places. It’s also very fashionable language in machine studying. Will you give it an opportunity? Nice! This mini collection will enable you to be taught all you want.

From JavaScript to Python Part 2.

From JavaScript to Python Part 3.

A couple of questions

Let’s begin by answering the query chances are you’ll be asking proper now. Why ought to I be taught Python? Effectively, you is perhaps asking a few questions. For instance, ought to I be taught one other programming language? There’s one particular factor you must think about earlier than you try and be taught a brand new programming language. Let’s assume that you just already know at the very least one programming language, a JavaScript for instance.

Studying a number of languages directly?

How proficient are you in you within the programming language you already know? Take into consideration a easy scale from one to 5. One is a whole newbie, three is superior and 5 is grasp. How would you consider your expertise on this scale? If you happen to assume are someplace between one and three, it won’t be the proper time to be taught a brand new language.

If you find yourself a newbie to programming making an attempt to be taught a programming language, it’s higher to stay to that language till you attain superior stage. On this case, you can simply get right into a scenario the place information out of your first language will begin to combine with materials from the second language you are attempting to be taught. The result’s usually confusion, slower studying price and drawback to make use of any of the languages you are attempting to be taught.

I do know this as a result of I attempted it. It value me a whole lot of time. Don’t do the identical mistake. An exception is once you surpass this stage in at the very least one language. In case you are already proficient in at the very least one programming language your present information is extra resilient. That means, it is not going to combine along with new materials. What’s extra, your present information may even enable you to be taught new language.

If that is true, then, sure, go forward and attempt to be taught two or much more programming languages concurrently. In any other case, work in your first language and add Python in your to do listing. Simply keep in mind that you’re not losing your time by focusing in your first language. As I discussed, a stable information of 1 language will enable you to be taught one other one. So, have endurance.

Ought to I be taught one other programming language?

I feel that studying one other language is an efficient factor. When you understand just one programming language you might be restricted by the constraints of that language. Each programming language has particular advantages, finest practices, paradigms and quirks. When you understand just one language you might be pondering in a particular manner, a manner that’s distinctive to that language.

This isn’t unhealthy as a result of it helps you utilize that programming language higher and clear up issues extra effectively. Nonetheless, it nonetheless limits your creativity. Who says that this or that manner is the easiest way to resolve this explicit drawback? When you understand multiple programming language, you may take a look at the issue from completely different perspective. This, in flip, can assist you discover a higher answer.

When you consider it from this angle, studying one other language can assist you discover new prospects. It will probably introduce you to a complete new world and present you ways a lot there may be nonetheless to find. It’s like when Alice determined to take the chance and enter the rabbit gap. You are able to do the identical. Enter the rabbit gap and be taught a brand new programming language, corresponding to Python.

And, in case you are afraid that a number of programming will create a multitude in your head, don’t be. Programming languages are similar to spoken languages. While you be taught one language rather well, you may add one other after which easily change between them with out experiencing any issues. The hot button is to be taught the primary programming language very nicely earlier than you try and be taught second.

Why Python?

Now, let’s reply the unique query. Why must you be taught particularly Python? There are at the very least 4 causes for selecting Python as one of many programming languages to be taught. First, it’s beginner-friendly. Python may be very straightforward to be taught even for folks with no prior expertise in programming. For this reason Python is usually beneficial as the primary language to be taught.

So, should you already know one programming language, or much more, studying Python needs to be very straightforward for you. The second motive is that Python is one in all these languages in a position to maintain progress and recognition over a few years. Python appeared in 1990. Not solely it’s not dying, fairly the alternative. Based on Tiobe index it’s frequently one of the fashionable programming languages.

In different phrases, Python is nothing new underneath the solar. It’s a language with a really lengthy historical past and a big quantity programmers loves it and makes use of it. It’s protected to say that you just don’t have to fret it is going to out of the blue disappear any time quickly. The third motive is that it’s a good language for web development. Python is usually a most popular alternative for back-end programming.

That is because of its simplicity, recognition, scalability, quantity of sources which can be obtainable and variety of good frameworks corresponding to django. Lastly, the fourth motive. Python is likely one of the hottest languages for machine learning and knowledge science. This makes it even more sensible choice in case you are interested in any of those matters. It’s also one more reason why Python is right here to remain.

Studying new programming language at all times takes a while, even when it is rather straightforward one. And, as we mentioned, it’s higher to make use of that point intentionally and focus solely on that one language, as a substitute of making an attempt to deal with extra directly. So, take your time, take into consideration these 4 causes and resolve for your self. In case you are keen to enter this rabbit gap, let’s get our arms on Python.

From JavaScript to Python: Feedback, variables, capabilities, loops

I’ll assume you already know at the very least one programming language, JavaScript. With that, let’s make the beginning simpler by discussing the fundamentals of Python whereas evaluating it to JavaScript. Earlier than we begin, ensure you have Python put in in your laptop. If not, head on Python website and obtain the model you need and that’s proper in your surroundings. If you find yourself performed with that, and putting in it, we will proceed.

Feedback

Let’s begin with one thing quite simple and fairly helpful, particularly if you wish to write clean code. How can we write feedback in Python? In JavaScript, we have now two choices. We are able to use a single-line remark // or multi-line remark /**/. In Python, there is only one alternative. After we need to write a remark, or remark out some code, the remark has to begin with hash (#).

// This can be a single-line remark in JavaScript.

/*
  This can be a multi-line
  remark in JavaScript.
*/

# This can be a ... remark in Python.

Aspect be aware: There are folks saying that Python really has multi-line feedback. They’re unsuitable. What they’re speaking about is utilizing """. Sadly, there may be actually no such a factor as multi-line remark in Python. That is solely a small “workaround” utilizing multi-line strings as feedback. This workaround takes the benefit of rubbish assortment.

These triple quotes are handled as common strings. The exception is that they will span a number of strains. And, since these strings should not assigned to a variable they are going to be instantly rubbish collected when the code executes. Put merely, they are going to be eliminated. Nonetheless, they aren’t ignored by the interpreter in the identical manner actual feedback (#) are.

"""
That is NOT an actual
multi-line remark
in Python.
"""

def mythBuster():
  """
  And neither is that this
  an actual multi-line remark
  in Python.
  """

# That is the one actual remark.

Variables

The second best factor are variables. In JavaScript, we will work with three “varieties” of variables. Or, we will declare variables in 3 ways. These are var, let and const. The distinction is in scope and mutability. In Python, that is simpler. There is just one manner or “kind” to declare a variable. This additionally means two issues. First, in Python you may change any variable you declared.

The second factor is that there’s only one scope and that’s block. Each variable you declare exists contained in the block the place you declare it. It’s, due to this fact, accessible solely inside that block. If you happen to attempt to entry some variable declared inside a operate for instance from the surface, the consequence can be an error.

// JavaScript instance:
var x = 'String'
let y = 'This can be solely short-term.'
const z = 'Attempt to change me.'

# Python instance:
x = 'String'
y = 'This can be solely short-term.'
z = 'Attempt to change me.'

# change x variable
x = 15

# block scope instance
x = 15

def hello():
  a = 'I exist solely inside this operate.'
  b = 'Attempt it by your self'
  print(a)

print(x) # 15
hello() # # I exist solely inside this operate
print(b) # NameError: identify 'b' is just not outlined

Features … or blocks

Subsequent on the road are capabilities. Effectively, they’re really known as blocks in Python. This will make it simpler for some folks to grasp what “block scoped” variable means, since “operate” is “block”. In contrast to in JavaScript, there is no such thing as a such a factor as curly brackets in Python. So, how can we create a block with something inside so it’s interpreted because the content material of that block?

The reply is by utilizing house, or fairly indentation to extra particular. After we create a block with some code we have now to make use of def key phrase and indent the code inside. This additionally means one factor. While you write code in Python, you must watch out about the way you indent it. One further house right here or there’ll end in errors or the code is not going to even execute. So, watch the house originally of strains.

// JavaScript instance 1:
operate printThis() {
  let x = 'One more short-term variable.'

  return x
}

// JavaScript instance 2:
let printThis = () => {
  let x = 'One more short-term variable.'

  return x
}

# Python instance 1:
def printThis():
  x = 'One more variable.'
  print(x)

printThis() # One more variable.

# Python instance 2: unsuitable indentation
def printThis():
  x = 'One more variable.'
    y = # unsuitable indentation
print(x) # unsuitable indentation - places print in a distinct scope, outdoors the block "printThis"

# NameError: identify 'x' is just not outlined
printThis() # Prints nothing as a result of "print(x) assertion is outdoors the block."

A fast recap. Keep in mind these 4 issues. First, there aren’t any curly brackets. Second, capabilities are known as blocks. Third, create a block by utilizing def key phrase adopted by brackets (()), with our with out the parameter, adopted by colon (:). Fourth, indent the code that’s alleged to be contained in the block for one stage. And, make sure that to maintain the indentation constant.

If/else

Subsequent, let’s check out if/else statements. Only a fast aspect be aware. In Python, if/else assertion can be known as “block”. It seems to be similar to if/else you understand from JavaScript, virtually identically. There are solely 4 small variations. First, once more, no curly brackets. Indentation is used to tell apart between the code contained in the block and out of doors it.

Second, there aren’t any brackets (()) across the situation. Third, the situation is adopted by colon (:). Lastly, there is no such thing as a else if. Python makes use of elif. What in regards to the else? That’s the identical, with a really small exception of further colon (:). And, the identical additionally applies to elif.

// JavaScript instance:
if (x > 15) {
  return 'Greater than 15!'
} else if (x > 25) {
  return 'Greater than 25!'
} else {
  return 'You aren't pondering large enough.'
}

# Python instance:
if x > 15:
  print('Greater than 15!')
elif x > 25:
  print('Greater than 25!')
else:
  print('You aren't pondering large enough.')

Whereas

Relating to whereas loops, there may be not a lot to speak about. They appear virtually the identical as their counterparts from JavaScript. There are solely few exceptions we mentioned after we talked about if/else statements, or blocks.

// JavaScript instance 1:
whereas (x > 0) {
  return 'You shouldn't attempt to run this ...'
}

// JavaScript instance 2:
let x = 0
whereas (x < 10) {
  x += 1
  return 'Sure, that is safer.'
}

# Python instance 1:
whereas x > 0:
  print('You shouldn't attempt to run this ...')

# Python instance 2:
x = 0
whereas x < 10:
  print 'Sure, that is safer.'
  x += 1

For

The very last thing we are going to check out right this moment can be for loops. Relating to for loops in Python, they give the impression of being extra just like JavaScript for...in fairly than the great outdated for.

// JavaScript instance:
let listing = [1, 2, 3]

for (let quantity in listing) {
  console.log(quantity)
}

# 1
# 2
# 3

# Python instance 1:
listing = [1, 2, 3]

for quantity in listing:
  print(quantity)

# 1
# 2
# 3

# Python instance 2:
for quantity in vary(0, 7):
  print(quantity)

# 0
# 1
# 2
# 3
# 4
# 5
# 6

Epilogue: From JavaScript to Python Pt.1

Congratulations! You’ve simply completed the primary a part of this mini collection. Right now, we took a take a look at and mentioned absolutely the fundamentals. To start with, we began by speaking in regards to the subject of studying one other programming language and why Python is an excellent alternative. Then, we explored matters corresponding to feedback, variables, capabilities or blocks, if/else and whereas and for loops.

That is just the start, solely the fundamental. Nonetheless, it offers you a superb place to begin. So, take the examples we used right this moment and play with them. Attempt to run them, customise them and run them once more to see what occurs. This may enable you to get the grasp of the syntax and memorize it quicker. One of the best ways to be taught Python is by writing code in Python. Ultimately, the easiest way to be taught something is by doing.

What’s subsequent? Within the second a part of this mini collection we are going to focus on ideas corresponding to varieties, numbers, strings, lists, dictionaries, courses and extra. We’ll once more use examples of each, JavaScript and Python, that can assist you perceive how the code seems to be in Python and what are the variations. Quickly, you may be nearly as good in Python as you might be in JavaScript. With that, I look ahead to seeing you right here once more the following week. Till then, have an excellent day!

If you happen to favored this text, please subscribe so you do not miss any future submit.








If you would like to assist me and this weblog, you may change into a patron, or you should buy me a espresso 🙂







Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.


Checkout extra Articles on Sayed.CYou

#JavaScript #Python #Studying #Language #Pt1