Equivalents in Python and JavaScript. Part 3
Tags: basics, intermediate, javascript, python
Published: 2018-07-06 09:19:09

---
title: Equivalents in Python and JavaScript. Part 3
published: true
tags: Basics,Intermediate,JavaScript,Python
canonical_url: http://djangotricks.blogspot.com/2018/07/equivalents-in-python-and-javascript-part-3.html
series: Equivalents in Python and JavaScript
---

 ![](https://1.bp.blogspot.com/-QOkE0Sse2NU/WzrCwrbe4qI/AAAAAAAAB54/Y0P1FNJqDc4gnR1JiAjv4MLGllcrZDFvACLcBGAs/s1600/equivalents-in-python-and-javascript-part-3.png) 

This is the 3rd part of 4-article series about analogies in __Python__ and __JavaScript__. In the [previous parts](https://dev.to/djangotricks/equivalents-in-python-and-javascript-part-2-3fej) we covered a large part of the traditional __Python__ 2.7 and __JavaScript__ based on the ECMAScript 5 standard. This time we will start looking into __Python__ 3.6 and __JavaScript__ based on the ECMAScript 6 standard. ECMAScript 6 standard is pretty new and supported only the newest versions of browsers. For older browsers you will need [Babel](https://babeljs.io/) to compile your next-generation __JavaScript__ code to the cross-browser-compatible equivalents. It opens the door to so many interesting things to explore. We will start from string interpolation, unpacking lists, lambda functions, iterations without indexes, generators, and sets!

## Variables in strings

The old and inefficient way to build strings with values from variables is this concatenation:

```python
name = 'World'
value = 'Hello, ' + name + '!\nWelcome!'
```

This can get very sparse and difficult to read. Also it is very easy to miss whitespaces in the sentence around variables.

Since __Python__ version 3.6 and __JavaScript__ based on the ECMAScript 6 standard, you can use so called string interpolation. These are string templates which are filled in with values from variables.

In __Python__ they are also called f-string, because their notation starts with letter "f": 

```python
name = 'World'
value = f"""Hello, {name}!
Welcome!"""

price = 14.9
value = f'Price: {price:.2f} €'  # 'Price: 14.90 €'
```

In __JavaScript__ string templates start and end with backticks:

```javascript
name = 'World';
value = `Hello, ${name}!
Welcome!`;

price = 14.9;
value = `Price ${price.toFixed(2)} €`;  // 'Price: 14.90 €'
```

Note that string templates can be of a single line as well as of multiple lines. For f-strings in __Python__ you can pass the format for variables, but you can't call methods of a variable unless they are properties and call getter methods.

## Unpacking lists

Python and now __JavaScript__ has an interesting feature to assign items of sequences into separate variables. For example, we can read the three values of a list into variables a, b, and c with the following syntax:

```python
[a, b, c] = [1, 2, 3]
```

---

For tuples the parenthesis can be omitted. The following is a very popular way to swap values of two variables in __Python__:

```python
a = 1
b = 2
a, b = b, a  # swap values
```

With the next generation __JavaScript__ this can also be achieved:

```javascript
a = 1;
b = 2;
[a, b] = [b, a];  // swap values
```

---

In __Python__ 3.6 if we have an unknown number of items in a list or tuple, we can assign them to a tuple of several values while also unpacking the rest to a list:

```python
first, second, *the_rest = [1, 2, 3, 4]
# first == 1
# second == 2
# the_rest == [3, 4]
```

This can also be done with __JavaScript__ (ECMAScript 6):

```javascript
[first, second, ...the_rest] = [1, 2, 3, 4];
// first === 1
// last === 2
// the_rest === [3, 4]
```

## Lambda functions

__Python__ and __JavaScript__ have a very neat functionality to create functions in a single line. These functions are called lambdas. Lambdas are very simple functions that take one or more arguments and return some calculated value. Usually lambdas are used when you need to pass a function to another function as a callback or as a function to manipulate every separate elements in a sequence. 

In __Python__, you would define a lambda using the `lambda` keyword, like this:

```python
sum = lambda x, y: x + y
square = lambda x: x ** 2
```

In __JavaScript__ lambdas use the `=>` notation. If there are more than one arguments, they have to be in parenthesis:

```javascript
sum = (x, y) => x + y;
square = x => Math.pow(x, 2);
```

## Iteration without indexes

Many programming languages allow iterating through a sequence only by using indexes and incrementing their values. Then to get an item at some position, you would read it from an array, for example:

```javascript
for (i=0; i<items.length; i++) {
    console.log(items[i]);
}
```

This is not a nice syntax and is very technical - it doesn't read naturally. What we really want is just to grab each value from the list. And __Python__ has a very neat possibility just to iterate through the elements:

```python
for item in ['A', 'B', 'C']:
    print(item)
```

In the modern __JavaScript__ this is also possible with the `for..of` operator:

```javascript
for (let item of ['A', 'B', 'C']) {
    console.log(item);
}
```

---

You can also iterate through a string characters in __Python__:

```python
for character in 'ABC':
    print(character)
```

And in the modern __JavaScript__:

```javascript
for (let character of 'ABC') {
    console.log(character);
}
```


## Generators

Python and modern __JavaScript__ has a possibility to define special functions through which you can iterate. With each iteration they return the next generated value in a sequence. These functions are called generators. With generators you can get numbers in a range, lines from a file, data from different paginated API calls, [fibonacci numbers](https://www.youtube.com/watch?v=T8xgfVzef_E), and any other dynamicly generated sequences.

Technically generators are like normal functions, but instead of returning a value, they yield a value. This value will be returned for one iteration. And this generation happens as long as the end of the function is reached.

To illustrate that, the following __Python__ code will create a generator `countdown()` which will return numbers from the given one back to 1, (like 10, 9, 8, ..., 1):

```python
def countdown(counter):
    while counter > 0:
        yield counter
        counter -= 1
        
for counter in countdown(10):
    print(counter)
```

Exactly the same can be achieved in modern __JavaScript__, but notice the asterisk at the `function` statement. It defines that it's a generator:

```javascript
function* countdown(counter) {
    while (counter > 0) {
        yield counter;
        counter--;
    }
}
for (let counter of countdown(10)) {
    console.log(counter);
}
```

## Sets

We already had a look at lists, tuples and arrays. But here is another type of data - sets. Sets are groups of elements that ensure that each element there exists only once. Set theory also specifies set operations like [union, intersection, and difference](http://2ality.com/2015/01/es6-set-operations.html), but we won't cover them here today.

This is how to create a set, add elements to it, check if a value exists, check the total amount of elements in a set, and iterate through its values, and remove a value using __Python__:

```python
s = set(['A'])
s.add('B'); s.add('C')
'A' in s
len(s) == 3
for elem in s:
    print(elem)
s.remove('C')
```

Here is how to achieve the same in modern __JavaScript__:

```javascript
s = new Set(['A']);
s.add('B').add('C');
s.has('A') === true;
s.size === 3;
for (let elem of s.values()) {
    console.log(elem);
}
s.delete('C')
```

## The Takeaways

- String interpolation or literal templates allows you to have much cleaner and nicer code even with a possibility to have multiple lines of text.
- You can iterate through elements in a sequence or group without using indexes.
- Use generators when you have a sequence of nearly unlimited elements.
- Use sets when you want to ensure fast check if data exists in a collection.
- Use lambdas when you need short and clear single-line functions. 

As you know from the previous parts, I am offering a cheat sheet with the whole list of equivalents in __Python__ and __JavaScript__, both, time honored and future proof. To have something printed in front of your eyes is much more convenient than switching among windows or scrolling up and down until you find what you exactly were searching for. So I suggest you to get the cheat sheet and use it for good!

<center>✨✨✨<br /><a href="https://www.djangotricks.com/goodies/YbnpiLKBmAZi/" target="_blank">Get the Ultimate Cheat Sheet of<br />Equivalents in Python and JavaScript<br />✨✨✨</a></center>


In the [next and last part](https://dev.to/djangotricks/equivalents-in-python-and-javascript-part-4-3mda) of the series, we will have a look at function arguments, classes, inheritance, and properties. Stay tuned!

* * *

Cover photo by [Alex Knight](https://unsplash.com/photos/Akz00I_GGjU)

 ![](http://feeds.feedburner.com/~r/djangotricks/~4/D6BXc0mUq3Q)