Week 1
Things that I learned during my first week at Hackbright:Functions:Another section that I am so happy that I took C++ because I totally understand the importance of having functions. What parameters and arguments are was also not new because of my background. I learned about Docstrings and how they are the abstraction of what your function does. Python is cool with how you can actually put into your Docstring and example of how you call the function in the terminal and what the response should be - a little test to make sure its working how you expect it to and it gives others an example of the your function's behavior. Super cool!Memory: Memory in Python is crazy compare to C++! Huge secret: Python doesn't have variables! Variables are identifiers and get bind to the spaces in memory that holds the value. As you change the value, the identifiers get rebound to the new locations that contains the new value. To keep from having a memory overflow, the garbage collector comes around and cleans ups.
x = 3
variable name (Binds/Rebinds) New space in Memory
I also learned about Immutable and Mutable types in python. Immutable types cannot change in place (Strings, Numbers,Tuples, etc.). Mutable types can change in place (List, dictionaries, etc.). This is because the identifier points to a space in memory and when you change an item in a list or dictionary, it rebinds to the new space, keeping the identifier and the rest of the items in its original space. While Immutable items must have the whole item rebound because you cannot change just once character of a string or a single number in a number, you need to rebind the variable to a whole new space space.***I learned about Array Mathematics Week 5 from one of my mentors. I told him that the memory lecture blew my mind because of different python works compared to C++ and he explained how the following operates in memory:int* arr = new int[255];int* v = arr;for (int i = 0; i < 255; ++i, ++v) {printf("%d\n", *v);}Iteration & List: Iteration is so much easier with Python! I love it! The set up is so easy with just:
variable name in thing to iterate
I love that I don't have to have index and hold where your at within the iteration without a counter(i)! There are times when you want to keep track of the index, you can use range. Sometime else that is super cool is that you can unpack list! What does that mean? Its fantastic! Its where you can set several items in an iterable in one line. At first this seemed unclear or misleading because your unpacking items with a clear x = y, but after a while it made sense. I do prefer to be explicit whenever possible, but if its within a function that the format has already been explain, its a great way to set up several variables at once.List Comprehension: A wonderfully beautiful way to write a for loop in one line.
example: a_word = [word for word in words if word.startswith("a")]
Collections-style data structures: At Hackbright, the instructors did a fantastic short play that explained the the differences between list, tuples and sets. It was a great reference to be able to understand the differences between the three and their strengths. Below illustrates this:
Ordered |
Mutable |
Unique |
Iterable |
|
Lists |
X |
X |
nope |
X |
Tuples |
X |
nope |
nope |
X |
Sets |
nope |
X |
X |
X |
A very cool trick that Sets can do is Set Math! You can have two set of items and do math to see what items both sets have, items that are not in both sets, all the items together and what times are only in one or the other. Example:moods = {'happy', 'sad', 'grumpy'}dwarfs = {'happy', 'grumpy', 'doc'}moods | dwarfs --> {'happy', 'sad', 'grumpy', 'doc'} #unionmoods & dwarfs --> {'happy', 'grumpy'} #intersectionmoods ^ dwarfs --> {'sad', 'doc'} #symmetric differencemoods - dwarfs --> {'sad'}dwarfs - moods --> {'doc'}