Learning PYTHON

 

I. Getting Familiar with the Python Interpreter

 

The Python interpreter is the little window you first open when you open the Python program from the START menu. It is called an interpreter because it translates the words and numbers you write in the window into a language the computer can understand.

 

When you open the python window, you will notice a funny symbol on the left that looks like three little arrow heads: >>>   This is called the “prompt” because it prompts you for a command or an action.

 

A. PYTHON as a Calculator

 

To familiarize you with the Python interpreter, let’s start by using Python as a fancy calculator. By typing at the prompt, you can do mathematical calculations. There are four different mathematical operators to add, subtract, multiply and divide: +, -, *, and / . Here’s a few examples of how to use them. Try them out yourself, and after typing in the operation hit the Enter key.

 

>>> 2+2   #addition

4             

>>> 4*4   #multiplication

16

>>> 16/4  #division

4

>>> 2-2   #subtraction

0

 

By the way, if you type a # sign at the prompt, Python will ignore anything you type after the # sign. This is called a comment and comments are used in all programming languages to remind the programmer why they wrote the program that way.

 

 

EXERCISES

 

1.   Add 17 and 383    What answer do you get? ________

2.   Divide 222 by 11  What answer do you get? ________

3.   Multiply 20 by 0.5           What answer do you get? ________

 

B. Using Parentheses () for calculations

 

When parentheses are used in calculations, they tell python what to calculate first just like in math class. Python works on the calculation in () before it does anything else.

>>> 5*(3+2)    #First Python adds 3 and 2, then multiplies this by 5

25

>>> (5*3) +2   #First Python multiplies 5 and 3 then adds 2

17

 

Try the following:

 

>>> 5*3+2

>>> 3+2*5

 

What does Python do first when you don’t add the parentheses?

 

 

II. Variables in Python

 

Learning to use variables is one of the most important parts of programming. Fortunately, Python makes using variables very easy. In math class, variables represent numbers or values and the same can be true in python. They can also represent numbers or letters, or even more complex things that we will get to later.

 

A. Numbers

Variables are like expandable boxes that can hold values of all sizes. Let’s begin by setting variables to different number values. In Python, variable names are always letters or words that you set equal to something else:

 

>>> x=5   #Here I set a variable called ‘x’ equal to the number 5

>>> x     #Entering x at the prompt, Python tells me x equals 5

5

>>> x=10  #Here I changed the value of x to 10.

>>> x

10

>>> y=200 #Now I made a new variable, ‘y’, equal to 200

 

Once you create variables, you can start to play with they in different ways. For instance you can add, multiply, divide or subtract them just like numbers. Here are a few examples to try out. Set the variables to the values above, and see what answers you get:

 

>>> y+x       

210

>>> x*y       

_____

>>> x*(x+y)         

_____

>>> x*(y+y)

_____

>>> z = x+y          #You can even set a new variable equal to

_____          #a combination of other variables.

 

 

But x, y and z are really boring variable names. In fact, you can make variable names as long and complicated as you like!

 

>>> width = 200

>>> jorge_is_terrific = 3000

>>> small_one = 0.000003

>>> BIG_ONE = 40000000000

 

Python doesn’t like spaces in the names, which is why I use the “underscore” _ character to connect words.  If you put a space in there you will get a SyntaxError like this:

 

>>> small one = 003

  File "<string>", line 1

     small one = 003

             ^

 SyntaxError: invalid syntax

 

 

 

 

Python also doesn’t like funny characters like percentage signs (%) and asterisks (*).

 

>>> big% = 90

  File "<string>", line 1

     big% = 90

          ^

 SyntaxError: invalid syntax

>>> big*=90

  File "<string>", line 1

     big*=90

         ^

 SyntaxError: invalid syntax

 

 

EXERCISES

 

1.     Set x to a value of 2000

Set y to a value of 0.25

Multiply x and y.       What answer do you get? _______

 

2.     Make the variable SPAM equal to x minus y. What is the value of SPAM? _______

 

3.     Divide SPAM by x and then add y. What is the value of SPAM? _______

 

One more thing you should know about variable names is that Python really cares about uppercase and lowercase letters in names. For instance, the variable names SPAM, spam, Spam, and SpaM are all different variables to Python even though they are spelled the same. This is why I like to keep all my variable names in lower case letters so I don’t get mixed up.

 

B. Strings (Letters and Words)

 

Python also has many ways to manipulate and play with letters and words. In Python, words are called Strings because you can string letters together to form words.

 

For example, my name is ‘Scott’, but you can also think of this word as a bunch of letter strung together:

 

‘S—c—o—t—t’

 

Of course, Scott could also be a very nice name for a variable:

 

>>> Scott = 31

>>> Scott

31

 

So we have to distinguish strings from variable names using quotation marks (‘ ‘).

 

>>> ‘Scott’    #Here is a string of my name

‘Scott’

>>> 'does not'

'does not'

>>> 'like'

'like'

>>> 'spam'     #And here is a food I dislike

'spam'

 

 

Just like with numbers, we can make variables hold strings as values. Here are a few examples you can try out at the prompt:

 

>>> name = 'scott'   #Here I set a variable name

# equal the string ‘scott’

>>> food = 'spam!'

>>> emotion = 'hate'

>>> emotion

'hate'

 

The ‘print’ command: Now that you’ve made some variable names, you can use the always useful print command. This command prints out the value of the variable to the screen. You will use this command all the time while programming. Try these:

               >>> print emotion

               >>> print x

 

III. Manipulating Strings

 

B. Concatenation and Multiplication of Strings

 

In Python, you can concatenate strings with the + operator. Concatenation is the process of tying or gluing  strings together to make longer strings.

 

Here are a few examples to try:

 

>>> 'Help' + 'me'

'Helpme'

>>> 'Help' + ' ' + 'me' + '!'  #Notice the empty ‘ ‘ space character

'Help me!'

>>>

 

You can also concatenate variables together if they hold strings as values:

 

>>> word1 = 'Help'

>>> word2 = 'me!'

>>> word1 + word2

'Helpme!'

 

You can also print out very long strings:

 

>>> long_string = 'Can you help me find the cat?\n I think she ran out of the house!'

>>> print long_string

Can you help me find the cat?

 I think she ran out of the house!

 

EXCERCISES

 

1.     What could you add to the word1, word2 examples above to make it read better?

2.     Create a new variable called new_word that equals the concatenation of word1 and word2.

3.     Add the string ‘\n’ to new_word and print new_word. What happens when you do this?

 

By now you have probably noticed that the \n character is an enter character and returns to the next line. This is an example of a hidden character – you don’t see them when you print the string out, but they are there anyway. Another hidden character is \t which is a tab character.

Not only can you concatenate (or add) strings together, you can also multiply (or repeat) strings with the * operator. Try a few examples to see what this does:

 

>>> word1*2

>>> word2*7

>>> new_word