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

The highway to Python will be simpler than you suppose. Use your data of JavaScript and be taught Python a lot sooner. On this closing half you’ll learn to work with modules and courses. Then, we are going to check out common expression and how you can use it in Python. After that, I offers you three suggestions you employ to be taught any programming language in much less time.

From JavaScript to Python Part 1.

From JavaScript to Python Part 2.

From JavaScript to Python: Modules

One other factor you’ll most likely know from JavaScript, that exist in Python and is used very often, are modules. If not, the idea of modules may be very easy. You break up you code into smaller chunks, or modules, and save these chunks in separate information. You may take into consideration modules as containers. Utilizing modules is often higher than having all of the code in a single place.

It helps you set up your undertaking. Additionally, you don’t want every part on a regular basis. With modules, you possibly can decide particular snippet of code and use it the place you want and whenever you want. And, if you happen to don’t want it, it is not going to bloat your undertaking with unused code. Additionally, there will likely be occasions when you will have to make use of some performance you presently don’t have.

Then, you should use a bundle supervisor. Supervisor created for Python is known as pip. Have you ever ever labored with npm? We will say that pip is a Python various of npm. It does the identical factor. It permits you to obtain public packages created for Python by different programmers and set up them in your laptop. Then, you possibly can import these packages as modules whenever you want. It’s virtually like utilizing npm. Nicely, virtually.

Anyway, there’s one factor that’s completely different in Python. In JavaScript, you must all the time specify what code do you need to export from module. In any other case, you will be unable to entry the code. Once you need some perform or variable to be out there outdoors the file, you must export with export assertion. Solely then you should use import assertion and it’ll work.

In Python, this isn’t crucial. Once you avoid wasting code in a module it’s exported as default. Once you need to load code from some module, you possibly can both import every part or you possibly can import just a few components. You are able to do this utilizing import assertion, identical to in JavaScript.

// JavaScript instance:
# example_module.js
export const greetingText = 'Hi there world!'

