Python vs Ruby
Side-by-side comparison of Python and Ruby — syntax, idioms, and trade-offs.
Python and Ruby are the two most popular dynamic scripting languages. They share many capabilities but differ in philosophy: Python insists on clarity, Ruby on expressiveness.
| Feature | Python | Ruby |
|---|---|---|
| Philosophy | One obvious way | Principle of least surprise |
| Typing | Dynamic, interpreted | Dynamic, interpreted |
| Variable | x = 42 |
x = 42 |
| Function | def add(a, b): return a + b |
def add(a, b) = a + b |
| List | nums = [1, 2, 3] |
nums = [1, 2, 3] |
| Dict / Hash | {"a": 1, "b": 2} |
{ a: 1, b: 2 } |
| Class | class User: pass |
class User; attr_accessor :name; end |
| Comprehension | [n * 2 for n in nums] |
nums.map { |n| n * 2 } |
| Error handling | try/except/finally |
begin/rescue/end |
| Loop | for i in range(5): |
5.times { |i| } |
| String interp | f"Hello {name}" |
"Hello #{name}" |
| Concurrency | asyncio / Threads (GIL) | Threads (GIL) / Ractors (3.0+) |
| Package manager | pip | Bundler |
| Testing | pytest |
rspec |
| Null | None |
nil |
| Private method | def _method(self): |
private; def method; end |
When to Choose Python
- Data science, machine learning, or scientific computing
- Async I/O with
asyncio - Readability and explicitness are top priority
- Large standard library, batteries-included
When to Choose Ruby
- Rails web development
- Expressive syntax with blocks, procs, metaprogramming
- Convention over configuration
- Flexible DSLs and internal languages