Task 1
Data Science Basic Programming
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 (intdi Python,numericdi R)b = 3.14→ Float (floatdi Python,numericdi R)c = "Hello"→ String (strdi Python,characterdi R)d = FALSE→ Boolean (booldi Python,logicaldi R)e = [1, 2, 3]→ List (listdi Python,vectordi R)f = {"name": "Azkya", "age": 25}→ Dictionary (dictdi Python,named listdi R)
Questions:
- Identify the data type of each variable above.
- 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:
- Updating Variables:
x <- x + 10updates the value ofxby adding 10 to its current value. Initially,xis 20, so after the update,xbecomes 30.
print(x)displays the updated value, which is30.
- String Concatenation:
paste(text1, text2)combines the two string variables,"Data"and"Science", with a space in between.
- The result,
"Data Science", is stored intext_combinedand printed.
- 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:
- Compute the sum, difference, product, division, and modulo of a and b.
- Compute a raised to the power of b.
- 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:
- Check if x is greater than y.
- Check if x is less than or equal to y.
- Check if x is not equal to y.
- 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:
- Convert num_str to an integer and add 10.
- Convert num_float to an integer.
- 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:
- Name
- Age
- Hometown Then, print the output as follows
## Enter your name:
## Enter your age:
## Enter your hometown:
## Hello , you are years old and from .