Data Types

What is a Data Type?

A data type is, as the name suggests, a type of data. Giving each type of data it's own definition enables them to function differently from each other, and posses useful differences, like their own set of methods.

In Python, there are a few core types, with the ability to create your own through classes.

Boolean

A boolean is the most simplest form of data, only being able to represent two states; on and off.

To create a boolean, simply use the keyword "True" for a positive value and "False" for a negative value.

positive = True
negative = False

Integer

An integer is the type of a whole number.

To create an integer, simply use a number.

eight = 8
eighty = 80

Float

A float is the type of decimal numbers.

To create a float, use a number followed by a full-stop and another number.

loan = 3.50

String

A string is the type used for words and sentences.

To create a string, encapsulate text within quotations.

string = "Hello, World!"

List

A list stores a multitude of other types of data, or other lists. The order of items in a list will always persist and only change if you change it.

To create a list, you need to surround values followed by commas, with square brackets.

var = 3.50
values = ["Hello", 5, "World", var]

Set

A set is like a list, but can't store identical pieces of data. The order of sets is not permanent and they are likely to change order.

To create a set, surround values by curly braces and separate them by commas.

values = {5, "Hello"}

Dictionary

A dictionary is like a list, though it stores values as key/value pairs instead of just values.

To create a dictionary, surround values by curly braces, like a set, but attach a secondary value to the key value with a colon.

values = {"apples": 5, "oranges": 3}

Last updated