DEV Community


Posted on

What's the difference? Ruby vs. Python

After spending a month and a half working exclusively in Ruby and Rails, I feel that I have a pretty solid knowledge base for the language. But Python is a very in demand language at the moment, and I've heard time and time again that the languages are very similar and both easy to use. This really sparked my curiosity, and originally I had decided that after my coding bootcamp I would try my hand at Python see what all of the hype is about. But coming fresh off of working in Ruby, I thought it'd be a perfect time to compare and contrast the two both logistically and technically to give anyone an idea if you're looking into using one or both of these languages.

Both Ruby and Python are dynamic, object oriented programming languages that can be used for a multitude of purposes, from building web applications to hosting and accessing databases.


  • Made to be easily read and understandable in a way similar to English
  • Multiple ways in which something can be written, allowing for user customization and personalization
  • Used mostly for free and flexible web development
  • Most popular framework is Rails
  • Examples of websites made using Ruby include Airbnb, Hulu, ZenDesk, GitHub, Basecamp, Kickstarter


  • Very strict way in which something can be written, one general formula for achieving things
  • Used substantially for data science and scientific programming
  • Most popular framework is Django
  • Examples of websites made using Python include Spotify, Youtube, Dropbox, Reddit, Instagram

Now to dip our toes into the technical differences, we can start with an oldie but a goodie, our classic hello world example.

message = "Hello World!"
Enter fullscreen mode Exit fullscreen mode
message = "Hello World!"
Enter fullscreen mode Exit fullscreen mode

For simply logging to the console, Ruby uses puts whereas Python uses print. Both Ruby and Python execute code from the top down, so anything else we add to this snippet will be printed to the terminal after our original "Hello World!" line. It will also overwrite anything that was previously declared, and the last line of code will be the final output.

But they both don't differ in the way that variables are declared or assigned. It is as simple as your variable name set equal to its value. They do differ slightly in terms of data types as you can compare and contrast from the table below:

Data Type Ruby Python
boolean ✓(true or false) ✓(True or False)
undefined nil None

In Ruby, generally a collection of data can be displayed in either an array or a hash. In Python, data is stored in a similar way but denoted as either a list or a dictionary.

#Ruby array
nums = [1, 2, 3, 4, 5]
puts nums[1] #2
Enter fullscreen mode Exit fullscreen mode
#Python list
nums = [1, 2, 3, 4, 5]
print nums[1] #2
Enter fullscreen mode Exit fullscreen mode

When accessing items from an array or list, Ruby and Python are generally the same as denoted above. But when taking a range of items, they differ only by the symbol used. Ruby uses ... and Python :. Python's : tends to be exclusive, whereas Ruby's ... tend to be inclusive.

nums = [1, 2, 3, 4, 5]

puts nums[0..3] #1, 2, 3, 4
#prints from index 0 to 3, inclusive
Enter fullscreen mode Exit fullscreen mode
nums = [1, 2, 3, 4, 5]

print nums[0:3] #1, 2, 3
#prints from index 0 to 2, exclusive

print nums[1:] #2, 3, 4, 5
#will print from index 1 to end

print nums[:4] #1, 2, 3, 4
#will print from first(index 0) to index 3
Enter fullscreen mode Exit fullscreen mode

As you can tell so far, these languages share remarkable similarities. The only noticeable difference in terms of Ruby hashes and Python dictionaries is that Ruby allows for a => to be used when assigning a key its value, and Python strictly uses colons. Ruby can also use colons for value assignment, but varies slightly in the way that the value is accessed.

#Ruby hash 
colors = { 1 => 'red', 2 => 'green', 3 => 'blue'}
puts(colors[1]) #red
Enter fullscreen mode Exit fullscreen mode
#Python dictionary
colors = {1 : 'red', 2 : 'green', 3 : 'blue' }
print(colors[1]) #red
Enter fullscreen mode Exit fullscreen mode

In addition to lists, Python also has a few other data collection types known as tuple and set.

Unlike lists, tuples are denoted by parentheses and are ordered and unchangeable. In order to add or remove items, the tuple would need to be changed into a list and then back into a tuple. Data inside of a tuple is accessed the same way as a list.

#Python tuple
teas = ("black", "green", "chamomile")
print(teas[-1]) #chamomile
Enter fullscreen mode Exit fullscreen mode

The last one is a set, which is a collection that is denoted by curly braces but does not have an order or index. That means it is not possible to access a value using an index, but it can be looped through to see all of the values. Values can also be added to a set.

flowers = {"lily", "azalea", "buttercup"}
print(flowers) #azalea, lily, buttercup
Enter fullscreen mode Exit fullscreen mode

(note: the order will be random when printed to the console)


For and while loops are very, very similar in Ruby and Python. The only noticeable difference is that Python uses a colon to denote when the loop is to begin and Ruby has to end each loop with an end.

nums = [1, 2, 3, 4, 5]

for i in nums do
  puts i
#1, 2, 3, 4, 5

j = 0
while j < 7
  puts j
  j += 1
#1, 2, 3, 4, 5, 6
Enter fullscreen mode Exit fullscreen mode
fruits = ["apple", "banana", "cherry"]
for i in fruits:
#apple, banana, cherry

j = 1
while j < 6:
  j += 1
#1, 2, 3, 4, 5
Enter fullscreen mode Exit fullscreen mode

They have a few special cases, as Ruby has until and Python has continue. But these don't readily affect their general uses for for and while loops.

If statements

