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

A Hundred Days of Code, Day 006 - Starting and Quitting Comprehensions

I know I said, I’d start with the basics, Reuven, but please forgive me this once :) Will do the rest, bottom up :) Have started up with this, because I’m fascinated with how folks manage to build in so much functionality into short, easy to read statements. To my mind, comprehensions are the pithy proverbs of the programming world. So here goes … Notes This style of writing code, comes from functional programming Functional Programming is another style of writing code It assumes that data is immutable. (Or at least it treats it as such) It does not change the originals I work with. (I assume this means that I always work on a copy or something) we work with sequences of data and create new ones based on the old ones (changing case of a sentence, comes to mind) There are a bunch of techniques/patterns/heuristics that’ll let me do that We can treat functions as data and pass them around as arguments to other functions. The functools module assists with writing such code Why code in such a manner? Some benefits include them being easier to read, easier to debug, shorter, more understandable. Distributed Computing / Multiprocessing across processors and cores/threads makes these techniques more relevant in this day and age My personal need is to write easy to read code, like when I see a well crafted list comprehension If I want to square a list of numbers, my current go to is to generate a new list, using a for loop. Comprehensions are a better way of doing this For loops are far when I want to execute something on each item of something iterable, like a list or a dictionary. when i want to have a statement happen If just want to transmute the elements of list into something else, change each element, like square them all above, then comprehensions are my friend Here’s a list comprehension to square a list of numbers from the list numbers. [one_number * one_number for one_number in numbers] The square brackets make it a list comprehension. If I want dictionary comprehensions, I would abut them with curly braces {}. And if I use (), I’ll get a generator expression (which I know only the bare basics about) For loops and comprehensions might look similar, but they really are different Note: whatever expression you use to generate output has to return something. like the square expression above . because what I want to do is catch all my results in a container. functions (like the print function) that return None as their result, cannot work here. They will display stuff, but store only None in our comprehension container :) I can also write the comprehesion over multiple lines (works because they are bounded by the list brackets here or bounded by other brackets elswhere) [one_number * one_number for one_number in numbers] here with the breakdown of the lines, the first line becomes my output generator; the expression I want to use and the second line, the data I want to process. Much more readable this way learning / feedback/ experiences from doing exercises Exercises today required a bit of head scratching and a bit of research, but were relatively simple I should keep in mind that the computer can go crazy too. I kept wondering why my code wouldn’t run and and kept banging my head for an hour. Even Rueven’s solution was what I did. Until I restarted my Jupyter note book and everything worked the way I expected. Should have a time limit on how much struggling I do. From the last week, it looks like I am a real sucker for pain otherwise. Half an hour tops and then go look up the solution. Have reached the second set of exercises and realised why Reuven told me to start with the basics. I failed with the second one in this set. I just know, there is a simple solution and I cannot seem to get it. This basically means … yes … that my foundations are creaky. Note to self, walk before you can run. By Jove, you should’ve learnt that by now! :) So this is me, waving goodbye sorrowfully to comprehensions. Will start with the basic Python course tomorrow. But I’ll be back soon enough, damn it! ...

July 13, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 005 - Magic Methods and Winding Up OOP

