Hello World

A sample Python program is shown here.

def greet(name):
    return f"Hello, {name}!"

if __name__ == "__main__":
    message = greet("world")
    print(message)

Run the program as below:

$ python hello.py
Hello World

A sample R program is shown here.

greet <- function(name) {
  paste("Hello,", name, "!")
}

message <- greet("world")
print(message)
Strings
str_single = "Hello"
str_multiline = """Multiline
string"""
print(str_multiline.upper())

Useful string methods:

name = "John Doe"
print(name.lower())  # john doe
print(name.split())  # ['John', 'Doe']
Numbers
integer = 42
floating_point = 3.14
complex_number = 2 + 3j

# Converting between types
float_to_int = int(3.7)  # 3

Working with NumPy for advanced number manipulation:

import numpy as np
array = np.array([1, 2, 3])
print(array.mean())  # 2.0
Strings
str_single <- "Hello"
str_multiline <- "Multiline\nstring"
toupper(str_multiline)

Useful string manipulation with stringr:

library(stringr)

name <- "John Doe"
str_to_lower(name)  # "john doe"
str_split(name, " ")  # List of "John" and "Doe"
Numbers
integer <- 42L
floating_point <- 3.14
complex_number <- complex(real = 2, imaginary = 3)

# Converting between types
float_to_int <- as.integer(3.7)  # 3

Working with vectors:

nums <- c(1, 2, 3, 4, 5)
mean(nums)  # 3
sum(nums)   # 15
Condition
day = "sunday"

if day in ["sunday", "saturday"]:
    print("Rest!")
elif day == "monday" and is_tired():
    print("Groan!")
else:
    print("Work!")

Example with try/except:

try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")
finally:
    print("Cleanup complete.")
Loops

For loop example:

for i in range(5):
    print(f"Iteration {i}")

While loop example:

count = 0
while count < 5:
    print(f"Count is {count}")
    count += 1
Condition
day <- "sunday"

if (day %in% c("sunday", "saturday")) {
  print("Rest!")
} else if (day == "monday") {
  print("Groan!")
} else {
  print("Work!")
}

Using tryCatch for error handling:

result <- tryCatch({
  10 / 0
}, warning = function(w) {
  "Warning occurred!"
}, error = function(e) {
  "Error occurred!"
}, finally = {
  print("Cleanup complete.")
})

print(result)
Loops

For loop example:

for (i in 1:5) {
  print(paste("Iteration", i))
}

While loop example:

count <- 0
while (count < 5) {
  print(paste("Count is", count))
  count <- count + 1
}
File Operations

Reading and writing text files:

with open("example.txt", "w") as file:
    file.write("Hello, File!")

with open("example.txt", "r") as file:
    content = file.read()
    print(content)

Handling CSV files with pandas:

import pandas as pd

# Reading a CSV file
data = pd.read_csv("data.csv")
print(data.head())

# Writing a CSV file
data.to_csv("output.csv", index=False)
File Operations

Reading and writing text files:

# Writing to a file
writeLines("Hello, File!", "example.txt")

# Reading from a file
content <- readLines("example.txt")
print(content)

Handling CSV files:

# Reading a CSV file
data <- read.csv("data.csv")
head(data)

# Writing a CSV file
write.csv(data, "output.csv", row.names = FALSE)

Working with Excel files using readxl and writexl:

library(readxl)
library(writexl)

# Reading Excel
data <- read_excel("data.xlsx")
print(data)

# Writing Excel
write_xlsx(data, "output.xlsx")