Hi Queermos! I’m Intern Chloe, I’m a software engineer at a large tech company, and I want to teach you things about programming computers! Programming is one of my top three favorite activities, after eating pasta and responding to Autostraddle tech support requests. Hopefully you might want to program computers too, because there’s a ton of job growth in I.T., and while there are some pretty kick ass queer women programmers, tech is still overwhelmingly dominated by men. Also lots of girls seem to think my ability to program computers is a good reason to send me OKCupid messages, so.
Let’s talk about python. I picked python for our gentle programming introduction because when you squint really hard it allllllmost looks like regular English. There are dozens of programming languages that have different strengths and weaknesses, but once you know how to program in one it’s way easier to learn more. Python is easy to learn, but also super practical and is mostly what I use in my job.
There’s a tradition among programmers every time you learn a new language to have your first program be “Hello World,” which just prints out “Hello World.” That seems a little straight for us, so we’re gonna do “Hello Queer World” instead.
First, we need a text editor to write computer code in. I’m going to be using Sublime Text 2, which you can download for free and works on Mac, Windows and Linux. Once you’ve downloaded and installed, follow these steps:
- Open a new window
- In the menu, go to “View” > “Syntax” and select “Python.” This makes the code pretty colors, basically.
- Also in the menu, go to “Tools” > “Build System” and select “Python.” This tells the computer to read what we write as Python code.
- Then, in your window, type: print “Hello Queer World!”
It should look something like this:
The last thing you have to do in order to run your program is save it. On a Mac you can type command+”s”, or you can click “File > Save.” We’re finally ready to roll. Next, go to “Tools > Build” (or command+”b”) to actually run your code!
Does it print out something like this?
Hello Queer World! [Finished in 0.0s]
Let’s break this down a little bit. print is a special statement in python that allows us to write out information from the computer program when we run it. “Hello Queer World!” is a string. In programming, data can have many different types; a string is anything between quotation marks. Now, try to change your program to print out “Hello <your name>” instead.
Another common data type is an int, short for integer, which is pretty much what you might expect it to be, and we can do all sorts of normal math-type things with ints too. For example, all of these statements are valid:
- print 4
- print 4 + 5
- print -10 / 2
What happens if you instead type print “4 + 5?” Python just reads “4 + 5″ as a string because it is in quotation marks, and so it doesn’t try to do math on it. Your window probably looks something like this:
Next we are going to talk about variables. Variables are a way of storing information that we want to use later on. For example, we could have a variable called my_name that stores a string for us. Then we can write something that looks like this:
my_name = “Chloe”
print “Hello” + my_name
And if you build this, it should print Hello <your name>. Variables can store any type of information; for example, maybe I also wanted to print out my age:
Disaster! This is our very first error message, which is telling us that we can’t add strings and ints together because they are different types. Error messages are really useful when you mess up — they try to give you information to fix the problem.
We can fix this by casting my_age to be a string instead — that is, changing it’s type for the purpose of printing it out:
Great job so far! You are learning so many things! Try making the above print statement more complicated and adding more facts about yourself using variables.
Ok ready for the last new big thing? The next major concept is called a function, which is a piece of code that you can reuse over and over again. For example, say I wanted to print out the name and age of anyone, not just myself. I could individually write out each print statement:
This is going to get really cumbersome because what if more Ellens come out? What then, will we just keep copy pasting code?! We need a function to end the madness.
def print_info(name, age):
Functions always start with a special keyword, def, which means that we are defining a function. Then comes the name of the function, print_info, and then the information that the function takes in — name and age. Below the function definition, we write the code that will be called every time the function is called.
def print_info(name, age):
print name + “is” str(age)
There are three important things to note about every function:
- The name: print_info
- The arguments: name (a string), age (an int)
- What it does when you call it: prints out name and age
A couple of things to notice about this code: mainly, you can think of print_info as a shortcut for the code inside the definition. Instead of having to type out the messy print statement over and over, you can just type print_info with the information you want it to contain. Also, notice how name and age look all orange and strange in the function definition. They are still variables, but a special kind of variable called function parameters that you send into the function when you call it, and then the function can use them like regular variables. Lastly, the line under the function definition is indented several spaces. Python actually cares about proper indentation, which means every time you end a line with a “:” you indent the next line 2 spaces. Sublime is awesome and will take care of this for you magically, but it is still worth knowing.
It’s super ok if this is confusing! Functions are scary and strange at we’re gonna do another example and then you can practice on your own.
Now, try on your own to write a function that takes in two ints as inputs and prints out the sum of them (I know it’s silly; bear with me). Ready go! Scroll below the next cat to find the answer:
Ok did you do it? Here’s my solution:
Cool! Great job! You know so many things about programming now! Though this is possibly the only “learn to program” guide on the internet using cat interstitials, you might want to learn more after this (I hope you do!). I highly recommend code.org for an excellent introduction. If you want to keep going, Coursera has a great selection of free online courses. What do you think? Was it confusing? Do you want to learn more? Were there enough cats? Tell me all your feelings about programming!
This has been the eighty-fourth installment of Queer Your Tech with Fun, Autostraddle’s nerdy tech column. Not everything we cover is queer per se, but we talk about customizing this awesome technology you’ve got. Having it our way, expressing our appy selves just like we do with our identities. Here we can talk about anything from app recommendations to choosing a wireless printer to web sites you have to favorite to any other fun shit we can do with technology.
Feature image via Shutterstock.
Header by Rory Midhani