PythonData Types

A guide to Python's data types

A quick and easy cheat sheet for data types in python

Integers and floats

The int data type contains an integer / whole number:

print(10)

Floats contain numbers with decimal points:

print(5.7)

To convert an int to a float or vice-versa you would use the int and float functions, like this:

print(int(5.7))
print(float(10))

Strings

Strings are collections of one or more characters enclosed in single or double quotes:

print('M')
print("Hello World!")

To create a multi-line string, use three single or double quotes:

multi_line = """this is
a multi-line
string"""

Lists

Lists, just like strings are collections:

letters = ["a", "b", "c"]

These collections can contain values of different types:

letters_and_numbers = ["a", 2, "c", 4.3, "d", 6.2]

To get the values of a list you would place square brackets after the list or list variable and specify the index of the item you want to get inside them:

print(letters[0])

List indexes begin with zero, so in this example letters[0] would return a and letters[1] would return b.

You could also get a string character at a specific index using the same syntax:

name = "Alex"
print(name[0], name[1], name[2], name[3], sep = ":")

Tuples

Tuples are immutable lists, which means you can't modify the values of a tuple after you've defined it.

tu = (12, 9.3, "Alex", "Adam")
print(tu[0], tu[1])

To get the length of a list, string or tuple you would use the len function, like this:

print(len(letters_and_numbers))
print(len(name))
print(len(tu))

Sets

Another "list-like" data type is set, which allows you to define lists that can't contain duplicate values. Unlike other data types, a set isn't defined by a specific syntax, but rather a class type.

print(set(["a", "a", "b", "c", "c", "c"]))
# output: set(["a", "b", "c"])

And they also allow you to get rid of duplicates from existing lists:

stuff = [12, 9.3, "World", "World"]
stuff = set(stuff)
# output: set([12, 9.3, "World"])

Dictionaries

And the final data type I'll be covering in this lesson is dict, which is short for "dictionary".

This data type stores key: value pairs which are separated by a comma, the keys are strings and the values can be of any type.

For example, let's create a simple dictionary that contains every data type we've learned about so far:

types = {
  "integer": 15,
  "float": 23.6,
  "string": "Hello World!",
  "multi-line-string": """this is a
  multi-line string""",
  "list": ["a", "b", "c"],
  "tuple": ("John", "Jones", "James", "Jim"),
  "set": set(["a", "b", "c", "c", "c"])
}

To get a value from a dictionary, you would - once again - use the square brackets, but this time instead of specifying a numbered index you would specify the key of the value you want, for example, let's print the string key, which has a value of Hello World!:

print(types["string"])

To delete a key from a dictionary, you would use the del keyword:

del types["float"]
print(types)

Type conversion

As demonstrated in the "Integers and floats" section, you can use methods like str, list and tuple to convert an value from one type to another:

# Convert an integer into a string:
print(str(10))

# Convert a float (decimal) into a string:
print(str(10.1))

# Convert a string into a list:
print(list(name))

# Convert a tuple into a list:
print(list(tu))

# Convert a string into a tuple:
print(tuple(name))

# Convert a list into a tuple:
print(tuple(letters))