...
Code Block |
---|
jupyter lab /path/to/folder |
...
Basic conversions
| R | Python |
---|
Arithmetic operators | Code Block |
---|
x + y
x - y
x * y
x / y
x ^ y |
| Code Block |
---|
x + y
x - y
x * y
x / y
x ** y |
|
The only difference is that R uses ^ for exponentiation, while Python uses ** . |
Comparison operators | Code Block |
---|
# R and Python
x == y
x != y
x > y
x < y
x >= y
x <= y |
|
No differences here. |
Logical operators | Code Block |
---|
x && y
x || y
!x
v1 & v2
v1 | v2 |
| Code Block |
---|
import numpy as np
x and y
x or y
not x
np.logical_and(v1, v2)
np.logical_or(v1, v2) |
|
Python doesn’t have built-in logical operations for vectors. numpy is a popular library for this purpose, but it’s not as clean as R’s syntax. |
Variable assignment | name <- "John Doe"
| name = "John Doe"
|
R does accept the = syntax for assignment. However, | but it’s non-idiomatic and can lead to unexpected errors. For example, Code Block |
---|
| my_function <- function(y = 3, x = 5) {
print(paste(y, x))
}
my_function(x = 10) # Passes 10 to the x parameter, and outputs "3 10"
my_function(x <- 20) # Assigns 20 to the global x variable, which it passes to the y parameter. Outputs "20 5" |
To avoid confusion, always use <- for assignment, and use = for passing arguments within function calls. |
Declaring a RangeRanges | Code Block |
---|
| 1:10
seq(1, 20, 2)
print(1:10) |
| Code Block |
---|
| range(1, 11)
range(1, 21, 2)
print(list(range(1, 11))) |
|
Ouch. Python’s Python’s code is not only longer, but it excludes the final number.
range(1, 11) creates a range object in Python, which represents the numbers 1 through 10. list(range(1, 11)) creates a list object in Python, which represents [1, 2, 3, … 10] Both are similar function similarly to the 1:10 vector in R. But However, you need to change a range to a list in order to output it.
|
Conditional Statements | Code Block |
---|
if (x > 5) {
print("x is greater than 5")
} else if (x >= 2) {
print("x is between 2 and 5")
} else {
print("x is less than 2")
} |
| Code Block |
---|
if x > 5:
print("x is greater than 5")
elif x >= 2:
print("x is between 2 and 5")
else:
print("x is less than 2") |
|
R requires the condition to be in parentheses. That would also work in Python, but it’s not required. R begins a block statement with an opening curly brace ({ ), and it ends it with a closing curly brace (} ). Python is the oddball here, using a colon (: ) to begin a block statement. It then uses indentation for the block’s statements. To end a block, you stop indenting. This can take some getting used to, because very few computer languages have similar syntax. Note that the recommended indentation for Python is 4 spaces, but consistency is all that matters. |
For Loops | Code Block |
---|
for (i in 1:5) {
print(i)
} |
| Code Block |
---|
for i in range(1, 6):
print(i) |
|
Note the similarity to the if statement comparisons above – particularly the absence of parentheses in the iteration expression, and the block syntax. |
Functions | Code Block |
---|
my_function <- function(x, y) {
return(x + y)
} |
| Code Block |
---|
def my_function(x, y):
return x + y
|
|
| | |
Indexing | | |
R uses 1-based indexing. Python uses 0-based indexing. R is actually the oddball here. |
...
Example conversions
The following examples demonstrate how to convert the R code at https://github.com/jjennewein/MDA_2023-24 into equivalent Python code.
...