INTRODUCTION TO PYTHON

Back to index

This module follows a fairly simple plan:
  1. Command Line Orientation - 6 lessons
  2. Turtle Graphics Activities - 6 lessons
  3. Tkinter Toolkit Lessons - 12 lessons
So it'll take a few lessons to get to the fun graphics stuff, but in the meantime there is a lot to learn, so let's get started!

COMMAND LINE:

Although there are various development environments available, in this unit we will be working at the command line. In fact, in this lesson only we will start off by opening a terminal window and typing python at the command line. Pressing return will result in something that looks more or less something like this:

user@computer: python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
Entering commands at the >>> prompt allows you to interactively work with the Python interpreter. It's a great way to try stuff out and experiment. Try typing the following at the >>> prompt:
>>>x=5 >>>y=7 >>>x*y
After that last line the Python interpreter will echo back an answer. Here's the basic explanation for what happens when you type these lines. First, the value five is stored in a variable called x. Second, the value seven is stored in a variable called y. Finally, the phrase x*y instructs Python to multiply the values stored in x and y and report the product. Here's another quick bunch of lines to try out:
>>>duck=47 >>>tree=66 >>>mississippi=100 >>>mississippi-(tree-duck)
As you can see variable names can be just about anything you want them to be, although they can't start with a number, but they can contain a number as we see here:
>>>n9=8 >>>n9*x
Also variables are reusable and so you can have lots of fun doing all sorts of calculations with the six variables (x,y,duck,tree,mississippi,and n9) set up so far in this lesson.

Here's something you can try, which won't make a lot of sense... or maybe it will...

>>> 'yes' if x>y else 'no'
Reverse the greater than sign and see what happens!

Things get even more interesting when you discover that variables don't have to contain number values. Consider the following:

>>>bob="boing " >>>bob*n9
Values such as "boing " are called string values since they contain a string of letters. Obviouisly multiplication of string values is going to yield different results than numeric multiplication. Also it should be mentioned that most numeric operators will not work with string values. Experiment a little and discover what an error message looks like!

An array is another type of variable which you can create:

>>>tvp=["chicken","duck","beef","pork","lamb","venison","turkey"] >>>tvp[3]
To quit the Python interactive interpreter type quit(). Although none of your assignments will be completed in the interpreter, remember that it's a good place to try things out if you want to do a quick experiment. It's a handy and quick way to try something out without writing an actual program, which gets us to the next part of this lesson.

Open a text editor and type the following code:

#!/usr/bin/python print "Getting to know ya..." n1 = raw_input("What is your name? ") n2 = raw_input("What is your favorite food? ") print "NAME: " + n1 print "FAV FOOD: " + n2
Assuming that you save this little program as hi.py, you will have to do the following to get it to run:
  1. Type chmod +x hi.py at the command line. BTW, chmod stands for "change mode" and the +x indicates that you want to make the program executable.
  2. Type ./hi.py at the command line and then assuming that you made no mistakes, you will just follow instructions and then observe the output.

ASSIGNMENT:

Alter the hi.py (or whatever you decided to name it) program so that it asks five more questions in addition to the two included in the sample code. Have the program print out a summary of the input in the same way as the sample program did.