Skip to main content

Python Data Types

 Welcome dear bloggers,

My name is Python International and this is going to be the first post a python lesson series. Today we are going to name all the python data types without analyzing them.

*This image does not contain all the types


So, python has 8 basic data types

  1. Numeric Types. They represents the data that has a numeric value and there are 3 categories:
    • Integer {Int}. These are only whole number like -2, -1, 0, 1, 2 etc.
    • Float {Float}. These are numbers with decimals like 5.5, 3.14 and even 1.0
    • Complex {Complex}. These are numbers with a real and an imaginary part, written  real + imaginaryj (Z = 3 + 4)

  2.  Text Types. They represent the data that have a text value.
    • String {str}.  A string is a collection of one or more characters put in a single quote, double-quote, or triple-quote. String variables can be declared either by using single or double quotes, for example hello = 'Hello from Me'  or  hello = "Hello from Me"

  3.  Sequence Types. A sequence is an ordered collection that holds multiple values. These values can be of the same type or different types, and sequences let you store and organize them efficiently.
    • List {list}. They should be used when you need to store multiple items under a single variable, and order matters or you need to modify the collection later. Moreover, they can be created using square brackets []. They allow you to:
      • Duplicate values.
      • They can hold a variety of data types, including integers, characters, floats, strings, and even objects.
      • You can mix different data types in one list, but it's usually better to keep the types consistent for clarity and good coding practices
        Example:  list = [1, "hello", 3.14, True, 7]
    • Tuple {tuple}. It is a immutable sequence. Once a tuple is created, you cannot change its contents. Like lists, tuples can hold different data types (integers, strings, etc.). Tuples are defined using parentheses (). Lastly they are usually useful for fixed collections of items. 
      • Example: my_tuple = (1, "apple", 3.14, True)
                        print(my_tuple[0])
    • Range {range}. It represents an immutable sequence of numbers, typically used for looping in for loops. It's commonly used when you want to work with a series of numbers but don't need to store them all in memory at once. Lastly, ranges are created using the range() function, which can take up to three arguments: start, stop, and step. :
  4. Mapping Type: This data type associate a value to a unique key. This data type is useful and efficient for quickly look up and access an item in a collection.
    • Dictionary {dict}. A dictionary in Python is an unordered collection of data values, used to store data values like a map. It is best to think of a dictionary as a set of key: value pairs
      • When to use: When you want to have a collection of items that have key-value pair, and you want to quickly look up/ manipulate the values based on keys or vice versa.
      • Create a dictionary:
        >>> example_dict = {'name': 'Thu', 'age': 25} 

  5. Set Types. A set in Python is an unordered collection of unique items. Sets are mutable, meaning you can add or remove elements after their creation. However, the elements of a set must be immutable (like numbers, strings, or tuples).
    • Set {set} . A set is an unordered collection with no duplicate element.
      • When to use: When you want to create a unique list of elements, and you may want to use mathematical operations on it like union, intersection, difference, and symmetric difference.
      • Example: my_set = {1, 2, 3, 4}
                        print(my_set) 
    • FrozenSet {frozenset}. Its a type of set that is immutable. Once created, the elements in a frozenset cannot be changed, meaning you cannot add or remove items from it. However, like regular sets, frozensets are unordered collections of unique elements.
      • Example: my_frozenset = frozenset([1, 2, 3, 4])
                         print(my_frozenset)

  6. Booleans {bool}. They represent one of two possible values: True or False. They are used for logical operations and conditions, such as in if statements, loops, or comparisons.

  7. Binary Types. They refer to data types that represent binary data—values that are composed of 0s and 1s. These are used for handling raw data, such as in file operations, network communication, or encoding/decoding tasks.
    • Bytes {bytes}. A bytes object is an immutable sequence of bytes (each byte is a value between 0 and 255). It's often used for binary data such as file contents, network data, or when you need to store raw bytes. You can create a bytes object using the b prefix or the bytes() constructor.
    • Bytearray {bytearray}. A bytearray is a mutable sequence of bytes. Unlike bytes, you can modify the contents of a bytearray after it has been created. It's often used when you need a mutable sequence of bytes to modify or manipulate binary data.
    • Memoryview {memoryview}. A memoryview allows you to access the internal data of bytes or bytearray without copying the data. It provides a way to view and manipulate slices of large binary data structures efficiently. This is useful when working with large datasets like arrays or buffers.

  8. None Types. None is a special data type that represents the absence of a value or a null value. It is often used to signify that a variable or function has no value assigned, or that a function explicitly returns no value.

Comments