## Complex Data Types

After investigating the most basic data types for variables, you can now proceed with a little bit more advanced data types.

• Tuple
• List
• Set
• Dictionary

### Tuple

Let’s start with tuples. They are the simplest composed data type in Python. By “composed” I mean that they can contain different values at the same time while without mixing them together. They are described within parentheses “( )” around the values, separated by commas. Tuples can contain any kind of data types, even other tuples, lists or dictionaries and you can mix the data types used within a tuple.

```rotation = (0.123, 0.426, 0.783)
greetings = ("Hello", "Good Morning")
employee = ("Steve", "Johnson", 42, ("glasses", "brown hair", "green eyes"), True)
```

If you want to access the individual elements of a tuple, you need to write the name of the tuple followed by square brackets “[ ]”. Inside the brackets, you give the index (i.e. position inside the tuple) with an integer starting with zero as the first element.

```pitch = rotation[0]
appearance = employee[3]
eye_colour = employee[3][2]
```

Good to know about tuples is, that their individual elements can’t be modified. Once created, they remain the same. If you want to change an element, you need to create a new tuple. Read more about tuples on the W3Schools website.

### List

Next, let’s talk about lists. A list is made to list things, similar to tuples. The main difference is, a list can be modified. They are described within square brackets around the values, separated by commas. Also here, the data types inside a list can be of any type including other lists.

```names = ["John", "Jane"]
position = [1.2, 3.0, 0.5]
matrix_2x2 = [[0, 1],[0, 1]]
```

If you have a list in a list, it might be interesting to write it like a table. (The same actually applies for tuples.)

```matrix_3x3 = [[0, 1, 1],
[0, 1, 0],
[1, 0, 1]]
```

A list can be modified which means that you can access each element of the list and change it without the need to make a complete new list. Again, the indexes are given in square brackets and start with zero.

```matrix_3x3[0][0] = 0
names[1] = "Sarah"
position[2] = position[2] + 1
```

If you want to replace an element, the above method is fine ,but what about extending the list by adding an element at the bottom? There is a function built into the list. You can use it with:

```list_name.append()
```

The element that you want to add to the list needs to be specified in the parentheses at the end.

```names.append("George")
```

Lists have even more functions that ca be accessed like this such as “remove” or “insert”. Read more about lists on the W3Schools website.

### Set

A set is similar to a list. The main difference is, that a set is not having any order. The are described within curly brackets “{ }” around the values, separated by commas.

```books = {"Sherlock Holmes", "Tom Sawyer", "Lord of the Rings"}
interesting_pages = {12, 23, 54, 62, 102}
```

In a list, the elements are neatly stored one after the other and their position remains the same. In a set, this is not a case. This means that the following sets are actually the same.

```books1 = {"Sherlock Holmes", "Tom Sawyer", "Lord of the Rings"}
books2 = {"Sherlock Holmes", "Lord of the Rings", "Tom Sawyer"}
books3 = {"Tom Sawyer", "Sherlock Holmes", "Lord of the Rings"}
```

As you can’t access the individual elements of a set, you can’t change them either. Though, you can add elements to the set.

```books.add("Moby-Dick")
```

The lack of a numbered structure also means that there is no way you can use an index to access the elements. What is the use of this then? Until now, you have not seen the necessary tools to use sets, but you can use sets to see if a value can be found within a set or you can apply an algorithm to each element of a set. Read more about sets on the W3Schools website.

### Dictionary

Dictionaries are comparable to sets. They also have no order so you can’t use an index to retrieve the elements. However, dictionaries have “keys” with which the elements can be accessed by. The are described in curly brackets starting with the name of the key in quotes followed by a colon “:” and then the value. If the value is a string, it is also inside quotes.

```vehicle = {
"type": "car",
"brand": "Fiat",
"model": "Punto",
"year": 2015,
"last_repair":{"what":"oil change", "when":2019}
}
```

Like the other complex data types, dictionaries can contain all other data types as well. You can access and change the value of an element by referring to its key. The same way you can add new keys to the dictionary.

```last_time_in_garage = vehicle["last_repair"]["when"]
vehicle["model"] = "Picanto"
vehicle["color"] = "red"
```

Dictionaries can be a good choice for storing data of a physical object, depending on the needs of the program. Read more about sets on the W3Schools website.

#### Note:

Sometimes you need to create a variable such as a list or a dictionary before you will know what you store inside it. In these cases, you can create an empty variable and fill it later. This can be done by leaving the respective brackets empty. Note that there is no use of creating empty tuples. This is also why it is not possible.

```list_entries = []
dictionary_entries = {}
```

Another thing that is good to know is that you can get the number of entries in a tuple, list, set or dictionary.

```length_of_tuple = len(tuple_entries)
length_of_list = len(list_entries)
length_of_set = len(set_entries)
length_of_dictionary = len(dictionary_entries)
```

