Programming Class - Lesson 1 - Basic Expressions
So begins Lesson One - Basic Expressions
In the previous message you wrote your first program, the traditional “Hello World.” How does it feel to be a computer programmer?
Not all programs are so simple of course, though you’d be amazed at what you can do with a few lines of code.
Computers are very dumb machines. People think they’re smart but they’re not - clever programmers write software that make computers look like they can think. In fact, computers can do only one thing - Follow explicit and clearly described instructions.
When I took my first programming class we used a program called “turtle” which is now called “logo” (you can download this for your computer and experiment with it if you like - it’s a free program). There was a blank screen with a small triangle in the middle (known as a turtle). We gave the turtle instructions like, forward 100, left 90, forward 100, left 90, forward 100, left 90, forward 100. When it followed our instructions it left a line showing where it had been. The above program steps caused it to draw a square.
If I had made a mistake and typed left 9 in stead of left 90 then it would not have made a right angle at one of the turns and I’d not end up with a square. The computer could not interpret what my intent was, it only followed instructions.
For this reason, when you write your programs you’ll want to be sure to clearly describe each step the computer should perform. That leads us to the most fundamental concept in programming - the expression.
An expression basically means a step in your program. Here are some examples:
a = 5
print "hello world"
6 < 10
download_pictures_from_facebook()
In typical programming, one line of code often includes several statements. For example:
if x > 10: print "%s is too large, please try again" % x
So lets do some practice. Open up PythonWin and go to your interactive window.
We’ll practice with some statements. Please do each of these, I’m going to introduce some interesting programming concepts.
In the below lines of code I’ll prefix lines you type at the interactive window like this:
>>> x = 10
That means you should actually type:
x = 10 [press return]
Don’t put any extra spaces on the beginning of your lines and don’t type the >>>
In some cases your statements will produce output. I’ll show you just what you’ll get on the interactive window like this:
>>> print "hello world"
hello world
That means you should have typed exactly:
print "hello world" [press return]
And the console would have shown you the words
hello world
on a new line without the >>>
I hope this is clear, if not, let me know. Now lets begin:
>>> x = 10
>>> y = 19
>>> x < y
True
>>> x > y
False
>>> x + y
29
>>> y - x
9
>>> y * x
190
>>> y / x
1
Let me explain what we’ve done above. First, we’ve stuck with some of the most fundamental concepts. I’ll explain each
Variable assignment
x = 10
Now, any time you use the letter x python will use 10 instead. You can later change the value of x by reassigning:
x = 9
** IMPORTANT: variables are case sensitve. ** X = 10 is different than x = 10. Likewise temp = 2 is different than TEMP = 2 or TemP = 2.
Logical comparison
Logic is a very special word in programming. It usually means an expression that ends up being True or False. By now you should be pretty comfortable with the mathematical concept of less than < and greater than >. You’ll use this a lot.
Logical comparisons evaluate to True or False. So above:
x < y is True because 10 is less than 19 and x > y is False because 10 is not greater than 19.
Here’s an example of how you may one day use logical comparisons:
age = get_age()
if age < 18:
user_cannot_vote()
else:
user_can_vote()
There are more logical operators but we’ll introduce them later as we use them.
Arithmetic operators:
addition is +
subtraction is -
multiplication is *
division is /
This may be a surprise because our last example earlier was this:
>>> y / x
1
You might have been expecting the answer to be 1.9. I’ll save the detailed explanation of this for the next lesson but realize that Python has two different kinds of numbers - Integers and Decimal numbers. Integers have no decimal places so if you divide one integer by another integer the answer will also be an integer and any remainder will simply be discarded. Note that it does not round up to 2, it simply chops off (also known as “truncate”) the remainder completely.
Here’s a new mathematical operator:
% is the modulus aka modulo operator. It works just like divide except it returns the remainder only:
>>> 19 % 10
9
>>> 5 % 2
1
>>> 6 % 3
0
Also:
** is exponential operator: 2 ** 4 means 2 to the 4th power = 16 (2 * 2 * 2 * 2) To do a square root you’d use .5 as your exponent. Cubed root would use (1.0/3) as the exponent
>>> 9 ** .5
3.0
>>> 27 ** (1.0/3)
3.0
The parentheses work just like in Algebra. In the example above they simply ensure that the 1.0/3 happens before the ** operation.
QUESTION:
So why doesn’t the following example give the answer of 3?
>>> 27 ** (1/3)
1
But this does:
>>> 27 ** (1.0/3)
3.0
EXERCISE: (copy and paste your code you typed for the answers)
- What is the square root of 8281?
- If k is 10 and m is 7 and p is 4 less than k what is p times k?
-
What do you think this will do (answer before you try it):
“hello world” * 3
3b. There’s something funny with the output from the above statement, how can you make it look nicer?
- Tell me something interesting you’ve learned on your own after
experimenting with the above material.
Bearfruit
Comments
8281**.5
8281**.5
91.0
k=10
m=7
p=k-4
p*k
60
3b. “hello world ” space after d
Post new comment