These two are also very similar in Ruby and Python. Differing only by Python using a : to denote the start of the statement and their interesting spellings for else if.

i = 2
if i <= 3
   puts "aww, what a small number."
elsif i > 3 and i < 10
   puts "that's a medium sized number"
   puts "wow, that's a big number!"
#aww, what a small number.
Enter fullscreen mode Exit fullscreen mode
c = 5
d = 1
if c > d:
  print("c is greater than d")
elif c == d:
  print("c and d are equal")
  print("c is greater than d")
#c is greater than d
Enter fullscreen mode Exit fullscreen mode

Interpolation and String Literals

Both languages use curly braces {} to interpolate into strings. But Ruby requires a # before the curly braces and Python does not.
Also, Ruby uses single or double quotes to span over multiple lines (double quotes are required for interpolation) and Python uses triple quotes.

name  = "Leslie"
occupation = "park director"
"Hello, my name is #{name} & I'm a(n) #{occupation}."
Enter fullscreen mode Exit fullscreen mode
pet = 'dog'
age = '6'
print(f'''I have a {pet} who is {age} years old.
{pet}s are very fun!''')
#I have a dog who is 6 years old. dogs are very fun.
Enter fullscreen mode Exit fullscreen mode

Python3 requires a f for a formatted string, meaning a string that will include interpolation into it.

Lastly, there are also a few other small things I noticed when comparing these two languages:

  • print requires parentheses to function, whereas puts does not
  • # works for commenting out in both Ruby and Python

If you're new to Python like me, have been interested in Ruby and wanted to see the differences, or just come to learn a bit about both, I hope this has been helpful and maybe given you a little insight into two of the most popular and dynamic programming languages today.

Top comments (8)

questionmarkexclamationpoint profile image

Ruby has Complex numbers. Ruby has sets (they also have a nice feature of being ordered in the same way you originally inserted). Instead of a tuple in Ruby, you can use #freeze on an array. Ruby also has a fraction type. Ruby also has a range type (which could replace nums in your example with (1..5)). Also worth noting that Ruby uses a big integer implementation, so there is effectively no max integer.

Although there are many ways to do the same thing, the more Ruby-like way to form those loops would be nums.each and 7.times. When I first started using Ruby I found myself trying to write things the way I have been taught in other languages. As I learn more and more about the various ways to do things in Ruby I am amazed at how readable things can be if the language gives you the right facilities. while i < 7 may be perfectly readable to you as someone who has written code before, but if you leave your preconceptions at the door I think you'll find that 7.times is more understandable. The same is true, I find, for so many things in Ruby.

Another thing of note is that in addition to if you can use unless, and in addition to while you can use until. You can also put these constructs after a statement, on the same line, to simplify and make things more readable, for instance: return true if i < 3, or x += 1 until x > 10 (kind of a silly example).

One thing I think you should highlight about Ruby is the stark difference between it and Python when it comes to lambdas. Ruby has the upper hand there.

Additionally, Ruby allows you to monkey patch your own code into existing classes. Wish Integer had a #factorial method? Monkey patch it.

I learned Python first, and then Ruby. When I have to write Python code now I feel as though I am bashing rocks together to make fire. When I use Ruby I feel like I am programming.

etampro profile image
Edward Tam

I think the differences between Ruby and Python are not easily demonstrated until we step into the metaprogramming and phylosophical territory.

But indeed, at the application level they share great deal of similarity.

marcosvafilho profile image
Marcos Filho

Great article, thanks for that.

You probably already know that, but you can also write Ruby hashes in a similar notation to Python dictionaries:

# Ruby hash 
hex_colors = { white: '#FFFFFF', grey_light: '#CCCCCC',  grey_dark: '#333333', black: '#000000' }
puts(hex_colors[:black]) # #000000

Some caveats when using this notation though:

  • no hash keys beginning with a number (because they are symbols)
# invalid Ruby hash
numbers = { 1: 'one', 2: 'two', 3: 'three'}

# valid Ruby hash (no need to use quotes)
numbers = { one: 1, two: 2, three: 3}
  • fetching a value from a key requires using a symbol:
hex_colors = { white: '#FFFFFF', grey_light: '#CCCCCC',  grey_dark: '#333333', black: '#000000' }

# valid notation to fetch values
hex_colors[:black] # returns #000000

# invalid notation to fetch values
hex_colors['black'] # returns nil
paulcook159 profile image
Paul Cook

Programming skills are undoubtedly in high demand and learning a programming language can help you break into in-demand fields like web development, data analysis, machine learning, etc. Wondering which programming language to learn first?

Here, we are waging a tug-of-war between Ruby and Python and find out which programming language is the winner!

Ruby and Python, both the languages were incepted in the mid-90s, with different philosophies, in order to address needs in the programming community.

Both the languages are dynamic, flexible and object-oriented and have different set of functionalities. Ruby is mostly known for meta programming and widely used in web development whereas Python programming skills are highly sought-after by the data science community.

Therefore, which language is better isn’t a question of capability rather an alignment of features & functionalities to your project requirements.

So, find out your own winner programming language between Ruby vs. Python in the infographic

ronaldgrn profile image
Petronald Green

Hmmmm, looks like some Python2 code got in there.

kwstannard profile image
Kelly Stannard
ad0791 profile image
Alexandro Disla • Edited

Flatiron is a great bootcamp.

YOu will do well out there.

cameronbanga profile image
Cameron Banga

Great article! Very well written!