Scala for Python developers


If you are a Python developer, perhaps you ever felt the need to step up your Django application, or your data pipeline, to the next level, towards a technology which allows to build larger, safer and more robust softwares.

  • This should not be taken as a “dictionary” between Python and the basics of Scala. In fact, a dictionary between two languages should never exist. You should rather read this with the mindset of “I’m doing this in Python, how would Scala developers achieve the same?” instead of “this class in Python translates to that class in Scala”.
  • you won’t find any instruction for setting you up. If you’re like me and you consume Medium articles while waiting at the dentist, or doing the line at your grocery store, then you most probably don’t have a computer to do it right now anyway. There are plenty of resources out there for helping you with that. Most of them will probably include installing sbt, IntelliJ, adding metals extension for VScode, or ammonite. If you’re fond of notebooks, perhaps Netflix’s Polynote could be a good fit, especially since it interoperates with Python.


In Python you declare variables simply by assigning them values, like `x = 3`. In Scala, you need to add the identifier `val` to write `val x = 3`. Notable difference, though, you can’t change the value of `x` afterwards. We don’t do that in Scala. (For the experts only, you can if you write `var` instead of `val`.)


Defining functions in Scala is very similar to Python’s way:

  • The `return` keyword is not necessary in Scala
  • The equal sign is used to start the body, instead of the colon (the colon is used to type the return type)
  • If you have a substantial function body, Scala uses curly braces instead of indentation

Working with lists

Creating lists and “dictionaries”

In Python, you typically use the square bracket syntax for lists, like `ls = [1, 2, 3]`. In Scala, you simply call the `List` “constructor” function, `val ls = List(1, 2, 3)`. Again, while in Python you can change your list `ls`, with, e.g., `ls[1] = 4`, you can’t do that in Scala (it’s actually never necessary).

d = {3: “hello”, “foo”: “bar”}
val d = Map(3 -> “hello”, “foo” -> “bar”)

Transforming the lists

In Python, there are typically two ways of manipulating lists (and dictionaries). Either with list comprehensions, or using built in functions `map`, `filter`… In Scala, you also have two choices: either by calling “transformation” methods on the lists (like map or filter), or using *for* comprehensions. So let’s see a hotchpotch of examples. We will consider `xs = [1, 2, 3]` (and equivalently `val xs = List(1, 2, 3)`).

Handling “missing” elements

In Python, we can use `None` for assigning (or returning) missing elements. Since Python is interpreted, there are no real gain to have something more robust: nothing will prevent you from writing `x + 2` if `x` is None at that point. (There may be some advantages in certain corner cases.) In Scala, things are different, and you have a special type for missing elements, called `Option`. (Note that `null` also exists, like in Java or JavaScript, but you should never use it!)

Data representation

Scala has a nice way of representing complex data, which are “case classes”. A case class is a regular class with a lot of boilerplate added to it. There are similar to classes extending NamedTuple in Python.

Control flow

Control flow in Python essentially involves for loops and long “if-else” statements. In Scala, there is basically no loop (we work on collections instead) and “if-else” sequences are powerfully replaced by “pattern matching”. Pattern matching is like a switch, but it allows to deconstruct elements to extract their components. For example, you can do

Object oriented

Python is often liked for its support for object oriented paradigm. I actually think that Scala is hard to beat in that regard, as it is very clean, intuitive and powerful. Let’s see a small class definition in Python, and its counterpart in Scala.


With all of that, you should be ready to get started coding in Scala. As you will discover, most of the time you simply follow what your ide suggests, relying on the types when in doubt…

Mathematician, Scala enthusiast, father of two.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store