Friday, September 10, 2010

Operators, Operands, and Hello, World!

Good morning! I couldn't sleep so I'm going through my Python tutorial before work. This morning we'll talk about manipulating variables. We used operators and operands yesterday, but I did not explain them. Operators are symbols, like + and - that manipulate data. The data being manipulated is the operand. So in the statement:

>>>x*3

We are multiplying the variable x (The value of a variable is substituted prior to evaluation by the computer) by 3. 3 and x are operands and * is the operator.  You can remember this easily by remember the operator of a piece of machinery is the person doing the work. In this case the * is doing the work on the two operands. The following are legal operators:

** = exponent/power
* = Multiply
 / = Divide
+ = Plus
 - = Minus

Another operator we have used already is the assignment operator or =. Using = assigns the value on the right to the variable on the left. It is similar but different from the equals sign which we will go over later. First let's remember the different types we have gone over (int, float) and let's add another one 'str.' Str, or string, types are strings of characters such as "Hello, World." The "Hello, World" program is a popular first program and so we can use it today to create our first saved program while learning about strings.

First open IDLE (Python GUI). Select File>New Window. A new window with a text editor pops up and here we can write our program.

##This is a Hello, World! program.
##It prints "Hello, World!" on the screen.
print "Hello, World!"

*Note - Remember the # is the comment designator and is not necessary, but a tip for later when writing longer more complex programs is to always include good notes in your program. You can include these comments for your own benefit, put comments of your own or simply start with print. 

Select File>Save>> HelloWorld.py (or any name followed by the .py extension) >Save. Now, let's run our program! Do this by selecting Run>Run Module, or pressing F5. If you have made changes it will let you know you need to save it to run it. When you click Run Module a Python Shell will pop up if you didn't have one up already and you should see this:
Hello, World!
 
You now have a program you can run over and over again as long as you want! It doesn't do a whole lot, but it's all yours. Hope everyone has a great Friday!

No comments:

Post a Comment