Auto-Summary
This post is the second part of a beginner's guide to Python programming, focusing on conditions, functions, and indentation. The author, DorkWithTheHair, explains how conditional statements like "if" allow programs to make decisions based on comparisons, using operators such as ==, <, >, !=, and >=. They emphasize the importance of proper indentation in Python, which replaces curly braces in other languages. The post also introduces functions, showing how they can encapsulate code for reuse, and covers the use of "return" and "else" to control program flow. The tone is casual and engaging, with humor and relatable examples, making complex concepts accessible. The author sets up future lessons on loops while reinforcing the foundational role of logic and functions in programming.
I'm Gonna Learn You The Python REAL GOOD
Or: Telling A Computer WHEN Shit Needs to Happen
Table of Contents:Part I: Basics for BeginnersYou thought I was done? You come into MY house...AND THINK I WAS DONE? No! While you were waiting, I
was studying the blade was...honestly, mostly working on my own shit and writing. And playing Valheim with my brother. Look, there's only so many hours in the day, get off my ass!
So, yes! We're moving on up in the world, learning to use ~*~coNDitIOnaLS~*~. Long story short: they're the parts of your program that say when something should happen. Stuff that just kinda happens outside them always runs no matter what. Something like:
Code:
x = 12
Will always set x to 12. While handy (possibly even dandy), there's more to the story that we've yet to uncover. Because, let's face it: there's more to life than just moving data around and printing it out. This is where we'll actually start making things
THAT DO STUFF. (ominous thunderclap)
You know how to start IDLE, by now, so do that. It's a new day, so we get a whole new window! Ultimately, I'm lookin' to have us create a thinger that might be useful with respect to this here forum's whole mission statement: fungals! But, first, we need to go over a few things. Namely, operators.
"What, like for a train?"
I mean, not NOT for a train. Modern trains probably have computers or some shit, so operators ARE involved. But, no, I mean ways of comparing and doing shit with variables. For now, let's just look at comparison operators:
Code:
╒══════════╤══════════════════════════════╕
│ Operator │ Effect │
╞══════════╪══════════════════════════════╡
│ X == Y │ Tests if X equals Y │
╞══════════╪══════════════════════════════╡
│ X < Y │ Tests if X is less than Y │
╞══════════╪══════════════════════════════╡
│ X > Y │ Tests if X is greater than Y │
╞══════════╪══════════════════════════════╡
│ X != Y │ Tests if X does NOT equal Y │
╞══════════╪══════════════════════════════╡
│ X <= Y │ Tests if X is less than OR │
│ │ equal to Y. │
╞══════════╪══════════════════════════════╡
│ X >= Y │ Tests if X is greather than │
│ │ OR equal to Y. │
╘══════════╧══════════════════════════════╛
(Yes, I did copy/paste all those special characters by hand. Why do you ask?)
Pretty straightforward stuff, yes? The first one might trip ya up, since ya seen the last few examples just using a single "=." A single equal sign is used for assignment. No, not school. As in, assigning a value to a variable like we've been doing. Python (and many other languages) differentiates between assignment and equality comparison using a double-equals. In fact, I'm struggling to think of any languages (except maybe COBOL) that DON'T do that.
Two equals for a test, one for the rest.
Pretty simple, thus far, wouldn't you agree? With these, we can perform the comparisons necessary for conditions to actually, ya know, do stuff. Thus, we come to our first proper keyword in Python (
print is technically a built-in function), which is
if. The brain of any program, the beginning of true ability, starts with
if. So, let's see what we can do with it!
(Also, for this and future posts, I'll prolly cut down on the screenshots. It's way less effort to just type shit.)
Code:
x = 12
if ( x < 15 ):
print( 'I done did the do!' )
This is where we hit up against that "one little bugbear" I mentioned in Part I. It's indentation. Ya see, a lot of languages mark different areas (usually called "blocks") of code with something like curly braces ({ and }). Python doesn't do that. Instead, it uses indentation levels; that is, blank space at the start of a line of code. This is something you NEED to know, because not properly indenting shit will make Python quite narky, causing IDLE to throw up an angry red error message. Those are scary. Let's avoid those if we can.
Thankfully, Python's pretty easygoing when it comes to what constitutes indentation. You can use tabs, you can use spaces. You could use three tabs per level, or six spaces. It doesn't matter,
so long as you're consistent. Me? I use two spaces, personally. Other folks use tabs or 4 spaces, but those just take up too much visual space, for me. Two spaces is a nice, happy medium between the unbridled chaos of one space and the vast wasteland of 3+ spaces and/or tabs.
But, use whatever your little heart desires. Again, just make it consistent, and Python shouldn't bitch.
Got all that? I know it was a lot to take in all at once (THAT'S WHAT SHE SAID!), but it's necessary as we go on.
Moving on to the actual code, let's do every high schooler's favourite thing: D I S S E C T I O N
So, what we're telling Python is to make a variable "x" and assign 12 to it. Then, if "x" is less than 15, print the message. Since 12 is less than 15, it goes! Or, it should if it knows what's fucking GOOD for it.

See? Pretty dang simple. We're building the foundation of literally ALL programming, right now. You heard me right, it's all
turtles if statements all the way down, baby! FUN FACT: those fancy LLMs? Yeah, they're basically fuck-off big boxes of daisy-chained if statements. In the words of the chirren, I'm being deadass, right now.
Like I said: if statements all the way down!
But, we're gonna need a way to package all this neat shit we're learning. A way to bundle it up so we don't have to keep typing the same shit over and over again. Wrap it up like
your pecker a Christmas present! That's where
functions come into play!
Functions pack blocks of code into a nifty little package that we can use later!
print is actually a function, that's why it's got them thar parentheses. We've been calling function
s this whole time!
Let's try and write our first ever function, shall we?
Code:
def TestNPrint( x ):
if ( x < 15 ):
print( 'Hahaaaaa! Suck on MAH PACKAGE!' )
Now, you're prolly wondering: "the fuck's that x doing inside those parentheses?" That's a valid question, actually! That's what we in the biz call
an argument. No, not the kind that goes on when someone's being an asshole. Arguments (also called "parameters") are just variables you give a function when you call it. Again: they're something we've been using this whole time! The strings going to
print? Those were arguments!
This function does much the same as the code we did above: printing a message if "x" is less than 15. BUT, instead of writing out the if statement and everything else out every. Single. Fucking. TIME. We can just do this:
Code:
TestNPrint( 12 )
That will call the
TestNPrint function we made, passing it the number 12 as we do. That is much nicer, ain't it?
Now, there's two last things I wanna go over before
fucking off and having a beer callin' it a day. They would be the
return and
else keywords. The former can be used at any point in a function to immediately make it stop and spit out a value that the code callin' it can use. The latter? It basically tells Python to do the code inside it if the previous test fails. Kind of a "do this if this is true, otherwise do this other thing" dealie. Let's see 'em BOTH in action! (cue sultry jazz)
Code:
def TestNReturn( x ):
if ( x < 15 ):
return 'Well, dickhead, I made it, even with your bullshit directions.'
else:
return 'Quit using Apple Maps, ya shit-stirrer.'
Ooh. Now we're gettin' somewhere, ain't we? Let's see this fucker in action!
Code:
msg = TestNReturn( 12 )
print( msg )
Since we're passing
TestNReturn the number 12, it'll set the variable 'msg' equal to 'Well, dickhead, I made it, even with your bullshit directions.', and then print 'er out! You could change that 12 to 15 or above to get the other one!
See what I meant when I said you only really needed to be able to read and follow basic logic? This is, no joke, almost all programming in a nutshell. Yeah, there's other shit layered on top of it, but it all boils down to logic, functions, and loops.
...Wait.
I didn't talk about loops, did I?
SHI—!
Next time on Friends..."Joey gave me the for-loops again!"
<THIS SPACE RESERVED FOR A LINK TO PART III>