Use Livebook to open this notebook and explore new ideas.
It is easy to get started, on your machine or the cloud.
# Hello World!
Here's how you write a `Hello World` script in Elixir.
```elixir
IO.inspect("Hello World!")
```
<!-- livebook:{"output":true} -->
```
"Hello World!"
```
## Variable Assignment
Assigning a value to a variable is super simple in Elixir. You just write `my_var =` and whatever is on the right side of the `=` sign gets assigned to the variable. Here is how to assign all the different basic types to variables:
```elixir
name = "Peter" # String
age = 32 # Integer
age_hex = 0x20 # Integer in Hex notation
height = 190.47 # Float
height_sci = 1.9047e2 # Float in scientific notation
adult? = true # Boolean
status = :active # Atom
address = nil # The 'None/null/Nil' value
IO.inspect([name, age, age_hex, height, height_sci, adult?, status, address])
```
<!-- livebook:{"output":true} -->
```
["Peter", 32, 32, 190.47, 190.47, true, :active, nil]
```
Variables are **immutable** but can be reassigned. This means that the memory holding the old value will not be overwritten; instead, new memory is allocated to hold the new value while the old value remains until garbage collected. Even if you modify a variable, it is not updated in place but copied to a new chunk of memory. This might seem wasteful, but it prevents different processes from modifying the same variable.
```elixir
age = 32
age = 21
IO.inspect(age)
```
<!-- livebook:{"output":true} -->
```
21
```
You can ignore assignments by prefixing the variable name with an underscore:
```elixir
_ = 30
_result = 30 + 32
```
## String Interpolation and Concatenation
You can interpolate and concatenate strings like this:
```elixir
# Interpolate a string with values
name = "Peter"
age = 32
IO.inspect("My name is #{name} and I am #{age} years old")
# Concatenate two strings
IO.inspect("But my real name is " <> "Batman 🦇")
```
<!-- livebook:{"output":true} -->
```
"My name is Peter and I am 32 years old"
"But my real name is Batman 🦇"
```
See source