python-scala-syntax-mapping
/python-scala-syntax-mapping
Reference guide for translating Python syntax constructs to Scala equivalents. Use when converting Python code to Scala and need mappings for basic syntax elements like variable declarations, control flow, comprehensions, string formatting, and common operators.
- Author
- benchflow-ai ↗
- Repository
- skillsbench ↗
- Licence
- Apache-2.0
- Last repository change
- 12.10.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 12.10.2026, without references to retired models or APIs.
Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.
What it does and when to use it
Reference guide for translating Python syntax constructs to Scala equivalents. Use when converting Python code to Scala and need mappings for basic syntax elements like variable declarations, control flow, comprehensions, string formatting, and common operators.
Original author description
Reference guide for translating Python syntax constructs to Scala equivalents. Use when converting Python code to Scala and need mappings for basic syntax elements like variable declarations, control flow, comprehensions, string formatting, and common operators.
How it works
- Variable Declarations
- Type Mappings
- Control Flow
- Comprehensions
- Functions
- String Formatting
- Common Operators
- Exception Handling
- None/Null Handling
SKILL.md preview
---
name: python-scala-syntax-mapping
description: Reference guide for translating Python syntax constructs to Scala equivalents. Use when converting Python code to Scala and need mappings for basic syntax elements like variable declarations, control flow, comprehensions, string formatting, and common operators.
---
# Python to Scala Syntax Mapping
## Variable Declarations
| Python | Scala |
|--------|-------|
| `x = 5` | `val x = 5` (immutable) or `var x = 5` (mutable) |
| `x: int = 5` | `val x: Int = 5` |
| `x, y = 1, 2` | `val (x, y) = (1, 2)` |
Prefer `val` over `var` unless mutation is required.
## Type Mappings
| Python | Scala |
|--------|-------|
| `int` | `Int` |
| `float` | `Double` |
| `str` | `String` |
| `bool` | `Boolean` |
| `None` | `None` (of type `Option`) or `null` |
| `list[T]` | `List[T]` or `Seq[T]` |
| `dict[K, V]` | `Map[K, V]` |
| `set[T]` | `Set[T]` |
| `tuple[A, B]` | `(A, B)` or `Tuple2[A, B]` |
| `Optional[T]` | `Option[T]` |
## Control Flow
### Conditionals
```python
# Python
if x > 0:
result = "positive"
elif x < 0:
result = "negative"
else:
result = "zero"
```
```scala
// Scala - if is an expression
val result = if (x > 0) "positive"
else if (x < 0) "negative"
else "zero"
```
### Loops
```python
# Python for loop
for i in range(10):
print(i)
for item in items:
process(item)
for i, item in enumerate(items):
print(f"{i}: {item}")
```
```scala
// Scala equivalents
for (i <- 0 until 10) println(i)
for (item <- items) process(item)
for ((item, i) <- items.zipWithIndex) println(s"$i: $item")
```
### While loops
```python
# Python
while condition:
do_something()
```
```scala
// Scala
while (condition) {
doSomething()
}
```
## Comprehensions
```python
# Python list comprehension
squares = [x ** 2 for x in range(10)]
evens = [x for x in numbers if x % 2 == 0]
pairs = [(x, y) for x in xs for y in ys]
```
```scala
// Scala for-comprehensions or map/filter
val squares = (0 until 10).map(x => x * x).toList
val evens = numbers.filter(_ % 2 == 0)
val pairs = for { x <- xs; y <- ys } yield (x, y)
```
## Functions
```python
# Python
def add(a: int, b: int) -> int:
return a + b
# Lambda
square = lambda x: x ** 2
# Default arguments
def greet(name: str, greeting: str = "Hello") -> str:
return f"{greeting}, {name}!"
```
```scala
// Scala
def add(a: Int, b: Int): Int = a + b
// Lambda
val square: Int => Int = x => x * x
// or shorter: val square = (x: Int) => x * x
// Default arguments
def greet(name: String, greeting: String = "Hello"): String =
s"$greeting, $name!"
```
## String Formatting
| Python | Scala |
|--------|-------|
| `f"Hello, {name}!"` | `s"Hello, $name!"` |
| `f"Value: {x:.2f}"` | `f"Value: $x%.2f"` |
| `f"{x + y}"` | `s"${x + y}"` |
## Common Operators
| Python | Scala |
|--------|-------|
| `**` (power) | `math.pow(x, y)` or use `scala.math.pow` |
| `//` (floor div) | `x / y` (for Int) or `math.floor(x / y)` |
| `%` (modulo) | `%` |
| `and`, `or`, `not` | `&&`, `||`, `!` |
| `in` | `.contains()` |
| `is` | `eq` (reference equality) |
| `==` | `==` (value equality) |
## Exception Handling
```python
# Python
try:
result = risky_operation()
except ValueError as e:
handle_error(e)
finally:
cleanup()
```
```scala
// Scala
import scala.util.{Try, Success, Failure}
// Pattern 1: try-catch
try {
val result = riskyOperation()
} catch {
case e: IllegalArgumentException => handleError(e)
} finally {
cleanup()
}
// Pattern 2: Try monad (preferred for functional style)
val result = Try(riskyOperation()) match {
case Success(value) => value
case Failure(e) => handleError(e)
}
```
## None/Null Handling
```python
# Python
if value is None:
return default
return process(value)
# Or
result = value if value is not None else default
```
```scala
// Scala - use Option
val result = optionValue match {
case Some(v) => process(v)
case None => default
}
// Or more concisely
val result = optionValue.map(process).getOrElse(default)
```
Versions
The database has no other skills named python-scala-syntax-mapping.
Database snapshot: 13.11.2026 · package version 8ae571c0c1e2 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 95python-scala-libraries
Guide for mapping common Python libraries and idioms to Scala equivalents. Use when converting Python code that uses standard library modules (json, datetime, os, re, logging) or needs equivalent Scala libraries for HTTP, testing, or async operations. - 85Scala Functional Patterns
Use when functional programming patterns in Scala including higher-order functions, immutability, pattern matching, algebraic data types, monads, for-comprehensions, and functional composition for building robust, type-safe applications. - 95scala-tokenizer-implementation-guide
Use this skill when writing /root/Tokenizer.scala. It provides the concrete implementation strategy, Scala idioms, and compilation workflow for translating the Python Tokenizer to Scala 2.13. - 100designing-syntax
Design custom syntax elements with reuse-first approach for workflow orchestration. Use when user needs custom operators, checkpoints, or syntax patterns not available in core syntax. - 95python-scala-functional
Guide for translating Python code to functional Scala style. Use when converting Python code involving numeric formatting, higher-order functions, decorators, closures, generators, or when aiming for idiomatic functional Scala with pattern matching, Option handling, and monadic operations. - 100python-scala-idioms
Guide for writing idiomatic Scala when translating from Python. Use when the goal is not just syntactic translation but producing clean, idiomatic Scala code. Covers immutability, expression-based style, sealed hierarchies, and common Scala conventions.