1  Basic Programming

1.1 Data Types and Basic Operations

Every programming language has different data types that define the nature of stored values. The fundamental types in Python and R include:

Data Type Python Example R Example Description
Integer (int) x = 10 x <- 10 Whole numbers
Float (double in R) y = 3.14 y <- 3.14 Decimal numbers
String (chr in R) name = "Alice" name <- "Alice" Text data
Boolean (logical in R) is_valid = True is_valid <- TRUE True or False
List (Vector in R) numbers = [1, 2, 3] numbers <- c(1, 2, 3) Ordered collection of values
Dictionary (Named List in R) person = {"name": "Bob", "age": 25} person <- list(name="Bob", age=25) Key-value storage

1.2 Variable Declaration

In both Python and R, variables do not need explicit declaration.

1.2.1 Python Code

x = 10             # Integer
y = 3.14           # Float
name = "Alice"     # String
is_valid = True    # Boolean

1.2.2 R Code

x <- 10            # Integer
y <- 3.14          # Numeric
name <- "Alice"    # Character
is_valid <- TRUE   # Logical

1.3 Basic Data Manipulation

Operations on variables such as reassignment, updating values, and basic computations.

1.3.1 Python Code

x = x + 5                       # Updating value
name = name + " Johnson"        # String concatenation
print(name)                     # Output: Alice Johnson

1.3.2 R Code

x <- x + 5                      # Updating value
name <- paste(name, "Johnson")  # String concatenation
print(name)                     # Output: Alice Johnson

1.4 Basic Arithmetic Operations

Arithmetic operations are fundamental for numerical computations.

Operation Python Example R Example Description
Addition (+) a + b a + b Adds two numbers
Subtraction (-) a - b a - b Subtracts one number from another
Multiplication (*) a * b a * b Multiplies two numbers
Division (/) a / b a / b Divides one number by another
Exponentiation (**) / (^ in R) a ** 2 a ^ 2 Raises a number to a power
Modulo (%) a % b a %% b Returns remainder of division

1.4.1 Python Code

a = 10
b = 3
print(a + b)   # Addition
print(a - b)   # Subtraction
print(a * b)   # Multiplication
print(a / b)   # Division
print(a ** 2)  # Exponentiation
print(a % b)   # Modulo

1.4.2 R Code

a <- 10
b <- 3
print(a + b)   # Addition
print(a - b)   # Subtraction
print(a * b)   # Multiplication
print(a / b)   # Division
print(a ^ 2)   # Exponentiation
print(a %% b)  # Modulo

1.5 String Operations

String operations are used to manipulate text data.

Operation Python Example R Example Description
Uppercase text.upper() toupper(text) Convert to uppercase
Lowercase text.lower() tolower(text) Convert to lowercase
Length len(text) nchar(text) Get string length
Substring text[0:5] substr(text, 1, 5) Extract part of string

1.5.1 Python Code

text = "Hello, World!"   # Variable declaration
print(text.upper())      # Convert to uppercase
print(text.lower())      # Convert to lowercase
print(len(text))         # Get string length
print(text[0:5])         # Slicing (first 5 characters)

1.5.2 R Code

text <- "Hello, World!"   # variable declaration
print(toupper(text))      # Convert to uppercase
print(tolower(text))      # Convert to lowercase
print(nchar(text))        # Get string length
print(substr(text, 1, 5)) # Extract first 5 characters

1.6 Logical and Comparison Operators

Logical and comparison operators help in decision-making.

Operator Description Python Example R Example
Equal (==) Checks if values are equal x == y x == y
Not Equal (!=) Checks if values are different x != y x != y
Greater (>) Checks if left value is greater x > y x > y
Less (<) Checks if left value is smaller x < y x < y
AND (and in Python, & in R) Both conditions must be true (x > 5) and (y < 10) (x > 5) & (y < 10)
OR (or in Python, | in R) At least one condition is true (x > 5) or (y > 10) (x > 5) | (y > 10)

1.6.1 Python Code

x = 10
y = 5
print(x == y)                # False
print(x != y)                # True
print(x > y)                 # True
print(x < y)                 # False
print((x > 5) and (y < 10))  # True
print((x > 5) or (y > 10))   # True

1.6.2 R Code

x <- 10
y <- 5
print(x == y)               # FALSE
print(x != y)               # TRUE
print(x > y)                # TRUE
print(x < y)                # FALSE
print((x > 5) & (y < 10))   # TRUE
print((x > 5) | (y > 10))   # TRUE

1.7 Data Type Conversion

Data type conversion allows changing one data type to another.

Conversion Python Example R Example Description
String to Integer int("100") as.numeric("100") Convert text to number
Integer to String str(100) as.character(100) Convert number to text
Float to Integer int(10.5) as.integer(10.5) Convert decimal to whole number

1.7.1 Python Code

num_str = "100"
num = int(num_str)               # Convert string to integer
print(num + 10)                  # Output: 110
num_float = 10.5
num_int = int(num_float)         # Convert float to integer
print(num_int)                   # Output: 10

1.7.2 R Code

num_str <- "100"
num <- as.numeric(num_str)       # Convert string to number
print(num + 10)                  # Output: 110

num_float <- 10.5
num_int <- as.integer(num_float) # Convert float to integer
print(num_int)                   # Output: 10

1.8 Practicum

1.8.1 Identifying Data Types

Determine the data types of the following variables in Python and R:

# R
 a = 42
 b = 3.14
 c = "Hello"
 d = FALSE
 e = [1, 2, 3]
 f = {"name": "Alice", "age": 25}

Questions:

  1. Identify the data type of each variable above.
  2. Print the data type of each variable using type() (Python) and class() (R).

1.8.2 Variables and Data Manipulation

Create the following variables in Python and R:

# R
 x = 20
 y = 5  
 text1 = "Data"
 text2 = "Science" 

Questions:

  1. Update the value of x by adding 10.
  2. Concatenate text1 and text2 into "Data Science".
  3. Convert the concatenated text to uppercase.

1.8.3 Arithmetic Operations

Given the following variables:

# R
 a = 15
 b = 4

Questions:

  1. Compute the sum, difference, product, division, and modulo of a and b.
  2. Compute a raised to the power of b.
  3. Create a new variable c = a / b and convert it to an integer.

1.8.4 String Operations

Given the following text:

# R
 text = "Hello, Data Science!"

Questions:

  1. Extract the first 5 characters from the text.
  2. Count the number of characters in the text.
  3. Convert the text to lowercase.

1.8.5 Comparison and Logical Operators

Given the following variables:

# R
 x = 7
 y = 10

Questions:

  1. Check if x is greater than y.
  2. Check if x is less than or equal to y.
  3. Check if x is not equal to y.
  4. Evaluate the expression (x > 5) AND (y < 20).

1.8.6 Data Type Conversion

Given the following variables:

# R
 num_str = "123"
 num_float = 45.67

Questions:

  1. Convert num_str to an integer and add 10.
  2. Convert num_float to an integer.
  3. Convert the converted num_float back to a string.

1.9 Bonus Challenge🚀

Create an interactive program that asks the user to input:

  1. Name
  2. Age
  3. Hometown

Then, print the output as follows:

"Hello [Name], you are [Age] years old and from [Hometown]."

Happy coding! 🚀🔥 If you need any help, feel free to ask Chat-GPT. 😊