YAML a strict superset of JSON, so anything written in JSON can be parsed to YAML. It’s mostly used for configuration files in projects and it’s super easy to understand and read the code. The file extension for YAML files is .yaml or .yml In this tutorial, we are going to learn about different data types that are present in YAML and work with YAML in Python. By the end of this tutorial, you will be able to understand the YAML and its syntax. The YAML follows indentation syntax similar to Python. But, it doesn’t allow tab (remember it while writing YAML files) for indentation.

Setup

Install a Python module called the pyyaml to work with YAML files.

Copy and paste the following code in a file and save it as yaml_script.py

We’ll use the above script to convert the YAML code to Python and check different data types.Create a file called learn_yaml.yaml and practice different examples in it, that we are going to discuss in this tutorial.

Without further ado let’s jump into the data types section of YAML.

Data Type in YAML

Everything in the YAML is a key-value pair. We have a different name for key-value pairs in different programming languages like dictionaries, hashes, objects, etc.., These are building blocks of YAML. The keys can be strings(quoted or normal), floats, or integers (Support may change in a future update). And values can be of any data type that YAML supports. Let’s see different data types presents in YAML.

Numbers

YAML supports integers, floating numbers, and exponential floating numbers. When you evaluate the above code with Python script you will get the result as follows. We can represent the values in different number systems like decimal, octal, and hexadecimal.

The leading zero(0) represents it’s an octal number.The prefix 0x represents its hexadecimal value.

See the example below. We can see the converted decimal values of octal and hexadecimal value by running our Python script. You should see the exact output as follows. Another exciting thing about YAML is that we can represent NAN(Not A Number) and Infinity. Run the Python script, you will see converted values of NAN, and inf to Python. That’s it for the numeric types in YAML.

Strings

Strings in YAML can be represented with or without quotes. Both are similar. Unlike JSON there is no strict rule to put every string in quotes. But, if we need to use the escape sequences, then we must use double-quotes. Let’s see some examples of strings. The newline character in the section key-value pair works as expected. As we already said, we need to use double quotes to work with escape sequences. We have used double quotes in the next key-value pair and it works as expected. Interpret the above YAML code with our Python script. You will get the result as follows. There two special characters in YAML that we can use to write multiple sentences as value to a key. Let’s say we have to split a long sentence into multiple lines. In this type of scenario, we can use a fold (greater than >) or block (pipe |) character to write multiple lines. What’s the difference between fold and block characters? Coming to it. Fold character won’t interpret the newlines, whereas block character does. Let’s see the examples. Run the Python script, then you will see the difference between the fold and block characters. And don’t forget to use indentation.

Booleans

In YAML, we can represent the boolean value True and False in three different ways. Look at them.

The values True, On, and Yes are considered as True in YAML.The values False, Off, and No are considered as False in YAML.

If you interpret the above YAML code, then you will get the first 3 key values as True and the next 3 key values as False.

Null

YAML supports the null value similar to JSON. We can use the keyword null or the symbol tilde(~) to define the null value in YAML. YAML got pretty alternatives right? Yeah, they are kind of cool. Run the Python script. You will get both values as None as Python uses None instead of the null keyword.

Arrays

We can specify the arrays similar to Python in YAML. Or we can write all the array elements in separate lines preceded hyphen (-). Let’s see an example for each representation. In the above YAML code, we have used square brackets similar to Python lists. Let’s see another way to represent arrays in YAML (looks like markdown lists). If you use our Python script to interpret the above examples. You will get the output as follows. We can have dictionaries in the list not only strings, numbers, etc.., You will get an array of dictionaries if you interpret the above YAML code with our Python script.

Dictionaries

We have already seen the syntax of dictionaries in the above examples. To quickly recap dictionaries are key-value pairs. We can have any valid data type as a value to the key. YAML even supports the nested dictionaries. Let’s see an example. Interpret the above code you will see the same result as follows. Do you know you can convert a list into a dictionary in Python?

Set

YAML supports another data type called set. Set contains unique values similar to Pythons’ set data type. Set items are preceded by the question mark (?) like list items preceded by hyphens (-). We need to mention that the data type is set using !!set after the set name. Look at the following example. As set contains only unique values, you won’t get 2 two times when you interpret the above YAML code with our Python script. Let’s see the result. We can also represent the set similar to Python syntax as follows. You will get the exact output as the above example. That’s it for the data types in YAML. Let’s see some extra features in YAML.

Comments

YAML supports comments. It’s great. We can write comments in YAML starting with a hash (#) symbol. If you interpret the above YAML code, then you will get an empty key with a null value. YAML doesn’t support multi-line comments. We have to write multiple lines starting with a hash for multi-line comments similar to Python.

Anchors

Anchors allow us to copy the content of a key wherever we want in the entire document. This is very handy if you like to duplicate some content in the document. To use anchors, we have to define a name for it as a variable name in programming languages. And then we can use it across the document wherever we want. We can define anchor name using & and use it with *. Let’s see an example. In the above example, we have used a duplicate_data anchor to copy the value of the data key. Both the keys contain the same values if you interpret the above YAML.

Conclusion

I hope you got a good understanding of YAML. Now, you can use YAML in your next project config file. You can refer to the YAML official website to get more advanced stuff. Happy coding 🙂

Introduction to YAML in Python for Beginners - 2Introduction to YAML in Python for Beginners - 71Introduction to YAML in Python for Beginners - 90Introduction to YAML in Python for Beginners - 39Introduction to YAML in Python for Beginners - 61Introduction to YAML in Python for Beginners - 73