Computer Science Week 1: The Recounting

Hooray! This is the first of my in-school summaries! First up: Computer Science 417, From Programs to Computer Science.

This course is meant to give people who already understand programming an introduction to Computer Science. The syllabus includes things like analysis of sorting algorithms, time-vs-space complexity, and a bunch of other cool stuff. First, however, the professor (Professor Hausner) wanted to make sure everyone was on the same page.

I have remarked before that learning a new programming language is easy if you already know one - although this is more true of some languages than others. link:https://python.org[Python] is an easy-to-learn language that will be used throughout this course. Even though the students in class all have some prior programming experience, it varies widely. We even have one student who has programmed exclusively in Fortran! So the professor spent this week making sure that everyone knew the python basics.

If you want to follow along, great! Go install python from the link above, or from your package manager footnote:[If you don’t know what that is, don’t worry. You probably don’t have one. I’m just trying to be inclusive of all computer users.].


Typing anything in this typeface into python will (hopefully!) produce the same results that I give here.

Feel free to play around with stuff, try variations on the examples I give, and find outside resources. link:https://khanacademy.org[Khan Academy] has some good tutorials.

Stuff

There are different types of things that you can have in python. 3 is an integer, "Hello, World!" is a string. True is a boolean value. [0, 1, 3] is a list that contains three integers. ["3", 3, [3]] is a list containing a string, an integer, and another list. In many programming languages, there is a restriction that a variable (like x), can only contain things that have the same type. Not so in python! I can say this:


x = 3 print x # This prints 3 x = “Jelly” print x # This prints Jelly —-

There are more types of things in python, but those are the only ones we will need for now.

Loops

In the example above, something wonderful happens. The 4 steps, one per line, happen in the right order. They go from top to bottom. This is good! If our program ran backwards, it would break all kinds of things! But sometimes, it’s easier to write something where the lines can jump around. Enter loops:


for word in [“apple”, “banana”, “dragonfruit”, “turtle”]: print word —-

This prints


apple banana dragonfruit turtle —-

This is, in my opinion, the most important loop in python. It allows you to take a group of things, like a list, and do something for each item in that group of things. It’s even more useful when you consider this:


for i in range(5): print i —-

which prints


0 1 2 3 4 —-

range is a function (see next section) that takes a number, and returns a list of all numbers smaller than that number, in order.

There are other loops (notably while), but there is also another construct that allows you to jump around.


if a == 2: print “Two” else: print “Not Two” —-

if statements let you do something only if a given condition is met. This is super useful, as I’m sure you can imagine. There’s one final big piece of python (and many, many little pieces) that you should know about.

Functions

The other thing the professor talked about in class was functions, and how they work in python. Functions look like this:


def double(x): return x + x —-

This creates a function called double that will add whatever you give it to itself, like so:


x = 5 print double(x) # This prints 10 —-

You can even make functions that call themselves, like this function that computes the factorial:


def factorial(n): if n == 0: return 1 else: return n * factorial(n – 1) —-

There are all sorts of cool things you can do with functions, like making functions that return functions, but that will have to be a topic for another time, because that’s all we covered in class. I’m looking forward to next week, and I hope you are too! Remember, if you have any questions you can email me at link:mailto:setupminimal@gmail.com[setupminimal@gmail.com], and you can find more information on python, as well as great tutorials, at link:https://python.org[python.org], or anywhere internet is sold.