R vs. Python – Basic differences

This entry is part 1 of 3 in the series R vs. Python

R and python are two of the main programming languages used in data science. Sometimes it may be necessary to move from one programming language to another. For example because python is the more generalized programming language while R is more specialized on data analysis.
At the pyconDE 2022 I gave a talk with the title Rewriting your R analysis code in Python.
This is part 1 of a series of blogposts about R vs. Python.
Part 1 is about the simple and more obvious differences.


  • Part 1: Basic differences
  • Part 2: Pipes
  • Part 3: Classes
  • Part 4: Library Loading
  • Part 5: … vs. args,kwargs
  • Part 6: Non-standard evaluation
  • Part 7: Run R code from python and vice versa

Basic Syntax

R has a classic C-like syntax with curly brackets {} for flow control
and parentheses () for stuff like if, for and while.
Python does not. It uses the colon : to start a block and indentation to define the block content. This also means python needs a no-operation keyword for empty blocks. pass


x <- 1
if(x > 0){
  # do nothing


x = 1
if x > 0:
    # do nothing


Python goes the way of most programming languages of using = for assigning values to variable names:


x = 1


The =-sign can also be used in R, but this in not recommended.
The recommended way is using <-:

x <- 1

There is also the assignment operator <<-. This can be used when you want to assign values to variables that are not defined in the function you are writing, or should be usable outside of the function that does the assignment. This is useful when you work with classes and objects for example.

Non-local assignment:

foo <- function(x){
  b <<- 1

And, thus also recommended, there is ->

1 -> x

Scalar vs. Vector

Despite having a different syntax, the most obvious difference between python and R is, that indexing in R starts counting its indices at 1, while python starts at 0.


data <- c(1,2,3,4)


data <- [1,2,3,4]

In R vectors, all values have the same data type. They are similar to numpy arrays in python:

import numpy as np
foo = np.array([1])

Indexing: 1 vs. 0

The other major difference between R and python is, that each variable is a vector by default in R:

foo <- 1

While in python you have scalars:

foo = 1
>>> TypeError: 'int' object is not subscriptable


Lists in R can have different data types per value. They are similar to lists and dictionaries in python.


myList = [1,"abc",[1,2,3]]


myList <- list(1,"abc",list(1,2,3))
Series NavigationR vs. Python – Pipes >>

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert