Basic data types
1. Basic data types
Data types are very important in Julia, and as you become more advanced, understanding them will help you write more efficient code.2. Types of data
Let's start with the basic types. So far, we have been working with numerical data. There are two major types of numerical data, integers, which are whole numbers, both positive and negative, and floats which are numbers with a decimal point. A number is still a float even if all the numbers after the decimal point are zeros. When you assign a value to a variable, Julia will store it as the appropriate data type. You can find the data type of a variable by using the typeof function. Here we print the types of x_int and x_float. Julia has stored them as types Int64 and Float64. The number 64 refers to the number of digital bits Julia uses to store the value.3. Other basic data types
The simplest data type is booleans, and these can be represented with only one bit of information. Booleans are values that can only be true or false, and in Julia, they are spelled with all lowercase letters. In the next chapter, we'll show how booleans can be used to make your scripts intelligent. Strings are another common data type and are Julia's way of storing and processing text. We'll cover more about manipulating strings in the next chapter too. Strings must be enclosed by double quotation marks, not single quotation marks.4. Other basic data types
Single quotation marks are reserved for the character data type, which is a single character surrounded by single quotations. Each item in a string is a character.5. Consequences of data types
Many operations depend on the data type. For example, if we take an integer and a float and multiply them together, the resulting data type will be a float. But if we multiply two strings, like in this example, Julia sticks them together to form a new string. Even though we use the same operation, it acts differently depending on the data type. This is known as multiple-dispatch and is one reason Julia is so powerful as a language.6. Consequences of data types
Some data types cannot be used together in an operation. In this example, Julia has no way to multiply an integer with a string, so it raises an error message. Because of this, we must be careful with which data types we use in our scripts.7. Converting between types
It is possible to convert between data types. If we want to convert an integer x into a float, we can use the Float64 function. When we print the type of the new variable, we see it is a float. Similarly, we can convert a float to an integer using the Int64 function.8. Converting between types
When converting a float to an integer, the float needs to have only zeros after the decimal. If it doesn't, then Julia will raise an error because it doesn't know what to do with the information after the decimal.9. Converting between types
We can convert a number into a string using the string function. This example creates a string with a 1 inside. Finally, we can convert strings into numbers. This is a little different than the previous examples. Here we use the parse function. The first item inside the parentheses is the target data type. The second argument is the string x.10. Converting between types
By replacing the first item with Float64, we can convert the string into a float instead.11. Summary of conversions
Here is a summary of data type conversions that you can refer back to when completing the exercises.12. Let's practice!
Now, let's practice.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.