Wednesday, January 29, 2014

Exercise 1: input and strings

Calibrating the exercises to the audience is going to be a challenging task, so I ask you to bear with me if the exercises are too easy or too hard. Every week there will be a poll you can click on to discuss whether the exercise is too easy or too hard and hopefully in a few weeks, I'll get the level right. Let's get to it! I will start with the exercise and include a discussion later, in case you want the extra challenge.

Exercise(s)

Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.
Extras:
  1. Add on to the previous program by asking the user for another number and printing out that many copies of the previous message. (Hint: order of operations exists in Python)
  2. Print out that many copies of the previous message on separate lines. (Hint: the string "\n" is the same as pressing the ENTER button)

Discussion

Concepts for this week:
  • Getting user input
  • Manipulating strings (a few ways)

User input in Python


To get user input in Python (3), the command you use is input(). Store the result in a variable, and use it to your heart's content. Remember that the result you get from the user will be a string, even if they enter a number.
For example,
name = input("Give me your name: ")
print("Your name is " + name)
What this will print in the terminal (or the shell, whatever you are running Python in) will be:
>>> Give me your name: Michele
Your name is Michele
What happens at the end of input() is that it waits for the user to type something and press ENTER. Only after the user presses ENTER does the program continue.

Manipulating strings (a few ways)


What you get from the input() function is a string. What can you do with it?
  1. Make the string into a number. Let's say you are 100% positive that the user entered a number. You can turn the string into an integer with the function int(). (In a later exercise or two or three there will be questions about what to do when the user does NOT enter a number and you try to do this; for now don't worry about that problem). Here is what this looks like:
    age = input("Enter your age: ")
    age = int(age)
    (or, if you want to be more compact with your code)
    age = int(input("Enter your age: "))
    In both cases, age will hold a variable that is an integer, and now you can do math with it.
    (Note, you can also turn integers into strings exactly in the opposite way, using the str() function)
  2. Doing math with strings. What do I mean by that? I mean, if I want to combine (concatenate is the computer science word for this) strings, all I need to do is add them:
    print("Were" + "wolf")
    print("Door" + "man")
    print("4" + "chan")
    print(str(4) + "chan")
    The same works for multiplication, but division and subtraction do not work like this.

Forgot how to submit an exercise? 

Wednesday, January 15, 2014

Week 0: Installing and Coding Python on Your Own Computer

