VMware ESXi and vSphere Cluster Management

Python Variable Data Types: Numbers, Strings, Lists, Tuples, and Dictionaries

Learn Python data types with practical examples of numbers, strings, lists, tuples, dictionaries, indexing, mutability, and type inspection.

A variable is a name that refers to a value. Every value in Python has a data type, which classifies what the value represents and determines which operations make sense for it.

For example, 5 is a number, while "5" is text. They look similar when displayed, but Python treats them differently: numbers can be used in arithmetic, while strings are sequences of characters.

Python uses dynamic typing. A variable name is not permanently restricted to one type. The same name can refer to values of different types at different times:

value = 5
value = "now this is text"

The value changed from an integer to a string. This is valid Python, although using clear names and consistent types usually makes programs easier to understand.

Five Introductory Python Data Type Categories

This lesson focuses on numbers, strings, lists, tuples, and dictionaries. Numbers and strings are usually individual values. Lists, tuples, and dictionaries are collections that group multiple values under one variable name.

Type category | Example literal | Stores | Access method | Mutable? | Typical use

Numbers | 5, 19.99, 2 + 3j | Numeric information | Use the value directly | Depends on numeric object; numbers are treated as immutable | Quantities, measurements, calculations

String | "hello" | Text characters in order | Numeric index, such as text[0] | No | Names, messages, labels

List | [5, 3, 2] | An ordered collection | Numeric index, such as values[0] | Yes | Changeable sequences

Tuple | (2, 4) | A fixed ordered collection | Numeric index, such as point[0] | No | Coordinates and other fixed sequences

Dictionary | {"color": "blue"} | Key-value pairs | A key, such as item["color"] | Yes | Labeled attributes and records

Numbers

Numbers represent numeric information. The essential numeric types for beginners are integers and floating-point numbers. Python also supports complex numbers.

Integers

An integer is a whole number, such as 5, 0, or -12. Assign an integer to a variable like this:

x = 5
print(x)
print(type(x))

type(x) identifies the type of the value referred to by x. In this example, it reports int.

Floating-point numbers

A floating-point number has a fractional component, such as 3.14 or 19.99:

price = 19.99
print(type(price))

Python reports this type as float. Floating-point values are useful for measurements and other values that commonly include fractions.

Complex numbers

A complex number contains a real component and an imaginary component. Python writes the imaginary component with j:

signal = 2 + 3j
print(type(signal))

Complex numbers are mainly used in specialized mathematics, engineering, and scientific programming.

Modern Python numeric terminology

Term | Status in Python 3 | Teaching guidance

int | The integer type; supports arbitrarily large whole numbers | Use for whole-number quantities

float | Floating-point type | Use for values with fractional parts

complex | Complex-number type using j | Use when real and imaginary components are needed

long integer | Historical Python 2 terminology; not a separate Python 3 type | In Python 3, ordinary int handles very large integers, with no L suffix

Strings

A string is an ordered sequence of characters representing text. String literals can use single quotes or double quotes:

message = "This is text"
name = 'Ada'

The quotation marks tell Python where the text begins and ends. The quote characters themselves are not part of the stored string.

Text digits and numeric values have different types:

text_number = "5"
number = 5

print(type(text_number))  # str
print(type(number))       # int

text_number cannot be used as an integer without intentional conversion. For example, int("5") produces the integer 5.

String indexing

Strings are ordered, so each character has a numeric position called an index. Python uses zero-based indexing: the first character is at index 0, the second is at index 1, and so on.

message = "Python"
print(message[0])  # P
print(message[1])  # y

Strings are immutable, meaning their characters cannot be changed in place. An expression such as message[0] = "J" raises a TypeError. Create a new string when different text is needed.

Lists

A list is a mutable, ordered collection of values. Lists use square brackets and comma-separated elements:

my_numbers = [5, 3, 2, 1]
print(my_numbers[0])  # 5
print(my_numbers[1])  # 3

Like strings, lists use zero-based indexing. The first element is at index 0.

Lists are mutable, so their elements can be changed after creation:

my_numbers = [5, 3, 2, 1]
my_numbers[1] = 10
print(my_numbers)  # [5, 10, 2, 1]

A list can contain values of different types:

mixed = ["ready", 3, 2.5]

Python permits mixed lists, but use a consistent element type when the list represents one kind of thing. For example, a shopping list may contain product names, while a list of quantities may contain integers.

Tuples

A tuple is an immutable, ordered collection. Tuples commonly use parentheses and comma-separated elements:

coordinates = (2, 4)
print(coordinates[0])  # 2

Tuples use zero-based numeric indexes just like lists. Their elements cannot be changed after creation. A tuple is a good choice for fixed data, such as a coordinate or an RGB color that should remain together and unchanged.

days_of_week = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
rgb_color = (255, 128, 0)

One-item tuple syntax

