The Four Common Data Types in Python
When working with data science projects in Python, it's essential to understand the four common data types that are frequently used. These data types include numeric, logical, character, and complex numbers.
Numeric Data Type
-----------------
In Python, the first data type is the numeric data type. This includes integers, floats, and more. For example, if we create a variable `a` and assign it a value of 2, we can check its data type by using the `class()` function. As shown in the script, `class(a)` returns `numeric`, indicating that `a` is indeed an integer.
Similarly, if we create another variable `b` and assign it a value of 3.14, which is a floating-point number, we can check its data type using the same method. The result is again `numeric`. However, when we use the `l` suffix after the number, such as in `5L`, it changes the data type to integer.
Logical Data Type
-----------------
The second data type is the logical data type, which includes boolean values (True or False) and more complex logical expressions. For instance, if we create a variable `e` with the value `true` (in all caps), we can check its data type using the `class()` function.
Similarly, if we create another variable `f` with the value `2`, which is assigned to `a`, we can assign `f` to have the same value as `a`. However, when we compare `a` and `f` using the equality operator (`==`), the result is indeed `True`.
Character Data Type
-----------------
The third data type is the character data type. This includes strings of single characters, such as letters or numbers.
For example, if we create a variable `g` with an "a" inside quotation marks, we can check its data type using the `class()` function. The result is indeed `character`. However, without the quotation marks, `g` is simply a variable.
Notice that when we assign a value to `g`, it takes on the same value as the variable `a`, which is 2 (without the quotation marks). This means that if we use single quotation marks or double quotation marks inside the character data type, it treats them as literal characters rather than values.
Complex Data Type
----------------
The fourth and final data type in Python is the complex data type. This includes numbers with imaginary parts, such as `5 + 5i`.
For example, if we create a variable `m` with the value `5 + 5i`, we can check its data type using the `class()` function.
R Programming Language
----------------------
In R, the four common data types are also frequently used. When launching the R command prompt, you can use the keyboard shortcut `Ctrl + L` to clear the screen before starting your code.
To check the data type of a variable in R, you can use the `class()` function. For instance, if we create a variable `a` with the value 2, we can use `class(a)` to confirm that its data type is indeed numeric.
Similarly, if we create another variable `b` with the value 3.14, which is a floating-point number, we can use `class(b)` to confirm that its data type is again numeric.
Using the integer suffix `L` after the number changes the data type to integer, as shown in the example using the variable `c`.
Logical Data Type
-----------------
R also supports logical data types, which include boolean values (TRUE or FALSE). For instance, if we create a variable `e` with the value `TRUE`, we can use `class(e)` to confirm that its data type is indeed logical.
Similarly, if we create another variable `f` with the value 2, which is assigned to `a`, we can assign `f` to have the same value as `a`. However, when we compare `a` and `f` using the equality operator (`==`), the result is indeed `TRUE`.
Character Data Type
-----------------
R also supports character data types, which include strings of single characters or multiple words.
For example, if we create a variable `g` with an "a" inside quotation marks, we can use `class(g)` to confirm that its data type is indeed character. However, without the quotation marks, `g` is simply a variable.
Notice that when we assign a value to `g`, it takes on the same value as the variable `a`, which is 2 (without the quotation marks). This means that if we use single quotation marks or double quotation marks inside the character data type, it treats them as literal characters rather than values.
Complex Data Type
----------------
R also supports complex data types, which include numbers with imaginary parts. For instance, if we create a variable `m` with the value `5 + 5i`, we can use `class(m)` to confirm that its data type is indeed complex.
By understanding these four common data types in Python and R, you'll be better equipped to work with different types of data and perform more efficient coding.