Thursday, 24 October 2024

Learning Python with Houdini Part 3

Data Types

    Mutable Vs. Immutable Types

    Immutable data types are sequences that can't be change. If you want to change the value, Python destroys the old version in memory and creates a new version. This is fast.

    Mutable are sequences that can be changed. If I have a chunk of memory, I can expand or contract my memory to include what I need. You need a lot of overhead for this and it is slow. Use this if you don't know how big the data is going to be and there could be expansion or contraction.

    Sequences are any collections of objects in Python. 

    Immutable:

  • Boolean
  • Integer
  • Floating point
  • Complex
  • String
  • Tuple (uses () to show it's a tuple)
  • Frozen Set 
    Mutable:
  • List (uses [] to show it's a list)
  • Dictionary (uses {} to show it's a dictionary)(great way of organizing your data, html and json files use dictionaries)
  • Set (there are no duplicates in sets)(use{}) (hybrid between List and Dictionary)
    How are integers and floating points and strings immutable if we've been able to change them in previous assignments? Well you can test this by assigning a variable as a string, and then try to change it like you would try to change a List. 

    In a List you use a reference number to specify the address you wish to change in a sequence (you start at 0 because it's the starting address in memory + 0). You can't do this with immutable types because you can't change that specific place in memory.
    Another thing with indexing is that you can go backwards in memory (when you go backwards, you start at -1 to get the very end)
    You can even use a range for you index, or specify you want to skip every second item.
    Lists don't have to have items with the same data type. You can have strings and integers. 
    For Dictionarys, you need a key and a value. A key is an address, but not an index. The only condition for a key is that it cannot be a mutable instance. You can have duplicate values but not duplicate keys. A value is any kind of value. The syntax is like this:
{key:value, key:value}

Notice that len() returns 2 elements, that is because it goes by the key, not what's in the dictionary. To access the elements you have to use the key.

    As a suggestion, keep all of your types inside of you sequences the same to make your life easier.
    If you want to add a new element to dictionaries, you can't append. The keys are not necessarily sorted. Keys are something called hashes (used to make data more compact). You need to define the key and then assign the data.
    You can iterate over a dictionary but it will not iterate over a predictable order because of the keys (due to the hashes)
Dictionary Methods:


    To create an empty Tuple, you just have parenthesises.
gal = ()
    This returns a value of none (). Why would you do this? Sometimes programmers like to use this when they're creating functions to remind themselves/others that that variable is important. 
    List Methods

    When you use input(), it returns a string. We use .split() to unpack the input with several variables.
    Sets are a hybrid of Dictionaries and Lists. It will change the order on how you inputted the values, dependent upon the hash. 
    You can check in there is a element in the set with:
    You cannot have duplicate elements inside of a set.

Recursion

    Recursion can be slow on your computer, because it is calling itself over and over again.
    Fractals: infinitely defined.
    3 units of recursion to help create any realistic type of visual effects. You need one fractal shape, that inside has several levels of recursion. 

No comments:

Post a Comment

Shader Tool Week 3 - Surface Mapping

 Texture Cubes     Also known as cube maps, its 6 2D texture maps corresponding to the faces of an axis-aligned cube. Used in lighting a sce...