Scala Vs Python Syntax Cheat Sheet

Scala Vs Python Syntax Cheat Sheet

Similarities and Differences Between Scala and Python


6 min read

There are about 8 million Python developers around the world and it's also the top programming language people want to learn, but why is that? Why not any other language but Python? Let me tell you, Python offers a lean learning curve, with just 2-3 days of hands-on Python anyone can start developing applications (they might not be technically sound software initially), it's syntax is one of the easiest to learn due to its plain english like code, it offers flexibility from both domains either object-oriented or functional programming (not so much FP, but it has its benefits) and it is used in almost every field of software engineering.

The only drawback people face when working with Python is related to its speed of executing programs. But is there any other programming language as easy to understand and to get started to work with? Yep, there is, Scala comes to the rescue!

Python faces speed issues mainly due to its interpreted nature, whereas Scala is a compiled language that offers dynamic typing as well (close to 90%), many new features in Python like pattern matching are already present in Scala since the beginning, there are awesome syntactic sugars in Scala that allows us to write even less code when compared to Python and also Scala being a functional programming language does not shy away from object-oriented programming as it is run over JVM.

When it comes to the syntax of these programming languages, there are a lot of similar patterns and some differences as well, but overall they both offer a lean learning curve with endless opportunities in the software engineering world.

So, let's discuss those similarities and differences in the form of a cheat sheet, which not only helps in refreshing some concepts from Python/Scala but also acts as a guide to learn Python or Scala for someone coming from the Scala or Python background respectively.

This will be a to-the-point kind of article, with little to no explanation but with necessary links wherever needed.

So, let's get started!

How To Import?

In Python importing a library can look like this:

import math.*

# OR

import math.pow

# OR

from math import pow, sqrt

# OR

from math import sqrt as squareRoot

In Scala importing a library can look like this:

import scala.math._

// OR

import scala.math.BigInt

// OR

import scala.math.{BigInt, BigDecimal}

// OR

import scala.math.{BigInt => BInt}

How To Declare Variables?

Python offers dynamic typing so you don't need to declare variables beforehand like in C/C++, but you can just type out the variables and use them:

some_variable = 10

Scala also offers dynamic typing to some extent and infer that type during compile time:

val some_variable = 10  // infers it as Int type

How To Define Conditionals?

In Python and Scala defining conditionals are more or less similar:

v = 5
if v%2==1:
elif v==0:

In Scala:

val v = 5
if (v%2==1) println("Odd")
else if (v==0) println("Zero")
else println("Even")

The important thing to note in conditionals is that in Python conditionals are referred to as statements whereas in Scala these are expressions that evaluates to something or nothing.

How To Define Loops?

In Python a simple for-loop would look like this:

for i in range(10):
    # do something here

The same for-loop in Scala would look like this:

for {
    i <- 0 until 10
}   {
    // do something here

The difference in their respective working behind the scenes, in Scala, is the use of conjunction of map and flatMap methods whereas in Python it is simple iteration.

There is a while loop in Scala, but let's not discuss that, while coding in Scala it is not encouraged to use while loops.

Why are while loops not recommended in Scala?

How To Write Methods?

In Python, we can simply use the def keyword to define functions, which are similar to methods in Scala.

def some_method():
    # do something here

In Scala we can define a method like:

def someMethod() = {
    // do something here

To define functions in Scala, we can use Function objects or their respective syntactic sugars. To know more about functions and functional programming in Scala, do check this.

How To Define Classes and Objects?

In Python, we can define a class using the class keyword, the same keyword can be used to define a class in Scala.

Whereas objects in Scala are singleton instances and similar to what we call static class in other programming languages.

A class however in Scala can be instantiated using the new keyword, whereas in Python calling the class directly creates an instance for that.

In Python:

class Student:

    def __init__(self):
        # do something here

student = Student()     # creates an instance of class Student

In Scala:

class Student

val student = new Student()     // creates an instance of class Student

// use Object keyword for singleton objects

Object Student

val aParticularStudent = Student

To know more about classes and objects in Scala, I recommend checking out this article.


Scala offers 2 more advantages over Python, namely traits and tail recursion.

Python does not offer tail recursive capabilities to its functions and suffers from stack overflow errors whenever functional programming goes wrong. To know more about why python does not offer tail recursion, you can read this article. However, there is a work-around as described in this article using decorators.

Similarly, Python does not have concepts like traits. A trait is used to describe the quality of a class, whereas in Python you would have to do multiple inheritances to get certain qualities or have to define certain methods within the class definition. Trait allows loose coupling.

How to define a trait in Scala?

trait SomeQuality

class Student extends SomeQuality

Wrap Up

Well, that wraps up this simple Scala Vs Python syntax cheat sheet.

What else would you add to this cheat sheet? Please let me know in the comments!

If you are looking to sharpen your coding skills in Python or Scala, then do consider buying these awesome books that go in-depth to let you understand the semantics of the language.

For Python:

For Scala:

  • Make sure to follow me to get regular updates or subscribe so that you never miss my upcoming articles, ranging from Python to Computer Vision to Scala.

  • Just starting your Open Source Journey? Don't forget to check Hello Open Source

  • Want to showcase your Python project or just out there to explore? Check Awesome Python Repos

  • Want to make a simple and awesome game from scratch? Check out PongPong

  • Want to ++ your GitHub Profile README? Check out Quote - README

Till next time!

Namaste ๐Ÿ™

Did you find this article valuable?

Support Siddharth Chandra by becoming a sponsor. Any amount is appreciated!