Python Data Types

Example Data Type
x = "Hello World" str
x = 20 int
x = 20.5 float
x = True bool
x = None NoneType
x = ["apple", "banana", "apple"] list
x = ("apple", "banana", "cherry") tuple
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set

Exhibit 25.50 Commonly used Python data types.

Common Python data types are listed in Exhibit 25.50.

Data types for storing sequences of values:

  • Lists
    Lists are ordered, changeable, and allow duplicate values. They are versatile data structures ideal for storing ordered collections like shopping lists or student names, performing array-like operations for numerical calculations, and managing dynamic data due to their ability to add, remove, or modify elements as needed. For example:
    list1 = ["apple", "banana", "cherry"]
    list2 = [1, 5, 7, 9, 3]
    list3 = ["abc", 34, True, 40, "male"]
    You can access list items using their index (starting from 0):
    list1[0] # returns "apple"
    list1[1] # returns "banana"
    list1[2] # returns "cherry"
    To change the value of a specific item:
    list1[1] = "blackcurrant" # replaces "banana"
  • Tuples
    Ordered and unchangeable, tuples cannot be modified after creation. Tuples are ideal for situations requiring immutability, such as representing fixed data like coordinates, dimensions, or configuration settings, and are often more efficient than lists due to their immutability, making them faster to create and access.
  • Dictionaries
    Dictionaries store data values in key:value pairs. They are essential for storing data as key-value pairs, enabling efficient lookup operations by keys, and representing complex data structures like JSON objects or configuration files. For example:
    car1 = {
        "brand": "Ford",
        "model": "Mustang",
        "year": 1964
    }
    The car1 dictionary provides information about a specific car. You can access dictionary items using their keys, i.e.:
    car1["model"] # returns "Mustang"
    A dictionary like car1 can represent a row in a table listing cars. If you combine multiple dictionaries, such as car1, car2, car3, etc., into a tuple (car1, car2, car3), you create a table where each row (e.g., car1, car2) is a dictionary containing information about a specific car.
    cars = (car1, car2, car3)
  • Sets
    Unordered, unchangeable, and unindexed. Can add or remove items. Sets are commonly used for membership testing, efficiently checking if an element exists in a collection, removing duplicates to maintain unique values, and performing mathematical operations like union, intersection, and difference.

The len() function returns the number of items in a sequence or a collection. You can use this function to determine the length of lists, tuples, dictionaries or sets. For instance, len(list1) will return 3.

You can also use the len() function to determine the length of a string, as strings in Python are stored as sequences of characters.

Note in Exhibit 25.50, the use of brackets in the syntax, for creating these data types and assigning values to them. An alternative approach to creating these data types is through functions such as list(), tuple(), dict() and set().

You can also use these functions to manipulate data. For instance, in Exhibit 25.50, the set() function is used to convert a list into a set, which automatically removes duplicate elements. The list() function is then used to convert the set back into a list of unique elements from the original list.

numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
unique_numbers = list(set(numbers))
print("List with duplicates removed:", unique_numbers)
List with duplicates removed: [1, 2, 3, 4]

Exhibit 25.51 Code using functions set() and list() to remove duplicates from a list of numbers.


Previous     Next

Use the Search Bar to find content on MarketingMind.