Hi everyone! Been busy the past couple months and junk. But now I'm back and uh, doing stuff and things.
So the past while I've actually been wanting to do a little introduction to programming, because I have a fair amount of friends who will sometimes ask me what it's like to be a wizard and I have to tell them it's not as magical as they think (I mean, it is to me, but I'm also fucking weird). Keep in mind I haven't had to "learn to program" in ten years-ish, so if anything I try to explain just is NOT making sense for you, let me know somehow!
I can't think of anything else necessary before diving right into the nitty gritty, so heeeeere we go!
Then you just kinda wait, tell anything that pops up "Yes", and you eventually get treated to a "Python has finished installing" dialog. Afterwards, you should see a nifty new entry in your Start Menu (right). Give it a click and you get something you've probably never seen before.
Now hold on, before you grab your pitchforks and call me stark-raving mad (true), let me explain. What I said earlier about Python being an interpreted language? Well, this is the interpreter. It is your friend in this crazy world of programming. This is where you can input those individual lines of code and the Python interpreter will tell you what it thinks of your code. Now, a lot of programmers will tell you that your first program should be the "Hello World" program, which literally just prints "Hello World!" to the screen.
Python was basically looking at it as "integer divides integer". The result, probably because removing fractional parts of a non-integer is annoying, is an integer rounded down from the actual result. Then, when you used "3.0" instead of "3", Python saw "real number divides integer" and gave you a real number. Now, by no means are these the only kinds of data you can deal with. Say you wanted to tell Python you love it. Just do this:
Including the quotation marks. Python should just spit that right back out at you, maybe replacing double-quotes with single-quotes. Want to put two strings together? Easy.
So the past while I've actually been wanting to do a little introduction to programming, because I have a fair amount of friends who will sometimes ask me what it's like to be a wizard and I have to tell them it's not as magical as they think (I mean, it is to me, but I'm also fucking weird). Keep in mind I haven't had to "learn to program" in ten years-ish, so if anything I try to explain just is NOT making sense for you, let me know somehow!
I can't think of anything else necessary before diving right into the nitty gritty, so heeeeere we go!
First Steps
1) Acquire computer
This is left as an exercise to the reader.
2) Acquire Python
Okay, there's some technical voodoo associated with this. Feel free to skip the next paragraph if you just want to get to the "how do I hack the Pentagon's wifi" part, which will be covered in the next millennia after the Pentagon of then has no relation to the Pentagon today.
EXTRA EDUCATIONAL BULLSHIT STARTS HERE
First, there are a lot of different types of programming languages. One of the more common distinctions is compiled vs. interpreted. A compiled language is one that is taken from its complete sequence of instructions in source code (human-readable instructions for that language) and translated by a compiler into a sequence of machine code instructions (human-readable for exceptionally machine-like humans). An interpreted language is one with sequences of instructions (which can be called "programs" when talking about either of these types of languages) that are executed one-by-one by an interpreter into machine code instructions. If you're reading this, you probably don't care about why this is important, but if you're curious, just ask!
EXTRA BULLSHIT STOPS HERE
Python happens to fall under the category of an interpreted language, so when you run a Python program, you're pushing a sequence of instructions through an interpreter, which your machine will run one at a time. Because of this, you need the Python stuff, which will include an interpreter. (Windows, get the MSI installer for whichever type of CPU you have; Mac, you only have the installer as an option. As far as I know Mac OS X comes with Python installed, but I can't guarantee that the version it comes with will support every line of code I'll have here. Linux users can use their package manager, eg. apt-get or yum or whatever other distros use).
Once you have your installer downloaded, the process is pretty easy. Just hit "Next" about four times:
Then you just kinda wait, tell anything that pops up "Yes", and you eventually get treated to a "Python has finished installing" dialog. Afterwards, you should see a nifty new entry in your Start Menu (right). Give it a click and you get something you've probably never seen before.
Now hold on, before you grab your pitchforks and call me stark-raving mad (true), let me explain. What I said earlier about Python being an interpreted language? Well, this is the interpreter. It is your friend in this crazy world of programming. This is where you can input those individual lines of code and the Python interpreter will tell you what it thinks of your code. Now, a lot of programmers will tell you that your first program should be the "Hello World" program, which literally just prints "Hello World!" to the screen.
But fuck those guys. I'm not here to turn my friends into run of the mill coders. I'm going to teach you how to do something cooler than that for your first program.
But that's a little bit ahead, because we must take baby steps on our path to automating office jobs. You should have this Python interpreter open, as signified by the >>> in front of your text cursor. Now, the first thing you will discover from this tutorial: Python is good at math. Try it. Just type in "2+2" (without the quotes), so you see this:
>>> 2+2
It didn't even have to think about it for your answer. Get a little trickier, give it some multiplication, subtract some numbers, you have a really awesome calculator here. Now give it some division that won't turn out as an integer. Like:
>>> 3/2
Second thing you will discover: Python is very bad at capturing intent. It told you "1", which is not incorrect - it's just not the answer you were expecting because Python, in order to work as deterministically as possible, has to treat some operations different from intuition. In fact, you'll find this kind of thing in every programming language I've ever worked with that supports division. So now try typing:
>>> 3.0/2
And it works! You should get 1.5 as the answer. If not, check sanity. But with this you've come across, very simplistically and transparently, the programming concept of "typing". Not what I'm doing in this blog post, but data typing. The basic idea is that every piece of data belongs to a category, and in order to make sure you don't accidentally blow up your computer, Python has to stay consistent with how it does things to particular types of data. So when you typed:
>>> 3/2Python was basically looking at it as "integer divides integer". The result, probably because removing fractional parts of a non-integer is annoying, is an integer rounded down from the actual result. Then, when you used "3.0" instead of "3", Python saw "real number divides integer" and gave you a real number. Now, by no means are these the only kinds of data you can deal with. Say you wanted to tell Python you love it. Just do this:
>>> "I love you"Including the quotation marks. Python should just spit that right back out at you, maybe replacing double-quotes with single-quotes. Want to put two strings together? Easy.
>>> "hello" + "world"Note the lack of a space in between them, but that's easy enough to fix. Now, before moving on to other data types, there's something else good to know: variables.
Spare me your lamentations from dealing with variables in algebra, we all went through that. But there is no solving for them here (unless you're doing mathematical programming, which is fun too)! The point of a variable is to assign a name to pieces of data for easy reference, storage, whatever. How do we create variables in Python? It's very difficult.
>>> x = 2Python shouldn't say anything to you if you type that. But then type just the letter 'x' (no quotes). It tells you the value of the variable! But...but what did you do here?
So what you did is you decided on an arbitrary label for your variable (here, just 'x'). Then, you gave it a value (here, 2). That's preeeetty much it. You'll usually want to have more interesting names for your variables, like currentNote (music) or thisDuder (game character) or countHotLadies (harem management), but that's really up to you. Also, they are case sensitive in Python and most languages, so 'x' and 'X' are totally different and not tied together unless you make them that way.
Cool. Awesome. We have a variable. Now what do we do? Well, another thing you should remember from algebra in high school are things called functions, such as sin x and log x* and other such things. Those basically take a number, represented by x in the common notation, do something with it, and give you another number.
*I acknowledge that, technically, logarithms are not a function but an operation, but this distinction isn't important from the perspective of a programmer.
Why are they important? Well, with programming, functions are extremely common. I haven't written more than twenty consecutive lines of code after my first month of coding that did not have at least one use or definition of a function. You can use them on more than just numbers - you can operate on strings, on lists of values, you can use a function on just about anything you can possibly imagine, including, in some languages, on functions.
One pair of functions that gets a fair amount of use is the str.upper() and str.lower() functions. Note this notation - the "str." in front indicates it's a string function, the "()" at the end indicates that it's most definitely a function. The functions, as you might expect, turn strings into their uppercase or lowercase equivalents. Usage would be:
>>> "hello".upper()Note that the string you're working with goes in front in its rawest form, quotes and all. More on that later, but check out the result. An uppercase version of "hello"! Sick nasty.
I think that's it for this post. Comment with any questions, complaints, or do so on my Facebook and I'll try to get to you ASAP. Happy coding, until next time!




No comments:
Post a Comment