Main Menu

search

You are here

Python: Data Types

[last updated: 2022-10-26]
Python home page
Python: Programming
-----

(see below for methods to convert between data types)

  • Disclaimer: This page is currently a mess... I have python 3. These notes may not be correct for other versions. OTOH some of these notes are indeed only relevant to python 2. It's a work in progress...

  • Why do you care about data types? Because many operations will only work on specific datatypes,
    and most functions require arguments of specific datatypes.

  • Most Helpful Command: So you have a variable... what data type is it?
    print( type( [varName] ) )
    ----------------------------------------------------------------

  • Everything in Python is an object. All objects have 3 parameters: "identifiers" (names), values, and data types.
    Since everything is an object, data types are actually classes, and the variables are instances of the class.
    In fact, when you enter: print( type(4) ), it returns: "class int"
  • The data type of an object/variable is not declared/defined explicitly in python (unlike some other programming languages - Java & Arduino eg.).
    Instead, it is done automatically by python, depending on the value of the variable.
    • So for example, if you define a variable:
      var01 = 1
      it will be in integer (int) type.
    • if instead you define the variable:
      var01 = 1.0
      then it will be type float (due to the decimal point)

    ----------------------------------------------------------------

  • Data types can be categorized in several ways:
    • Whether they are composed of single vs. multiple entities/objects:
      • integers and bytes just have single objects
      • strings, lists, byte-arrays and others have multiple objects. They are 'sequences' of objects.
        • There is a format/protocol for referring to individual objects within the sequence:
          eg. suppose var01 = "asdaf"
          print( var01[2] ) returns: 'd' (note the index in the brackets is 'zero indexed',
          meaning '0' refers to the first element of the sequence, '1' refers to the second, etc.)

    • Whether they are mutable or not:
      • Some data types are immutable, meaning they cannot be 'changed in place'.
        • For example, strings are immutable. So if you define a string:
          var01 = "asdf"
          then you cannot change elements of it:
          var01[1] = 'x' will give an error
        • You CAN, however, redefine the whole string with:
          var01 = "axdf"
          • This creates a new object, even though it has the same name.
          • To see how this works:
            suppose you do: var99 = "adsf"
            and now do: print(id(var99))
            this will return the python id of the var99 variable,
            something like: ... ... ...
          • ????????????????????????????????

          • But now if you "redefine" the variable with:
            var99 = "yuio",
            then: print(id(var99)) returns a new ID, eg. ... ... ...
          • In contrast, if you define say an integer variable, which are mutable,???????
            with: var50 = 10
            then: print(id(var50)) might return something like: ... ... ...
            but then changing var50 eg with var50 +=1,
            while the value of var50 has indeed changed and is now 11,
            the ID has not changed, and print(id(var50)) still returns ... ... ...

            wrong - a new id is in fact created...

    • Whether they are built-in (native) or user-created:

    ----------------------------------------------------------------

  • ...
  • https://diveintopython3.net/native-datatypes.html

    Python has many native datatypes. Here are the important ones:

    Booleans are either True or False.
    Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even complex numbers.
    Strings are sequences of Unicode characters, e.g. an HTML document.
    Bytes and byte arrays, e.g. a JPEG image file.
    Lists are ordered sequences of values.
    Tuples are ordered, immutable sequences of values.
    Sets are unordered bags of values.
    Dictionaries are unordered bags of key-value pairs.

    • Boolean ("bool"):
      Either True or False

    • Numeric:
      • Integer ("int"): whole numbers, either positive or negative
        • Create/assign an integer variable like this:
          var01 = 4
          var01 = -254
        • random note: in practice (at least in python 3), there may not be a maximum value (other than sys.maxint (?) =~ 2^63)
        • random note: assign an octal integer with: 0o76 eg.
      • Floating Point ("float"):numbers with decimal points
        • Create/assign a floating point variable like this:
          var01 = 4.7
          var01 = ...
      • Complex:

    • Binary:
      • Bytes ("bytes"):
      • Byte Arrays ("bytearray"):
      • Memory View:

    • Sequences:
      • Lists ("list"):
      • Tuples ("tuple"):
        • Tuples are defined with parentheses - ( ), as opposed to lists that are defined with brackets - [ ]
          However you may omit the parentheses when defining tuples, eg. tuple1 = 1, 2, 3
            Defining a tuple with just one element still requires the comma: tuple2 = (1,)
        • ...

      • Ranges ("range"):

    • String:

    • Sets:
      • Sets ("set"):
      • Frozen Sets ("frozenset"):

    • Mappings ("dict"):

      ?????????????????

      • These are data types that have more than one entity per object, ie, sequences/collections/lists, etc.
        What these types have in common is that there is a format/protocol for referring to specific elements of the sequence.
        seq01 = "asdf" - defined as type string
        seq01 = ['a', 's', 'd', 'f'] - defined as type list
        in either case,
        seq01[0] - returns: 'a' (zero-indexed, '0' is the first element of the sequence)

      ??????????????????????

      from: (link to:) w3resource.com/python/python-bytes.php

    • Lists:
      • Lists are collections of some number of elements
        (a list with zero elements is called an "empty list")
      • each element is some valid datatype, including other lists
        elements can all be different data types
      • lists are defined with brackets "[ ]", with elements comma-separated, eg.
        firstList = ["element1", "element2", "element3"]
      • elements are referenced by their index number, ie. their position in the list,
        (zero-indexed, meaning the first element is index 0), eg.
        firstList[0] returns "element1"
        • if the element is itself a list, then referencing works like this:
          if list2 = [("dog", "collie"), ("age", 5)] - ie. list2 has two elements, and each one is a 2-element list
          then: list2[1] returns ("age", 5)
          and: list2[1][0] returns "age"
      • len([list or listVarName]) returns the number of elements in the list
      • append a list like this:
        list1 = [1, 2, 3]
        list2 = [4, 5, 6]
        for n in list2:
        [indent]list1.append(n)
        list1 now = [1, 2, 3, 4, 5, 6]
    • Tuples:

    -------------------------------------------------

  • Conversion between Data Types:
    • random notes:
      • s = "6a4f" - a 'hex string'
        i = int(s, 16) - converts to integer
        str(i) - converts it to a string
      • ...

    • Float to a String:
      • string3 = str(float2)

    • String to Bytes:
      • b = bytes(mystring, 'utf-8')
      • -or- b = mystring.encode('utf-8')

    • Integer to Bytes:
      • int.to_bytes(length, byteorder)
      • byteorder can be 'little' or 'big'
      • eg:
        bytes2 = int2.to_bytes(3, 'big')

    • Integer to Binary:
      • bin3 = bin(int2)
      • The binary returned will be a string prefixed with “0b”.

    • converting to Hex:
      • print(hex(255)) # decimal
        output: 0xff
      • print(hex(0b111)) # binary
        output: 0x7
      • print(hex(0o77)) # octal
        output: 0x3f
      • print(hex(0XFF)) # hexadecimal
        output: 0xff

    • convert string to hex:
        hex_string = "0xFF"
        an_integer = int(hex_string, 16)
        hex_value = hex(an_integer)
        print(hex_value)

    • not sure what this is...
      • You have a sequence of raw unicode that was saved into a str variable:
        s_str: str = "\x00\x01\x00\xc0\x01\x00\x00\x00\x04"
        You need to be able to get the byte literal of that unicode (for struct.unpack(), etc.)
        s_bytes: bytes = b'\x00\x01\x00\xc0\x01\x00\x00\x00\x04'
        Solution:
        s_new: bytes = bytes(s, encoding="raw_unicode_escape")

    -------------------------------------------------------------

    https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-...

.

.

.

eof