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
- 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)
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.
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:
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.
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