export const greeting = perform() {
  return greetingText

# Python instance:
greetingText = 'Hi there world!'

def greeting():

# import every part from '' module
import example_module

greeting() # 'Hi there world!'

print(greetingText) # 'Hi there world!'

# import just a few components from '' module
from example_module import greeting

greeting() # 'Hi there world!'

print(greetingText) # 'Hi there world!'

What if you happen to imported one module in one other, second, module after which imported this, second, module in a special, third, module? Can you employ the code from the primary module? The reply is sure. Although you’re working with the third module you should use the code from the primary module. Do not forget that Python robotically exports all code.

This lets you use code from different modules not directly. The one situation is that one of many modules within the “module chain” comprises import assertion for the module you want. If that is true, that code is accessible from the primary module with that import. What this implies, briefly, is which you can additionally import a module from one other module. Let’s check out a easy instance.

# That is
greetingText = 'Hi there world!'

def greeting():

# That is
# import every part from
import module_one

# That is
# import every part from
import module_two

module_one.greeting() # 'Hi there world!'
print(module_one.greetingText) # 'Hi there world!'

From JavaScript to Python: Lessons

One other idea that will likely be acquainted to you’re courses. In contrast to JavaScript, Python is object-oriented programming language from the very starting. It’s most likely because of this why working with courses may be very straightforward in Python. Identical to in JavaScript, issues corresponding to strategies, cases, inheritance and sophistication and occasion variables all exist in Python as nicely.

Once you need to create a class you must begin with class assertion. This assertion is then adopted by the identify of the category. The identify of the category is adopted by colons, such as you already noticed in case of if assertion and loops. It’s value mentioning once more that indentation issues in Python, quite a bit. In different phrases, indent all of the code you need to be contained within the class.

In Python, each class has one thing known as documentation string. That is non-compulsory. You may entry this string any time utilizing ClassName.__doc__. What follows subsequent are the statements, information attributes and strategies and something you need the category to include. Once you need to entry some content material of the category, you do it utilizing dot notation-ClassName.x.

Few easy ideas value mentioning. Let’s begin with class variables. These are variables you already know from part 1 and in addition from JavaScript. The worth of those courses is accessible for, or shared with, all cases of this class. Apart to class variables, there are additionally occasion variables. These variables are the identical as class variables, however exist at school cases.

Subsequent is a category constructor, __init__, or initialization technique. It is a particular technique Python calls each time whenever you create a brand new occasion of the category. After this constructor, different strategies you need to add to the category appear to be regular features. The one exception is that the primary argument for each technique is self.

The excellent news is that Python provides this self argument to the checklist of arguments for you whenever you name the strategy. Which means you don’t have to recollect that there’s some self whenever you need to use some technique. Once you need to create an occasion of sophistication, you employ the category identify of the category you need and the arguments you outlined in __init__ technique.

Once you need to change any information contained within the class? You once more use dot notation to pick out the precise information you need to change and assign a brand new worth to it.

// JavaScript instance:
class Individual {
  constructor(identify, age) {
    this.identify = identify
    this.age = age

  displayName() {
    console.log('My identify is ' + this.identify)

  displayAge() {
    console.log('My age is ' + this.age)

// Create occasion of Individual
const Adam = new Individual('Adam', 28)

// Entry strategies in Adam, occasion of Individual class
Adam.displayName() // "My identify is Adam"
Adam.displayAge() // "My age is 28"

# Change Adam's age
Adam.age = 20

Adam.displayAge() // "My age is 20"

# Python instance:
class Individual:
  # Documentation string.
  "Base class for all folks"

  # Class variable - shared with all cases of this class.
  # If you wish to entry this variable and its worth you employ "Individual.isAlive"
  isAlive = True

  # It is a class constructor or initialization technique.
  def __init__(self, identify, age):
    self.identify = identify
    self.age = age

  def displayName(self):
    print('My identify is ', self.identify)

  def displayAge(self):
    print('My age is ', self.age)

# Create occasion of Individual
Sofia = Individual('Sofia', 21)

# Entry strategies in Sofia, occasion of Individual class
Sofia.displayName() # "My identify is Sofia"
Sofia.displayAge() # "My age is 21"

# Change Sofia's age - assign new worth to "age" attribute
Sofia.age = 25

Sofia.displayAge() # "My age is 25"

Python and Common Expression

Another topic some folks think about to be difficult is common expression, or regexp. We already explored this topic of working with common expression on this weblog. So, in case you are not accustomed to this subject, check out this two-part mini series-first part and second part. How common expression works in Python?

With a view to use common expression you will have to import re module. Then, you should use the syntax of normal expression to attain what do you want. Let’s check out couple of examples utilizing some fundamental strategies corresponding to match, search and sub (search and substitute). You could find checklist of all out there flags and particular characters in documentation for re module. Or, you should use this cheat sheet (with downloadable PDF).

The match technique checks for a match solely at the start of the string. That is one thing essential. If you wish to use match, it ought to be since you need to take a look at if the string begins with particular character or phrase. When match is discovered, it’ll return match object. In any other case, it’ll return None.

Subsequent is search. This technique is much like match. The distinction between match and search is that match checks for a match solely at the start of the string. The search technique will search by way of the entire string and return match object for a match wherever within the string. No, that isn’t a typo. Each, match and search return match object.

Are you curious and need to know extra about common expression and how you can use it in Python? Check out this complete tutorial on guru99. For regexp and how you can use it in JavaScript, check out Introduction to common expression pt1 and pt2.

// JavaScript instance:
const testText = 'Textual content for testing common expression. It's best to know that common expression can be known as regexp.'

// match()
// Match present phrase common.
const testOne = testText.match(/common/)

// Match non-existing phrase common.
const testThree = testText.match(/Yeti/)

console.log(testOne) // ['regular']
console.log(testThree) // null

// search()
// Seek for present phrase 'Textual content' that's on the start of testText.
const testThree = content/)

// Seek for present phrase 'ought to' that isn't on the start of testText.
const testFour = to/)

console.log(testThree) // 17
console.log(testFour) // -1

// substitute()
// Discover phrase 'Textual content' and substitute it with 'Content material'.
const testFive = testText.substitute(/Textual content/, 'Content material')

console.log(testFive) // Content material for testing common expression. It's best to know that common expression can be known as regexp.

# Python instance:
testText = 'Textual content for testing common expression. It's best to know that common expression can be known as regexp.'

# match()
# Match present phrase 'Textual content' that's on the start of testText.
testOne = re.match('Textual content', testText)

# Match present phrase 'ought to' that isn't on the start of testText.
testTwo = re.match('ought to', testText)

print(testOne) # re.Match object; span=(0, 4), match='Textual content'
print(testTwo) # None

# search()
# Seek for present phrase 'Textual content' that's on the start of testText.
testThree ='Textual content', testText)

# Seek for present phrase 'ought to' that isn't on the start of testText.
testFour ='ought to', testText)

print(testThree) # <re.Match object; span=(0, 4), match='Textual content'>
print(testFour) # <re.Match object; span=(41, 47), match='ought to'>

# sub()
# Discover phrase 'Textual content' and substitute it with 'Content material'.
testFive = re.sup('Textual content', 'Content material', testText)

print(testFive) # Content material for testing common expression. It's best to know that common expression can be known as regexp.

3 Tips about studying new programming languages

Let’s finish this half, and the entire mini collection, on a lighter observe. What follows are some common suggestions that will help you be taught not solely Python, however another language you need to be taught.

Join every part to the dots you already know

Understanding one other language is a giant profit whenever you need to be taught one other one. You may speed up your studying by connecting ideas the brand new language to your first language. That is precisely why we used code instance from each languages, Python and JavaScript, as a substitute of only one. Our purpose was for instance how these ideas appear to be in numerous syntax.

In lots of circumstances, you already know the semantics, or ideas. Because of this, it’s usually not essential to go over the speculation once more. You simply have to know what’s completely different and keep in mind that. It will enable you to be taught new programming in a lot much less time than often. It’s much like working with git. Once you examine two information you don’t have to know the entire code, solely what modified.

For those who don’t perceive it, go deeper

Once you determine to be taught new programming language, by no means skip the components you don’t absolutely perceive. This virtually all the time causes a number of troubles sooner or later. So, if in case you have troubles with understanding one thing, don’t transfer on. As an alternative, do the alternative and go deeper. Learn extra principle, attempt extra tutorials and ask extra questions. Persist with that subject till you perceive it on 100%.

Take into consideration this means of studying as constructing a constructing. No matter you skip now will solely result in cracks within the construction of that constructing. Then, one thing can occur and a type of cracks will trigger the entire constructing to break down. The one technique to keep away from that is by gaining full understanding. Bear in mind, if you happen to don’t perceive one thing, go deeper.

Deal with doing

There’s nothing dangerous on studying the speculation by studying articles and books. Nonetheless, this strategy is much from the best. What you need to do as a substitute is to concentrate on doing. That is one of the best ways to be taught something. Give it some thought. How did you be taught to stroll, swim or trip a motorcycle, or different many expertise? You realized them by doing, making an attempt and failing and making an attempt once more.

Use the identical strategy whenever you need to be taught new language. Choose a language you need to be taught, corresponding to Python, after which seek for the simplest tutorial you possibly can attempt. It’s okay if you happen to don’t perceive one thing, and even if you happen to don’t perceive something in any respect. Your purpose shouldn’t be discovering a tutorial you perceive. Your purpose is to play with the code and observe what occurs.

You may usually be taught extra and sooner through the use of statement alongside together with your frequent sense. And, if you happen to nonetheless can’t determine what is going on? You may search on the web, attain out to somebody on social media or discussion board, seize a ebook or the rest. Bear in mind, this isn’t about studying solely by doing, however about specializing in doing. In case you are not making progress, use any assets out there.

Specializing in doing is my favourite technique to be taught simply something. There are folks saying that the very best factor to begin studying is by beginning slowly, taking small steps and beginning with principle. You don’t need to overwhelm your self. Specializing in doing is a significantly better strategy. You simply bounce proper into the language or subject you need to be taught.

Let’s use swimming or trip a motorcycle for instance once more. You can begin slowly and take small steps. This will imply setting apart jiffy and skim some fundamental principle about how you can swim or trip a motorcycle. The following day, you repeat the method, perhaps add a little bit of follow. After which repeatedly. Will you learn to swim or trip a motorcycle? Very seemingly. The issue is that it’ll take a number of time. Now, think about specializing in doing.

You spend a couple of minutes studying in regards to the fundamental principle. You discover some fast data about what to do. Then, you purchase a motorcycle, or discover pool deep sufficient to swim however not too deep so you’ll drown. Then, you’re taking motion. You get on the bike, or into the pool, and begin making an attempt and failing and making an attempt once more. Very quickly, you will note you make progress.

Which technique will enable you to be taught the specified talent sooner? Specializing in doing is an excellent candidate for a winner. True. This technique usually contains sure stage of discomfort and even ache. Nonetheless, this will not be the draw back. As an alternative, we are able to use it as a motivation to focus extra and be taught sooner. How briskly will you be taught to trip a motorcycle to keep away from falling in your face?

Thankfully, in relation to programming the discomfort is often a lot smaller. And, the one ache you must endure is being “slapped” by errors, or clean display screen if the code doesn’t run in any respect. It is a danger value taking. So, whenever you need to be taught one thing, overlook about studying tons of principle. As an alternative, bounce proper into it, concentrate on doing and be taught on the go.

Epilogue: From JavaScript to Python Pt.3

Congratulations! You’ve simply completed this mini collection. By now, you need to have some understanding of the essential ideas of Python. There’s nonetheless quite a bit you must be taught to turn into proficient on this language. Nonetheless, because of your data of JavaScript, getting deeper into the main points and intricacies of Python and will likely be simpler and extra snug.

The place to go from right here? I counsel engaged on tutorials. Subsequent, you possibly can comply with on-line programs and faculties that supply playgrounds and dealing with code. Some good selections are CodeCademy (presents free and paid programs), SoloLearn (presents free programs) and Learn Python (presents free programs). For on-line playground you possibly can attempt Python Fiddle. Bear in mind, concentrate on doing.

For those who favored this text, please subscribe so you do not miss any future publish.

If you would like to help me and this weblog, you possibly can turn into a patron, or you should purchase 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 #Pt3