This is everything about the standard data types that you need to know to have a solid foundation before you start with the actual programming part. The next step is to get to know the different types of operators to start calculating in Python.

Continue learning about arithmetic expressions or go back to revisit the basic data types.

## Variables

The first element that you need to know about programming is the concept of variables. A variable is basically a space in your computer’s memory to save data during run time (i.e. while the program is running). This is similar to a human that is thinking something. The fact that he is keeping the thought in the back of his mind is comparable to the program storing data in a variable.

## Variables

For making the concept a little bit more visual, you can think of a variable as something like a bucket. Inside this bucket, you can store data. The question is not, what is this data? And here comes the tricky part. In a bucket, you can fill whatever you want (even if it does not really fit inside and sticks a little bit out of the bucket). With a variable, you can not do the same as in programming languages like C or C++, every variable is made to store a specific type of data like numbers or words. (Python is a little bit less strict in this regards, but you should still know about this.)

This means that in C, if you want to store a number inside a variable, this variable needs to be made for that number type. You can’t simply store a word inside that variable. Also, it is not possible to store a number inside that variable if the number is too big. You could compare this like there were buckets especially made for numbers. Let’s say these buckets have a square shape. A square shaped number will fit inside the bucket, but a round shaped word will not fit inside as the bucket is not made for round shaped objects.

One thing that you also should notice is that the variable must exist before you store data inside of it. Similarly, you first need to have a bucket to place something inside the bucket. This is usually called, the variable is initialized and this is also where you define what type of data it is allowed to store.

The ‘variable for each type’ thing holds true for most languages out there, but Python is a little different. Remember what the programming languages guide said about Python? Python is doing some things for you, in this case, it will allow to fit every data type into every variable no matter what. It just doesn’t care about these rules as this is all happening in behind the scenes. Also, Python does not care about the initialization thing either. (What a rebel.)

## Using Variables

An important thing about variables: each variable has a name. This name must be unique inside the scope of that program as otherwise, the computer might confuse the variables if they are all called the same name. The syntax is generally similar to the following:

```variable_name = variable_value
_variable = _value
```

Variables are often named with the words all lower case and separated by an underscore. The name can be anything you like but there is one rule: it must start with either a letter or an underscore. This means it can’t start with a number or a special character in front.

You can also assign the value of a variable to another variable.

```variable1 = something
variable2 = variable1
```

In this example, the variable called `variable1` will have the value `something` and the `variable2` will have the same value as `variable1`, thus `something`.

Continue learning about basic data types or go back to a general explanation about programming languages.

## Basic Data Types

Now, let’s discover the most basic data types that are available out there. These are:

• Integers
• Floating Point Numbers
• Boolean Values
• Strings
• Characters

### Integers

Let’s start with Integers, or Int for short. An Integer is nothing else but a whole number (i.e. 0, 1, 2, 50, -73, -101). It is usually used for counting. Or quantifying in discrete steps. In Python you assign an integer as follows:

```value = 15
number_of_wheels = 4
height_in_cm = 10
x = 7
```

### Floats

Next, a Floating Pint Number, or Float for short, is a number with a decimal part (i.e. with a decimal point). Floats are usually used to store continuous information such as measurement values. The decimal part still can be zero though. (Note that the decimal point is a dot and not a comma.)

```temperature = 52.6
voltage = 4.692
a = 3.0
```

### Booleans

The next category are Booleans, or Bools for short. A bool can only contain one of two values: True or False. They are usually used to describe the state of a binary property such as if a condition is true or if a device is turned on.

```battery_is_full = True
motor_is_on = False
```

### Strings

The next type are Strings, sometimes called Str. A string is basically a word or even a sentence. These words can be anything you like. Also, because Python would not know if you want to assign a word into a string variable or the value of another variable, you must specify the value inside quotes (‘ ‘) or double quotes (” “). There is no difference between the different types of quotes, but you are allowed to use a quote inside a sentence if it is the different kind of quotes that you use to specify the string (like ” it’s “).

```greeting = "Hello Dave's RoboShack!"
robot_name = 'Shacky'
```

### Characters

The last type is called a Character, or Char for short. It has pretty much the same properties like a string except that a char is maximum one letter long. Basically it is a string with one single letter.

```initial = "D"
subject = 'B'
```

In Python, there is not really a use for chars. In C for example, a string is nothing else than a list of chars. (Lists are covered in the next part of this guide as they are already a little bit more complex data types.)

#### Note:

It is pretty easy to confuse strings and numbers. How? Let’s see:

```a = 2
b = 3.5
c = '32'
d = "2.3"
```

In the examples above, it might seem clear which of these variables are strings and which are ints or floats due to the quotes. But if you work with them in a program, it is quite easy to oversee this which can turn into quite some headaches when debugging. Even though it looks like they are the same, they are not. Later, you will see how you can convert a string into a number type and the other way around to work with them.

Continue learning about complex data types or go back to revisit variables.