Monday, July 25, 2011

Super Simple Python #1 - Math and Variables

Wait, wait! Don't groan yet! It's really simple math! Like, pluses and minuses and stuff! No long division, and no integrating differential equations, I promise!

Open up IDLE (look at #0). You'll notice that the lines you type on start with ">>>". That's normal. You'll also notice that I'll start things I want you to type with ">>>" also. That's also normal. You should not, however, type extra ">>>". That's not normal. The line after the ">>>" is the output line. That's also normal. You'll also notice that I say also too much. That's also not normal.

Now some math! Type:

>>> 1 + 1
2

Yay! Math! See? Easy stuff. Now try:

>>> 1 - 1
0

And it works like that with all the numbers too!

>>> 3 + 5 - 10
-2

Ooooo look! It's a negative two!

Now a variable!

>>> red = 3

We call that "assigning 3 to red." You may have noticed that it didn't have any output. That's because the number 3 went into the variable red. Now if we type:

>>> red
3

There's our 3!

We can also do:

>>> blue = 5 - 10
>>> blue
-5

There, we totally subtracted 10 from 5, then assigned that (-5) to blue!

Now I'm going to be crazy and try something:

>>> red + blue
-2

NO WAY!! I totally just blew my own mind right there. I added red and blue and got a number!!! It took the value of red (3) and the value of blue (-5) and added them! That's some sweet skills right there.

No comments:

Post a Comment