A Hundred Days of Code, Day 012 - Python, Advanced Data Structures continued

Ok! Had a nice refreshing break, yesterday being Sunday. Back to work today! If today’s notes, feel a little light, that’s because I was struggling with exercises. Notes Part 2 A lightweight solution to classes, if I am just combining all sorts of data structures is the built-in collections. Some of them could be List of Lists List of Tuples List of Dictionaries Dictionary of Dictionaries Dictionary of Lists Mostly use them to solve tricky issues. If I find myself using lots of functions, just to deal with these structures, I am better off using classes. These structures also lend themselves to being converted into classes. Sometimes it’s just a matter of adding a few methods, et voilà, I have a class! Lists of lists ...

July 20, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 011 - Python, Advanced Data Structures

Started with a new Reuven Lerner Course, on Advanced Data Structures.. Aiming to comfortably finish this in a week. Notes and experiences, follow. The course is three parts. Part 1 - Deep dive into the common data structures Part 2 - Combining Data Structures. Lists of lists, Dicts of lists, Tuples of Lists and so forth. How far can I go without classes? Or how can I enhance my classes with this approach? Part 3 - Complex data structures, that come with the Python Standard Library. The Collections module, Weak references etc. Notes Part 1 ...

July 18, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 010 - Python Functions, Basics Done!

Today was hard! Notes ‘*args’ way to get multiple arguments without having to assign placeholders for them it scoops up all the arguments and gives it to the function in a tuple. I could then loop over it with for. I just put * in front of one of my placholders et voilà, def example_func(*catch_everything) I can define placeholders to catch specific arguments, but those need to be before my ‘*args’. Edge Case: I could have my arguments in a list/tuple already. I can just add the structure with a * in front of it and Python will unpack it for me. If I have a list, catch_me_if_you_can=[1,2,4] then I can pass the elements individually, as arguments with a def example_func(*[catch_me_if_you_can]) Another kinda frequent edge case: ‘*args’ catches everything that is unassigned. so if i have something with a default value, it will get trampled by the data from my args. Best thing to do is to set the default after the ‘*args’ like so, example_func(a, *args, b=10) Keyword Arguments ...

July 17, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 009 - Python Functions, Basics

Started up with learning about Python function basics, today. Notes follow. Using the Lerner pool of wisdom, as usual. Why do we need functions? DRY (Don’t Repeat Yourself), because we can take stuff that is repeatable, assign it a nickname and just call that nickname over and over, instead of tediously writing all that stuff over and over and over If want to modify something, I can just call it (by the nickname) and then add my little spice to it. Like I have the recipe to an omlette written down somewhere, I can then just add a little note with secret ingredients at the bottom of that recipe to create a better or a different omlette. Functions are objects. ...

July 16, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 008 - Python Basics, Lists, Tuples, Dictionaries, Sets and Done!

This was an amazing run! Learnt lots. Done with the basics course. Woohoo! Notes With lists Not pythonic to mix types can iterate, slice and dice, search, get lengths, transmute them (for e.g. reverse items in them) just like strings can have lists of lists! lists are mutable, unlike strings. can change items/elements of a list If i create a list from another list, the new list items reference the old list items if i want to copy elements from a list rather than reference them, I’ll go newlist = oldlist[:] which will copy the elements from the old list into the new one, rather than create references to the old ones Realise that there is a difference between changing the values of a variable and between changing elements of a list. When I do x = 2 and y = x and then change x to be x = 3, remember that the value of y does not become 3. y continues to point to the original location in memory which holds 2. What I did was to break x’s connection to that value and assign to another value in another location On the other hand, when I do x = ['jane', 'jill', 'mary'] and then create a copy with y = x and then change x[0]='edith', what I do in this case is change the actual element. The references to that object in momory stay unchanged. Which is why y[0], which holds references to the same object, will also reflect the change to edith. Finally if I go, x = ['athena', 'diana', 'aphrodite'], now we’re back to the assignment. x will point to this new list while y points to the old one. This distinction between changes in assignment vs changes to content is very important, when dealing with mutable/changeable data types like lists Append something to a list with .append - list.append(something, something else) Heuristic, when a method on a mutable data type, changes data, in returns None So I should just go mylist.append('blah') and stuff will be done If go mylist = mylist.append('blah') (which I am very apt to do) the method will change the list, then return None and that None will get assigned to mylist, In effect, erasing the list and causing unnecessary heartache. So, be careful, very careful. Not careless, like Bond, James Bond. += or .extend takes an iterable on the right. picks each element. and then assigns them singly to the list on the left. Append can take only one item. If I want to add xyz as separate entries to mylist that has ['a','b','c'], append will just take it as a big lump and make ['a','b','c','xyz'] Instead I do a += mylist += 'xyz' or mylist.extend('xyz')will give me ['a','b','c','x','y','z'], which is what I wanted in the first place it will only work if there are iterables on the right. (strings, lists, dictionaries etc.) the insert method will let you insert something at a location in the list. Give it what you want, tell it where you want, and boom! mylist.insert('d', 3) will put d after abc, giving me ['a','b','c','d','x','y','z'] I can replace things in a list by giving it a location and telling it what to put in there. I could do mylist[0]='artemis' and the list will change to ['artemis','b','c','d','x','y','z'] I could also give it a slice along with a list of what to put in there. Doing mylist[1:2]=['josephine','lisbeth'] will now give me ['artemis','josephine','lisbeth','d','x','y','z'] if you add additional elements, than what you replace, Python will insert them, growing the list. Removing things from a list? the .pop method will pop off the last item in a list. If i give it a location, it will remove the item at that index location. mylist.pop(3) will remove d from mylist giving me ['artemis','josephine','lisbeth','x','y','z'] When I know what I want to remove, I just use the remove method instead of pop. mylist.remove('josephine') will remove poor Josephine leaving us with ['artemis','lisbeth','x','y','z'] If I’m not sure, I could check for the presence of an item before I remove it. Should prevent a program from barfing on me if that element isn’t there if 'q' in mylist: mylist.remove('q') also the remove method only removes the first instance of something. Iterating and counting over lists? the for loop! I can use the enumerate function if I want numbered items out I can iterate over a slice. The range function is very list like in its behaviour. I can sort lists. same caveat as above. sort will change the list in place and return None. Don’t go assigning mylist = mylist.sort(). It won’t work A .sort(reverse=True) will sort in reverse sorting between mixed types in a list doesn’t work in Python 3 anymore. The types in a list, ought to be the same. Sorting list of lists works by comparing indices If I don’t want to change my original list, I can use the sorted function on the list. It works on most iterables, not just lists, while the list method is restricted to lists It returns a new sorted list, and keeps the original list intact. I can do sorted('something', reverse=True) will return a reversed list. I can str a list and list a string ...