You can code in Python on at the very least the 3 major operating systems (Mac, Windows, Linux) - I make no guarantees about Android, FirefoxOS, or ChromeOS. To effectively write code in Python, you need three things installed on your computer:

  1. The Python interpreter, or the program that will run the Python code you write
  2. A place to write your Python code, like a text editor or something fancier
  3. Something that can help you install interesting Python packages in the future (this is called a package manager
If you use OSX or a flavor of Linux, you already have the Python interpreter installed on your computer, and you have some kind of text editor to write your code in. If you have Windows, you need to install the Python interpreter no matter what. My recommendation for beginners is to install an IDE (integrated development environment) to write your code in. It is very common for Python-specific IDEs to come pre-packaged with Python, so all you need to do is install the IDE and start coding. The main difference between an IDE and a regular text editor is that an IDE gives a few more tools to the programmer - for example, you do not need to compile and run your code from a terminal if you use an IDE. The choice to use one is up to you, and I encourage you to try out a few different ways of writing Python code to see which one you like better.

I am not going to tell you about package managers for Python yet, but eventually you will need to install one to install interesting Python libraries to play with.


A Possible IDE for Python:

There are many options for IDEs / text editors that you can use - some more customizable, some more package-friendly, some more interactive, some more "hardcore". The long story short is that it doesn't matter what IDE / text editor you use, as long as you have Python installed on your system and you know how to use it. I have listed a number of IDEs / editors here that you can use. The one I recommend for beginners is Enthought Canopy, just because it automatically installs Python, a pretty decent IDE, and a bunch of mathematical packages that are annoying to install manually. The same goes for Anaconda. But ultimately the choice is yours.

The IDE you choose is totally a matter of preference. If you are just starting, I recommend using one of the standard packaged IDEs - Enthought, Anaconda, IDLE, and switching between them. Try all of them and decide which one you like best. You might get to the point where you want to optimize your keystrokes or find yourself doing the same type of operation over and over again. But for 95% of ordinary users, these packaged IDEs work fantastically. Try one out, but be flexible. The only thing you should never do is pay for an IDE - there are plenty of free ones out there that work great, so don't pay for something until you're sure it's what you want.
What do I use, personally? On my Windows machine I have Enthought Canopy, on my Linux machine I use Sublime Text 3, and on a Mac I use Sublime as well. But I used to be a heavy IDLE user when I first started with Python.

Python 2 or Python 3:

You might have heard some of your coder friends talking about this new "Python 3" movement. The short story is this: Python 2.7 has been the industry standard for years, but there were a few major annoyances that caused developers problems. Python 3 was released in late 2008 and has been continuously improved ever since, but it is not yet up to speed with industry standard. The main problem is "legacy libraries" and "legacy code", that is, code / packages that were written back in the Python 2.7 days that were not translated into Python 3. 

Because you are most likely not maintaining legacy code with Python, the recommendation is to learn Python 3 - it is the future of Python, and packages are slowly being ported to Python 3. All the exercises I will post here will assume you have Python 3 installed.

Exercises: 

  1. Install Python 3 and an IDE / text editor of your choice
  2. Figure out how to open it, where to run your code from, and where the output of your code will be. Usually you can figure this out by reading the website of the product, watching YouTube videos, or searching the internet.

Wednesday, January 8, 2014

How it works (how to submit solutions)

Problems

Every week on Wednesday evening (Jerusalem time; early afternoon EST; late morning PST), I will post a small tidbit of Python 3 (an explanation, a library, a sample function) and pose an exercise that relates to the explanation / sample written in the post. The exercise should take no more than 30 minutes to solve and test. I will also try to include links to external websites that explain concepts in a simple way. Because I am not yet sure about the audience of the blog, I suspect that the first few weeks might take some calibration. The more you tell me and give feedback, the better it will be!

If you want to be reminded every week that a new exercise has been posted, enter your email in the email bar to the right.

Solutions

Your solution

At the bottom of every post, there will be an embedded Google Form where you will submit your solutions. The way to submit solutions will be through an anonymous Gist. This is like pastebin, except I can automate some scripts to make processing solutions easier for me. 

All you need to do is go to http://gist.github.com, paste your code into the text box (not the comment box), and Create a Public Gist. You can even do this without a Github account! The link that is created with your code is the submission link that you will put into the form at the bottom of every exercise post. That's it! (Note: if you want to make a Github account and keep copies of your code for yourself as gists, feel free to do that as well). 

My solution

I will post a few submitted solutions and comment on them, so you can see examples of other people's solutions. In my comments I will refer to general coding style and methodology as well as Python coding conventions. In the event that my solution differs significantly from posted solutions, I will post my solution as well. You can check your submitted solutions against the posted solutions to see how you did.

Wednesday, January 1, 2014

The goals of this blog

The impetus for this blog came to me (as most things) in the shower. I have been teaching computer science for MEET for about three months, and have seen astonishing improvement from my students in the realm of computer science and programming. I wanted to use some of the exercises I am using with them with the rest of the world - it feels like a waste to not share my efforts with the rest of the world.

The format: Every week on Wednesday, I will post two levels of exercises in a post on this blog - one aimed at beginners and one aimed at slightly-more-than-beginners. I won't disclose the format of the exercises here, mainly because I want to leave flexibility for myself in making these exercises. But the "solution" will be a program in Python that accomplishes a particular task. Each task should take maximum 30 minutes to solve; the intention is that each exercise is a quick practice.

The audience: Anyone who wants practice with Python, but has seen programming and Python before.

The goals:
  1. Provide a short weekly practice for Python
  2. Encourage independent thinking and learning
  3. Provide feedback / solutions / debriefs of exercises at the end of the week-long period by using user-submitted code.