Task 1

Data Science Basic Programming

Logo

1 Identifying Data Types

Identifying data types is the process of determining the type of value stored in a variable. In Python and R, different types of data exist, such as integers, floats, strings, booleans, lists (vectors in R), and dictionaries (named lists in R). Knowing the data type of a variable is important because it affects how the variable can be used in operations and functions.

To identify data types:
- In Python, the type() function is used.
- In R, the class() function is used.

Based on this:

  • a = 42 → Integer (int di Python, numeric di R)
  • b = 3.14 → Float (float di Python, numeric di R)
  • c = "Hello" → String (str di Python, character di R)
  • d = FALSE → Boolean (bool di Python, logical di R)
  • e = [1, 2, 3] → List (list di Python, vector di R)
  • f = {"name": "Azkya", "age": 25} → Dictionary (dict di Python, named list di R)

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] "numeric"
## [1] "numeric"
## [1] "character"
## [1] "logical"
## [1] "numeric"
## [1] "list"

2 Variables and Data Manipulation

Variables are used to store values in programming. In both Python and R, variables do not require explicit type declaration, meaning their types are assigned dynamically based on the value given.

Key Concepts:
1. Updating values – Variables can be modified by performing arithmetic or string operations.
2. String concatenation – Two or more strings can be combined into one.
3. Case conversion – Strings can be converted to uppercase or lowercase for formatting.

Create the following variables in Python and 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] 30
## [1] "Data Science"
## [1] "DATA SCIENCE"

Explanation:

  1. Updating Variables:
    • x <- x + 10 updates the value of x by adding 10 to its current value. Initially, x is 20, so after the update, x becomes 30.
    • print(x) displays the updated value, which is 30.
  2. String Concatenation:
    • paste(text1, text2) combines the two string variables, "Data" and "Science", with a space in between.
    • The result, "Data Science", is stored in text_combined and printed.
  3. Uppercase Conversion:
    • toupper(text_combined) converts the concatenated string "Data Science" to uppercase, resulting in "DATA SCIENCE".

3 Arithmetic Operations

Arithmetic operations are fundamental mathematical operations used to perform calculations on numerical values. Both Python and R support basic arithmetic operations, including addition, subtraction, multiplication, division, exponentiation, and modulo.

Given the following variables:

  • 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] 19
## [1] 11
## [1] 60
## [1] 3.75
## [1] 3
## [1] 50625
## [1] 3

4 String Operations

String operations allow manipulation of text data, such as changing case, extracting substrings, and finding string length. These operations are useful for formatting, searching, and processing text.

Given the following text:

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] "Hello"
## [1] 20
## [1] "hello, data science!"

5 Comparison and Logical Operators

Comparison operators are used to compare values, while logical operators are used to evaluate expressions that return TRUE or FALSE. These operators help in decision-making by determining conditions that control the flow of a program.

Given the following variables:

  • 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] FALSE
## [1] TRUE
## [1] TRUE
## [1] TRUE

6 Data Type Conversion

Data type conversion allows changing one data type to another. This is useful when working with different types of data, such as converting a string to a number for calculations or converting a number to a string for display purposes.

Given the following variables:

  • 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] 133
## [1] 45
## [1] "45"

7 Bonus Challenge

Create an interactive program that asks the user to input:

  1. Name
  2. Age
  3. Hometown Then, print the output as follows
## Enter your name:
## Enter your age:
## Enter your hometown:
## Hello  , you are  years old and from  .