Done with Reuven Lerner’s OOP basics Notes len does the right thing across multiple types of objects. it counts characters in a string, elements in a list, and key-value pairs in a dictionary. how does it know how to do that? that is because len uses a magic method (methods that have a __ for a prefix and suffix) len uses the __len__ method which i can use in my own classes, to implement a len method for my classes If I choose to do so, I need to realise what those underlying methods expect to return and return the same type in my classes. __len__ expects to return integers, so when I implement it in my classes, I ought not to return a string. Once again Reuven strongly suggests using common sense :) same thing when i try to print something. it calls the magic method __str__ to do stuff. or if i try to do repr to check for raw representations, the __repr__ method is called. Python is filled with these methods which I can use to allow my classes to have standard Python like functionality. like a print(doggie) should give me details about the cute pup object i just created, if I have taken the time and attention to implement the __str__ or __repr__ methods most of these methods come with the base primitive class object which every class inherits from. so i get them for free. and then i can override them and customise them to what I want to show. people try standard methods on new objects. implementing them is much better than creating my own and asking people to use ’em. if both are equal or really similar, ok to just use only __repr__. later when I have experience and other needs, i can implement __str__ too learning / feedback/ experiences from doing exercises Despite warning myself above, I went ahead and made the mistake of returning a whole lovely string, instead of an integer, when I tried to implement the len method on my class XD and then I forgot to add the f prefix to an f-string and wondered why it was not interpolating for nearly 30 mins Reuven has a wry sense of humour. In one of my exercises I get to create classes of animals for a zoo, with various attributes such as colour and legs and so forth. Then he has me put them in cages. After making me put 2 sheep in a cage with a wolf, he wonders if the legs of the sheep ought to be reduced or not XD The number of typos I am making is staggering. Missing colons, missing quotes, missing brackets. If this is Python, I shudder to think how I’d fare if I picked up a static language to learn XD Wasted another half an hour because I spelt colours with capitals like Black and then later looked for black and was tearing my hair out, because I knew there were black animals and why in tarnation, would Python not show me them XD Saw Reuven do nested comprehensions and now I want to do that too! it took him one well thought out line to do what I did in 2 multiline functions Final Words This was amazing! I learnt so much about classes and the way they work. Reuven has a very joyful pedagogy. I love him go hahahaha like Santa Claus. I love the fact that he makes mistakes. I love that for every problem, he poses, he has various approaches. His fluency shines through every lesson. I got lucky with the Pycharm Humble Bundle. But I loved this so much, that I am going to sign up for all the Lerner courses, I possibly can. Beginning with the basics. If OOP is any indicator, the journey is going to be lots of fun. ...

July 12, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 004 - Class Attributes and Inheritance

Learnt about Class Attributes and Inheritance, today. Notes The way we write functions/methods and define classes might look similar, class CrazyTalk(object): and def how_now_brown_cow(): The way they behave/execute, though is wildly different. Classes run / spring to life as soon as the program launches. Functions don’t, unless they are called. Realised that objects are containers. What you can do with the object depends on what is in it. Does it hold a dictionary with various methods to modify it? Does it just hold a bunch of text? Reminds me of media formats as containers. A mp4 file is not just an mp4 file. The video resolution could vary from one file to another. The type of audio encoding could be different. It could carry multiple streams of audio or video or other metadata. What you can do with the file or where you can play it, depends on what is inside it. Same with classes and objects. ...

July 11, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 003 - Methods

Learnt about methods today. Notes follow … My understanding about methods? They are functions in classes that help me manipulate the data the objects contain when they are created. I have been using something them subconsciously all along. The __init__ method, that is called/run automatically every time an object is created. Aha A nice way to unpack the .notation i use Let’s say I have a class SquareNumbers that has a function (method) ­— x2 — that will, you know, square a number. And then I create an object f with a number and call the square method on it. ...

July 10, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 002 - Basic Exercises

Did a few exercises today. They were simple. Create a few classes, change them, modify them, use a list as an attribute and so on. In a couple of ways, this was just what I needed. One, I had an extremely busy day at work, and so I did not have the brain power to do anything complex, so I needed the bar really low any way. And two, I’ve realised, that I have always tried to just read a book and then leap mountains. I don’t know why. I must be a sucker for pain. And then when stuff does not work, I sulk and get frustrated. Easing into hard topics like this, makes it more enjoyable for me and I learn better. This is a meta skill, I should remember to use. Come to think of it, everything I have learnt recently, actually learnt, has been this way. I learnt Dvorak and touch typing, slowly, key by single key. I learnt to diet and lose weight, by tens of grammes in the beginning. ...

July 9, 2020 · Mario Jason Braganza