= 10 # Integer
x = 3.14 # Float
y = "Alice" # String
name = True # Boolean is_valid
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
1.2.2 R Code
<- 10 # Integer
x <- 3.14 # Numeric
y <- "Alice" # Character
name <- TRUE # Logical is_valid
1.3 Basic Data Manipulation
Operations on variables such as reassignment, updating values, and basic computations.
1.3.1 Python Code
= x + 5 # Updating value
x = name + " Johnson" # String concatenation
name print(name) # Output: Alice Johnson
1.3.2 R Code
<- x + 5 # Updating value
x <- paste(name, "Johnson") # String concatenation
name 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
= 10
a = 3
b 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
<- 10
a <- 3
b 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
= "Hello, World!" # Variable declaration
text 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
<- "Hello, World!" # variable declaration
text 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
= 10
x = 5
y 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
<- 10
x <- 5
y 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
= "100"
num_str = int(num_str) # Convert string to integer
num print(num + 10) # Output: 110
= 10.5
num_float = int(num_float) # Convert float to integer
num_int print(num_int) # Output: 10
1.7.2 R Code
<- "100"
num_str <- as.numeric(num_str) # Convert string to number
num print(num + 10) # Output: 110
<- 10.5
num_float <- as.integer(num_float) # Convert float to integer
num_int 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
= 42
a = 3.14
b = "Hello"
c = FALSE
d = [1, 2, 3]
e = {"name": "Alice", "age": 25} f
Questions:
- Identify the data type of each variable above.
- Print the data type of each variable using
type()
(Python) andclass()
(R).
1.8.2 Variables and Data Manipulation
Create the following variables in Python and R:
# R
= 20
x = 5
y = "Data"
text1 = "Science" text2
Questions:
- Update the value of
x
by adding10
.
- Concatenate
text1
andtext2
into"Data Science"
.
- Convert the concatenated text to uppercase.
1.8.3 Arithmetic Operations
Given the following variables:
# R
= 15
a = 4 b
Questions:
- Compute the sum, difference, product, division, and modulo of
a
andb
.
- Compute
a
raised to the power ofb
.
- Create a new variable
c = a / b
and convert it to an integer.
1.8.4 String Operations
Given the following text:
# R
= "Hello, Data Science!" text
Questions:
- Extract the first 5 characters from the text.
- Count the number of characters in the text.
- Convert the text to lowercase.
1.8.5 Comparison and Logical Operators
Given the following variables:
# R
= 7
x = 10 y
Questions:
- Check if
x
is greater thany
.
- Check if
x
is less than or equal toy
.
- Check if
x
is not equal toy
.
- Evaluate the expression
(x > 5) AND (y < 20)
.
1.8.6 Data Type Conversion
Given the following variables:
# R
= "123"
num_str = 45.67 num_float
Questions:
- Convert
num_str
to an integer and add10
.
- Convert
num_float
to an integer.
- Convert the converted
num_float
back to a string.
1.9 Bonus Challenge🚀
Create an interactive program that asks the user to input:
- Name
- Age
- 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. 😊