All labs · lesson 1 of 9

R vs Pythonbeginner10 min

R/Python Comparison: Calculator Basics

Learn how to use R and Python as calculators - perform basic arithmetic and mathematical operations

Edit any code block and press Run (or Ctrl/⌘ + Enter, or Shift + Enter). The code runs in your browser, so there is nothing to install. The first run downloads the language once; later runs are instant.

Both R and Python can be used as powerful calculators. Let’s explore basic arithmetic operations and see how they compare.

Basic Arithmetic

Both languages support standard arithmetic operations like addition, subtraction, multiplication, and division.

Exercise 1

Try basic arithmetic operations

# Basic arithmetic in Python
print(1 + 1)
print(2 * 3 + 5)
print(10 / 3)
print(10 // 3)  # Integer division
print(10 % 3)   # Modulo
print(2 ** 3)   # Exponentiation
# Basic arithmetic in R
print(1 + 1)
print(2 * 3 + 5)
print(10 / 3)
print(10 %/% 3)  # Integer division
print(10 %% 3)   # Modulo
print(2 ^ 3)     # Exponentiation

Mathematical Functions

Both languages provide mathematical functions. In Python, you need to import the math module first, while R has these functions built-in.

Exercise 2

Explore logarithmic and exponential functions

from math import log, exp, sqrt

# Mathematical functions
print(log(2))      # Natural logarithm
print(exp(1))      # e^1
print(sqrt(16))    # Square root
print(log(100, 10)) # Log base 10
# Mathematical functions (built-in)
print(log(2))      # Natural logarithm
print(exp(1))      # e^1
print(sqrt(16))    # Square root
print(log(100, 10)) # Log base 10

Key Differences

  • Python requires importing the math module for advanced functions
  • R has mathematical functions available by default
  • Python uses ** for exponentiation, while R uses ^
  • Python uses // for integer division, while R uses %/%
  • Both use % (Python) or %% (R) for modulo operations