2  Variables, expressions, and statements

Variables, expressions, and statements are fundamental concepts in programming, including in R and Python. In both languages, a variable is a named container for data, and it can hold different types of data such as integers, floats, and strings. An expression is a combination of variables, operators, and functions that evaluates to a value. Expressions are commonly used to perform mathematical operations, logical comparisons, and data manipulation. In R, statements are executed line-by-line in a script or console, and can include function calls, variable assignments, and control flow structures such as loops and conditionals. Python is similar in that statements can include function calls and variable assignments, but also includes additional constructs such as classes and modules that allow for more advanced programming concepts. Understanding variables, expressions, and statements is essential for developing proficiency in R and Python programming, and lays the foundation for more complex programming tasks.

In terms of differences between the two languages, one notable difference is the use of assignment operators. In R, the assignment operator is <-, while in Python it is =. Additionally, Python requires indentation to denote code blocks, whereas R uses curly braces. Another difference is that R has built-in support for statistical analysis and data manipulation, while Python is often used more broadly for tasks such as web development and machine learning. Overall, both languages have their strengths and weaknesses, and the choice between the two often depends on the specific needs of the project or task at hand.

2.1 Basic Exmaple

Here are some examples in both R and Python:

2.1.1 Variables

2.1.1.1 R example

# Creating a variable called "age" and assigning it the value 25
age <- 25

# Creating a variable called "name" and assigning it a string value
name <- "John Doe"

# Creating a variable called "score" and assigning it the value 90
score <- 90

# Creating a vector of numbers and assigning it to a variable called "grades"
grades <- c(80, 90, 85, 92, 87)

2.1.1.2 Python example

The ‘python’ engine in knitr requires the reticulate package.

library(reticulate)
# Creating a variable called "age" and assigning it the value 25
age = 25

# Creating a variable called "name" and assigning it a string value
name = "John Doe"

# Creating a variable called "score" and assigning it the value 90
score = 90

# Creating a list of numbers and assigning it to a variable called "grades"
grades = [80, 90, 85, 92, 87]

2.1.2 Expressions

2.1.2.1 R example

# Performing a mathematical expression and storing the result in a variable called "result"
result <- 5 + 10

# Combining variables and strings in an expression to create a new string
greeting <- paste("Hello", name, "your age is", age)

# Using a logical expression to check if a variable is greater than 10
is_greater_than_10 <- score > 10

# Combining variables, functions, and operators in an expression to create a new vector
new_grades <- grades * 0.7 + 30

2.1.2.2 Python example

# Performing a mathematical expression and storing the result in a variable called "result"
result = 5 + 10

# Combining variables and strings in an expression to create a new string
greeting = "Hello " + name + ", your age is " + str(age)

# Using a logical expression to check if a variable is greater than 10
is_greater_than_10 = score > 10

# Combining variables, functions, and operators in an expression to create a new list
new_grades = [x * 0.7 + 30 for x in grades]

2.1.3 Statements

2.1.3.1 R example

# Assigning a value to a variable and then printing it
age <- 25
print(age)
[1] 25
# Using an if statement to control program flow
if (age > 18) {
  print("You are an adult")
} else {
  print("You are a minor")
}
[1] "You are an adult"
# Using a for loop to iterate over a vector and print each value
for (grade in grades) {
  print(grade)
}
[1] 80
[1] 90
[1] 85
[1] 92
[1] 87
# Defining a function and calling it with arguments
calculate_grade <- function(score) {
  if (score >= 90) {
    return("A")
  } else if (score >= 80) {
    return("B")
  } else {
    return("C")
  }
}

grade <- calculate_grade(score)
print(grade)
[1] "A"

2.1.3.2 Python example


# Assigning a value to a variable and then printing it
age = 25
print(age)

# Using an if statement to control program flow
25
if age > 18:
    print("You are an adult")
else:
    print("You are a minor")
    
# Using a for loop to iterate over a list and print each value
You are an adult
for grade in grades:
  print(grade)

# Defining a function and calling it with arguments
80
90
85
92
87
def calculate_grade(score):
  if score >= 90:
    return "A"
  elif score >= 80:
    return "B"
  else:
    return "C"

grade = calculate_grade(score)
print(grade)
A