July 15, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 007 - Python Basics, Variables, Basic Data Types, Strings and Loops

Started with the Reuven Lerner, Intro Python:Fundamentals course today. Made surprising headway, even though today was crazily demanding with work and personal stuff. Notes The difference between == and is If you want to see if two things are equal, use == like, is 2+2 == 4 If you want to know if two objects are the same thing. if they are just pointers to the same memory location, use is. The heuristic is, if you are checking for singleton values, something that there is only one thing of; then in Python, we use is. Use sparingly . Make sure you check mostly to see if things are the same object or practically to see if something is None mostly to check for the presence or absence of things. like x = None and then I go check if x is None … True / False heuristic in Python Nearly everything in Python is true except for False, None, 0 or anything empty (empty lists, dictionaries, etc.) Learnt how working with strings in reverse works if I want the last element in a string, I could reference it by calculating it’s length and lopping one off (since indexes start at 0), like with the string s = 'Halt! Who goes there?' I could access the last element by s[len(s)-1] or the last but one, with a s[len(s)-2]. This is made a lot more convenient by just dropping the len(s) stuff, just leaving the - numeral part - s[-1] or s[-2] This makes so much sense in my head now, if what I am doing is counting straight and then subtracting one. I should remember, things sometimes are simpler than they seem :) For some reason, I always imagined, Python could see the length of my string, so why couldn’t there be a reverse string way of doing things like s.rev[0] that would just count backwards 😂 Strings are immutable. They can’t be changed. I can just process a copy and generate new ones, though. just realised that the f in f-strings stands for formatted strings, since when I write raw strings, I use an r for, duh, raw strings Slicing involves carving out a small contiguous part out of a larger string. The thing to note is that when I say s[0:4] it means upto, but not including 4 If I say s[0:100], slices are forgiving and give me upto the end of my small string, without throwing an error. Empty colons in slices imply endings. s[:10] means start from 0 and go up to 9. Or s[9:] means start at 9 and go upto the end. Ergo s[:] means from the beginning to the end. I can have step sizes to step over. The default, hidden size is 1. I can step, oh, say every three elements by append :3 to the end of my slice like so, s[0:20:3], basically [start:end:step size] I can also have negative step sizes which will give me the results in reverse. The most common one being -1, because the quickly reverses a string for me (commonly done). But it works with other step sizes too. A -2 will start counting backwards every other character from the end. A reverse slice begins with the end character and then beginning. So s[10:5:-1] to carve out a slice backwards. Sequences are strings, tuples and lists. They all can be sliced and diced and iterated upon. Heuristic, else in a for loop is to match stuff when I don’t encounter a break. the else part matches the for to form the for-else construct, even though the break is inside the for loop while loops are for when you don’t know the end of what you are looking for. there is not finite end. No 0 - 10 happening. You are just searching for something in an amorphous swamp. The swamp is vast, there is no way to say from this end of the swamp to that end.You only give up when you find what you are looking for, or when you decide it’s time to give up (some preset condition). make sure there is a counter that breaks stuff or I might loop forever Learning / Feedback/ Experiences from doing exercises Realising why Python notebooks are used. Very, very, very, handy to quickly write prose and code at the same time. It fits my mental model of programming perfectly! I keep thinking strings and characters are English and forget to quote them! The number of NameErrors I get, is not funny. Thing that keeps biting me when I write or statments. I should remember to say if x='blah' or x='meh'. I keep writing if x='blah' or 'meh', which is wrong. I keep using a lot of superfluous variables. I should be more intentful. Instead of just printing an expression, I tend to figure out the expression, assign to a variable and then print it. Good for building, probably bad for optimal use. But I won’t worry about it too much now. Just something to improve on in the long term. Reuven is a painstakingly methodical teacher. If he’s so good in his videos, I am jealous of folks attending his live classes Read all about my Python basics journey here ...

July 14, 2020 · Mario Jason Braganza