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