Data Types

A variable can store various types of data, called a data type. Lets introduce some of the basic types we’ll be looking at.

Integers

An integer is a whole number, and can be positive or negative. Integers can usually be of unlimited length.

score = 45

Floating Point Numbers

A floating point number, sometimes called a real number, is a number that has a decimal point.

temperature = 44.7

Strings

In Python code, a string must be enclosed in quotes “…” or ‘…’

name = “John Myers”

Sets

A set is an unordered collection of unique items enclosed in curly braces { }. Sets can contain different types.

You can create a set like this:

setName = {1, 6, 2, 9}

Tuples

A tuple is similar to a list and is a sequence of items each identified by an index. As with lists, the index starts with 0 not 1.

In contrast to lists, items in a tuple can’t be changed once assigned and tuples can contain different types of data.

To create a tuple enclose the items inside parentheses ( ).

userDetails = (1, ‘John’, ‘123 May Road’)

Dictionaries

A dictionary is an unordered collection of items, each identified by a key.

To create a dictionary, enclose the items inside braces { }. Identify each item with a key using a colon.

dictionary = { 1: ‘Dave’,
2: ‘Jo’
3: ‘Jane’
}

To read a value, put the key in square brackets.

print (dictionary[1])

Read page 28 – 34 of the textbook “Python for Beginners”.