A single-item tuple requires a trailing comma. Parentheses alone do not create the tuple:

single_value = (5,)
not_a_tuple = (5)

print(type(single_value))  # tuple
print(type(not_a_tuple))   # int

The comma is the important part of the one-item tuple syntax.

Dictionaries

A dictionary is a mutable collection of key-value pairs. Each entry has a key and its associated value. Dictionaries use curly braces, colons, and commas:

item = {"color": "blue", "size": "small"}
print(item["color"])  # blue

Unlike a list or tuple, a dictionary is not accessed by asking for the value at a numeric position. You retrieve a value using its actual key. In this example, "color" and "size" are keys.

Dictionary keys must be unique and must be usable as dictionary keys. Strings are a common beginner-friendly choice. Assigning a value to an existing key updates it; assigning a new key adds an entry:

item = {"color": "blue", "size": "small"}
item["size"] = "medium"
item["price"] = 19.99
print(item)

Dictionaries are useful for labeled real-world objects. A product record might use keys such as "name", "price", and "in_stock"; a user profile might use "username" and "email".

List vs Tuple vs Dictionary

Feature | List | Tuple | Dictionary

Order and positional access | Ordered; uses numeric indexes | Ordered; uses numeric indexes | Uses keys for lookup rather than positional indexes

Mutability | Mutable | Immutable | Mutable

Syntax delimiters | Square brackets: [ ] | Parentheses: ( ) | Curly braces with key-value pairs: { }

How values are retrieved | values[0] | values[0] | values["key"]

Best use case | A changeable ordered collection, such as a shopping list | A fixed sequence, such as coordinates | Labeled attributes, such as a product record

Choose a list when the collection may change. Choose a tuple when the values form a fixed sequence. Choose a dictionary when names or labels are more meaningful than positions.

Inspecting Types and Accessing Values

Use the built-in type() function to inspect a value or variable:

x = 5
message = "hello"
my_numbers = [5, 3, 2, 1]
coordinates = (2, 4)
item = {"color": "blue"}

print(type(x))
print(type(message))
print(type(my_numbers))
print(type(coordinates))
print(type(item))

Valid access expressions for the introductory types include:

message[0]       # first character of a string
my_numbers[0]    # first list element
coordinates[0]   # first tuple element
item["color"]    # value associated with the "color" key

Invalid access produces an error. A sequence index must be between 0 and its length minus one. A dictionary lookup must use an existing key unless you deliberately handle missing keys.

Common Problems

Confusing text digits with numbers

"5" is a string and 5 is an integer. Combining them in arithmetic can produce a TypeError. Convert intentionally when appropriate, for example with int("5").

Forgetting zero-based indexing

Using index 1 retrieves the second item, not the first. The first item in a string, list, or tuple is at index 0.

Using an invalid sequence index

An index outside the valid range raises IndexError. For a sequence of length four, valid indexes are 0, 1, 2, and 3.

Trying to modify an immutable value

Assigning to an indexed character in a string or an indexed element in a tuple raises TypeError. Create a new string or tuple, or use a list when in-place changes are required.

Looking up a dictionary by position

Dictionaries are accessed with keys, not list-style positions. Use item["color"], not a numeric position. A nonexistent key raises KeyError. Check the available keys or use item.get("color") when an absent key is expected.

Expecting a separate long type

Python 3 does not have a separate long type or require an L suffix. Use int for both ordinary and arbitrarily large whole numbers.

Choosing a Type

  • Use an integer for a whole-number quantity, such as quantity = 5.
  • Use a float for a value with a fractional component, such as price = 19.99.
  • Use a string for text, such as a person's name.
  • Use a list for an ordered collection that may change, such as a shopping list.
  • Use a tuple for an ordered collection intended to remain fixed, such as an RGB color or coordinate.
  • Use a dictionary for an object described by labeled properties, such as a product.

Practice Example

quantity = 5
name = "Notebook"
shopping_list = ["Notebook", "Pen"]
location = (2, 4)
product = {
    "name": "Notebook",
    "price": 4.50,
    "in_stock": True,
}

print(type(quantity))
print(name[0])
print(shopping_list[1])
print(location[0])
print(product["price"])

This example uses each category for a suitable purpose: a number for quantity, a string for text, a list for changeable items, a tuple for fixed coordinates, and a dictionary for labeled product information.

Key Points

  • A variable is a name that refers to a value, and every value has a data type.
  • The type determines what information a value represents and which operations are appropriate.
  • Python is dynamically typed, so a name can later refer to a value of another type.
  • Integers, floats, and complex numbers represent numeric information.
  • Strings represent text and are immutable ordered sequences.
  • Lists are mutable ordered collections and use numeric indexes.
  • Tuples are immutable ordered collections and use numeric indexes.
  • Dictionaries are mutable key-value collections and use keys for lookup.
  • Use type() to inspect a value's type.