Posts Tagged Native Data Types

Python 3: Native Data Types: Cheat Sheet

When first learning Python 3, I made a “cheat sheet” listing of the data types, primarily to help get myself accustomed to Python’s data types that differed from the data types used in C++. I did this while reading Mark Pilgrim’s book “Diving into Python 3″(Highly recommended as a solid intro into Python 3) and using the Python v3.1.3 documentation. I decided to post the “cheat sheet” just incase someone finds it useful. This is by no means the definitive listing for Python data types, but it is intended as a quick reference for basic features of the types listed.

Python 3.1: Native Data Types

  1. Booleans
    1. True = 1
    2. False = 0
  2. Numbers
    1. Integers                                                               2,3,4, etc.
    2. Floating Point                                                     2.0, 3.0, 4.0, etc.
    3. Fractions  (import the fractions module)      2/3, 3/5, etc
    4. Complex                                                             (real)i + (imaginary)j
    5. In a Boolean Context
      •             zero values are False
      •             non-zero values are True
  3. Strings
    1. Sequences of Unicode characters
    2. Delimited by either single or double quotes – use double quotes when an apostrophe is present in the string
    3. Do not take arithmetic operations (without a conversion – obviously!)
  4. Bytes and Byte Arrays
    1. Have all methods found on strings, with the exception of encode(), format() and isidentifiier()
    2. Bytes objects are immutable , 8-bit bytes, in the range of 0 to 256
    3. Byte array objects are mutable and unhashable.
    4. Image files
  5. Lists
    1. Indexed a_list[0] (index from left to right) a_list[-1](index from right to left)
    2. Slices a_list[x:y]
      •              x = start point included
      •              y = end point, up to but not included
      •              [:] = copy the entire list
    3. Adding items
            •              append(x)
            •              extend([x,y,z])
            •              insert(x, value)
            •                        x = position to inset value
            •                       value = the object to insert at position x
    4. Duplicates are allowed
    5. Holds anything and its dynamic
    6. del a_list[1]   => deletes a specific item
    7. del a_list       => deletes the entire list
    8. a_list.pop(x)
      •             when x is omitted, removes the last item in the list and sends it as output
      •             when x is provided, removes the item at position x and sends it as output
    9. Calling pop() on an empty list raises an exception!
    10. In a Boolean Context
        •             an empty list is false
        •             a non-empty list is true
  6. Tuples
    1. Accessed just like Lists
    2. There is no mechanism to change a Tuple (add/subtract elements)Equivalent to a “write-protected” list
    3. A tuple can be sliced like a list
    4. Check for specific values
    5. Can assign multiple values at once (in order)
    6. If the tuple has a single element, it must be followed by a comma – to consider it a tuple
    7. In a Boolean Context
      •              an empty tuple is False
      •              a non-empty tuple is True
  7. Sets
    1. An unordered collection of unique values.
    2. A single set can contain values of any immutable data type.
    3. Implemented as classes
    4. Once you have two sets, you can do standard set operations
      •             union
      •             intersection
      •             set differences
    5. In a Boolean Context
      •            an empty set is False
      •            a non-empty set is True
  8. Dictionaries
    1. An “unordered” set of key-value pairs.
    2. When a key is added, a value for that key must also be added.
    3. Python dictionaries are optimized for retrieving the value when you know the key, but not the other way around.
    4. You can have duplicate values, but not duplicate keys.
    5. Assigning a new value, overwrites the old one.
    6. Case sensitive
    7. Can contain a collection of any type (mixed types allowed)
    8. Like lists and sets, the len() function gives the number of key-value pairs in the queue.
    9. In a Boolean Context
      •             an empty dictionary is always False
      •             a dictionary with at least one key-value pair is True
  9. The value None
    1. Is a special constant, it’s a null value, not the same as False, not zero, and it is not an empty string.
    2. Comparing None to anything other than None will always return False
    3. In a Boolean Context
      •             None is False
      •             anything else other than None is True

, ,

Leave a comment

%d bloggers like this: