Following with the series in this crash course from Python to Scala, today I’ll introduce one of the most useful Scala’s data structures and make the comparison to Python.

Scala Lists

Starting off with one of the most used data structures in Scala (and in functional languages in general) and also the most common data structure in Python as well: the lists.

A list in Scala is a data structure to represent a collection of values of the same type. Lists are very used in Python, and the concept is quite similar in Scala, with a couple of exceptions. First, in Python are written as a list of comma-separated values between square brackets. The empty list, is represented as a pair of empty square brackets:

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> empty = []
>>> empty

In Scala, a list is build with the use of a constructor of name List and the values passed by parameter to the constructor. The empty list is represented by the empty constructor:

scala> val squares = List(1, 4, 9, 16, 25)
squares: List[Int] = List(1, 4, 9, 16, 25)

scala> val empty = List()
empty: List[Nothing] = List()

Following my series of tutorial of Scala for Python programmers, I’ll start to talk about something most Python programmers don’t usually pay attention to because the language doesn’t require it to do so.

Scala Types

I’m talking about data types. It’s not that Python doesn’t have types for its variables, but as it is a dynamically typed programming language, you usually don’t care about the type of the variable. At least not unless you try to add a number and a letter: you cannot add apple and oranges, naturally you cannot add strings and numbers (not at least without conversion first):

In general terms, however, Python won’t bother about the type you are giving to your variables: actually, you won’t be able to declare a type for them as Python will infer it. So, this is perfectly normal for a Python program:

string = "This is a string"
print(string) # Will output "This is a string"

string + 100 # Invalid. Will result in a TypeError exception.

string = 100 # Perfectly valid. `string` type will be int from now on.
print(string) # Will output 100

string + 100 # Valid. Will result in 200.

This is the first post in a series in which I’ll try to give a nice insight for the Scala Language to a programmer with background in Python. I chose to do these posts since, at least when I started this series, the “Scala for people coming from Python” tutorial was a work in progress.

First of all I’ll state some of my background (in case you didn’t check my about page), in a kind of a disclaimer. There are people out there who are experts in Python. I’m not one of them. I only have a background of 4 years in this language, and only work with the 2.X version (started with 2.5 until 2.7). Never even try to learn Python 3. Also, there are experts on Scala as well, I’m not one of those either. In fact, my Scala knowledge is far from deep, I learned Scala at the end of last year and been using it since then (along with Python).

There are people out there who are experts in Python. I’m not one of them. […] Also, there are experts on Scala as well, I’m not one of those either.

Once you know this, I’ll just say I have enough knowledge of both Scala and Python to get by. I’ve done some projects in Django and some projects in Play Framework, but nothing really impressive. The reason I’m doing this set of tutorials is because when I started to learn Scala I didn’t have one and many times I end up in Stackoverflow looking for how to do in Scala things I did in Python.

As every programmer trying to learn a new language, I think is suitable for me to start this professional blog with a Hello World. So, in this first blog post I say Hello World!

This is the first blog I’ve created that has relation with what I do for a living. The only other surviving blog is a personal one (and is in Spanish also).

The aim of this place is to write about Computer Sciences problems and solutions I came across to overcome them, most of them of course are programming ones.

As I said in my about page, I was for a long time a Python programmer, but now I turn myself into the world of Scala and it has so interesting I’ve practically abandoned my old time coding Python skills (of course, with Python, is never that hard to return).

I hope my posts will help you somehow.