Introduction to Python

Table of Contents

Part 1: Python on a local computer

Getting started

To get started, create a directory called intropython. Do so in a terminal window. Feel free to go back and peek at the UNIX tutorial if you need to jog your memory on how to do so. (Don't use Finder or Windows Explorer to do this; you'll be much faster and will be able to do much more with the computer if you push through for a while and use the command-line prompt as much as possible. Ask for help if you like.) Use the cd command to navigate to the intropython directory that you have just created.

You'll need to figure out is which text editor you'd like to use. Word processors (like Microsoft Word, Apple Pages, LibreOffice Writer, and so on) are terrible for writing programming code because they insert all sorts of formatting details that are irrelevant to programming. Bare-bones editors (like TextEdit, Notepad, and so on) suffer from the opposite problem: they're so limited in functionality that you can't do anything you'd actually want to do with them. There are a variety of text editors that work well for programming purposes. We have a number of them installed in the department: you can choose which one you want to use. Here are some choices.

  • Brackets is a free and well-designed editor that's good for programming, and is our departmental catch-all recommendation. If you don't have a particular reason that you want to try using a different editor, use Brackets. In the department labs, there should be an icon for it in the dock that like two brackets, i.e. []. Or you can find it by looking within /Applications/CarletonApps. Since it's free, you can also install it on your computer.
  • Atom is another free editor that's pretty similar to Brackets. Some people don't like it as much. Some like it better. It's pretty similar to Brackers. They're pretty similar. Feel free to try it if you're board and want a change of pace.
  • I've been using the editor Emacs for a long time, and it's my personal favorite for my own projects. Emacs has something of a steeper learning curve to it, but it is more extensible than most other editors out there. Try it if you'd like to use something that's really configurable and extensible, but expect to run through some tutorials at first. The default keyboard shortcuts are bizarre, for example.
  • One other oldie-but-goodie is the editor Vim, which falls in the same category of Emacs in being a bit tricker to learn at first, but it pays for itself back once you have. It has the advantage of being installed by default on pretty much any UNIX system, and it typically runs in a terminal window instead of in a window of its own. I use Vim regularly when I'm on a strange computer and I've just got to make a quick edit to something. It is totally keyboard driven, so expect to run some tutorials to be able to use it.

Decide on a text editor, and start it up. Feel free to change your mind over the course of the term – learning new tools is fun.

A first Python program

Save the program hello.py into your intropython directory. To do that, right click on the link for hello.py, and choose "Save Link As…" Open up this program in your text editor, and take a look at it.

To run your program, open up a terminal window (there's an icon in your dock), and type:

python3 hello.py

in the terminal. If all goes well, your program should print "Hello, world!" to the terminal window. If this doesn't work, or if errors result, get help from from the prof, the prefect, or a lab assistant.

EXERCISE 1: Modify the program so that it prints out "Welcome to Intro CS, [your name]."

Python input

This time, work with at input.py. Save it to your intropython directory, then read it carefully. Run it a couple of times. (Again, get help if you have any error messages.)

EXERCISE 2: What happens if you enter a number when prompted for your name and a name when prompted for a number? If you get a message, what did the message say? What do you think it means? Put your answer to this question as a comment (a line in your Python program that begins with a #) into input.py.

EXERSISE 3: Look at the last line of the program, and look at the output that actually gets printed when you run your program.

  • What actually gets printed when you type \n ?
  • What actually gets printed when you type \\ ?
  • What actually gets printed when you type \' ?
  • What actually gets printed when you type \" ?

Put your answers to these question as a comments into input.py.

EXERCISE 4: Modify the program so that it asks for the current year, and the year you were born. Your program should then print out the difference between the two, which is (approximately) your age.

Turning in your programs

Zip up your two programs and the answers to the questions into a single file before submitting. On the department Macs, you can do this at a command prompt by typing

zip intropython.zip *

which will create a single file called intropython.zip. This is the file that you should submit.

Submit your work via Moodle.

Part 2: CodeStepByStep

Overview

Throughout this course, we'll be using a text editor (e.g. Brackets) to do the bulk of your Python coding, which is why I wanted you to get started with it right away. For the short term, though, we'll be using CodeStepByStep to do a series of short targeted exercises in Python. Visit CodeStepByStep, and log in using the account you created in the last assignment.

I'll be asking you to do a series of exercises in CodeStepByStep. First, here are some notes and caveats.

CodeStepByStep sometimes will show a button associated with each problem labeled "Show Solution(s)." If you see it, you should only use this button as a last resort. In particular, CodeStepByStep will not give you credit for solving the problem if you do show the solution. Use it sparingly.

It may very well be possible to find solutions to these problems elsewhere online. You may not find solutions to these problems on other websites and copy them in, even if you cite them. If you're stuck, ask me, a prefect, or a lab assistant for help, or post a question on the course Q&A Forum. All of these places will help you get unstuck without handing you the entire solution, so you can continue to learn by working your way through to the answer.

You should solve these problems individually. You may talk to each other about ideas if you want help, but the code that you write should ultimately be your own. Again, please ask for help if you need it! You may use resources to help you when you're stuck, but these problems should not be viewed as pair-programming problems. I want to make sure that everyone in the course has a solid baseline knowledge of Python before we get too far in.

Problems to work online

Work on the following problems. If you have connected your CodeStepByStep account to this course, I will automatically receive a report indicating all of the problems that you have gotten credit for.

hello_world

inches_to_centimeters

receipt

receipt2

fear_the_tree

escape1 (Note that for this one problem, CodeStepByStep has a weird bug that if your output isn't quite right, it will give you the strange error "problem context error: expected program, but found bare code." If you see this, it just means that your program still isn't putting out the right output.)

escape2

Author: Dave Musicant, Amy Csizmar Dalal

Emacs 24.5.1 (Org mode 8.2.10)

Validate