Python for Rubyists, Part I: Getting Started

Python is one of the most popular programming languages in the world. According to GitHub, as of 2020 it was second only to JavaScript.

So if you already know how to program in Ruby and you’re looking to learn a new programming language, Python is a pretty good choice.

It helps that the two languages have a lot of similarities — among other things, they’re both scripting languages and don’t need to be compiled, and they’re both dynamically typed and allow you to use variables without declaring them first.

But there are still plenty of differences in the syntax of the two languages. In this series, we’ll go through some of the basic Python syntax as it compares to Ruby. It won’t be a comprehensive guide to Python, but it should be enough to get you started.

First, make sure your environment is set up. We’ll be using Python 3 for this guide, so just type python3 in the command prompt on Windows or in the terminal on OS X or Linux.

If it turns out you don’t have python on your machine yet, just go here for instructions on how to set it up.

Once you do have python installed, fire up your favorite code editor and let’s get started!

In Python, the classic Hello, world! program is one simple line: print(“Hello, world!”) .

This is an important difference between Ruby and Python — where in Ruby you’d use puts to output a new line to the terminal, in Python it’s print.

Next we’ll add some flexibility to the output in the form of some variables.

first_name = "Alyssa"print(f"Hello, {first_name}!")

This outputs the string “Hello, Alyssa!”

In Ruby, you use #{x} to interpolate variables in strings. In Python 3, there are a few ways to do it, but the simplest is to put an f in front of the string and surround variables with curly braces, like we did above.

Now let’s turn this into a simple function:

def say_hello(first_name):
print(f"Hello, {first_name}!")

The output here is the same as before, but this time we used a function to do it.

The snake_case, the parameters in parentheses, the def to indicate you’re defining a function or method— that should all be very familiar to Rubyists. The major difference here is the colon to show where the function begins, and the indentation to show which lines of code are included within the function.

This is extremely important. In Ruby, you can be lazy about indenting your code. It may be ugly, but it doesn’t affect the outcome of what you’re writing. In Python, indenting is how you tell the program which lines are included in things like functions, loops, and conditional statements. Make sure you indent properly.

Another important difference when it comes to functions and methods is the return value. In Ruby, if you don’t explicitly return a value from a function then it will just return the result of the last instruction in the function. In Python, it will return the ‘None’ value. So if you want to return something specific from a function or method, make sure you include a return statement.

That’s all for now! Tune in next time to learn more about variables and some basic data types.

Software developer and science/tech writer. Python, Ruby on Rails, JavaScript, React/Redux, Java. Fascinated by the amazing stories behind today’s tech.

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