Elixir - Data Types

For using any language, you need to understand the basic data types the language supports. In this chapter, we will discuss 7 basic data types supported by the elixir language: integers, floats, Booleans, atoms, strings, lists and tuples.
Elixir, like any other programming language, supports both integers and floats. If you open your elixir shell and input any integer or float as input, it'll return its value. For example, 
42

When the above program is run, it produces the following result −
42

You can also define numbers in octal, hex and binary bases.
To define a number in octal base, prefix it with '0o'. For example, 0o52 in octal is equivalent to 42 in decimal.
To define a number in decimal base, prefix it with '0x'. For example, 0xF1 in hex is equivalent to 241 in decimal.
To define a number in binary base, prefix it with '0b'. For example, 0b1101 in binary is equivalent to 13 in decimal.
Elixir supports 64bit double precision for floating point numbers. And they can also be defined using an exponentiation style. For example, 10145230000 can be written as 1.014523e10
Atoms are constants whose name is their value. They can be created using the color(:) symbol. For example, 
:hello
Elixir supports true and false as Booleans. Both these values are in fact attached to atoms :true and :false respectively.
Strings in Elixir are inserted between double quotes, and they are encoded in UTF-8. They can span multiple lines and contain interpolations. To define a string simply enter it in double quotes −
"Hello world"
To define multiline strings, we use a syntax similar to python with triple double quotes −
"""
Hello
World!
"""
We'll learn about strings, binaries and char lists(similar to strings) in depth in the strings chapter.
Binaries are sequences of bytes enclosed in <<  >> separated with a comma. For example,
<< 65, 68, 75>>
Binaries are mostly used to handle bits and bytes related data, if you have any. They can, by default, store 0 to 255 in each value. This size limit can be increased by using the size function that says how many bits it should take to store that value. For example,
<<65, 255, 289::size(15)>>

Elixir uses square brackets to specify a list of values. Values can be of any type. For example,
[1, "Hello", :an_atom, true]

Lists come with inbuilt functions for head and tail of the list named hd and tl which return the head and tail of the list respectively. Sometimes when you create a list, it'll return a char list. This is because when elixir sees a list of printable ASCII characters, it prints it as a char list. Please note that strings and char lists are not equal. We'll discuss lists further in later chapters.
Elixir uses curly brackets to define tuples. Like lists, tuples can hold any value.
{ 1, "Hello", :an_atom, true 
A question arises here, - why provide both lists and tuples when they both work in the same way? Well they have different implementations.
Lists are actually stored as linked lists, so insertions, deletions are very fast in lists.
Lists are actually stored as linked lists, so insertions, deletions are very fast in lists.
Tuples on the other hand, are stored in contiguous memory block, which make accessing them faster but adds an additional cost on insertions and deletions.
Tuples on the other hand, are stored in contiguous memory block, which make accessing them faster but adds an additional cost on insertions and deletions.

    35 Lectures 
     3 hours 

 Pranjal Srivastava

    54 Lectures 
     6 hours 

 Pranjal Srivastava, Harshit Srivastava

    80 Lectures 
     9.5 hours 

 Pranjal Srivastava

    43 Lectures 
     4 hours 

 Mohammad Nauman
 Print
 Add Notes
 Bookmark this page