Ok, let’s start playing…err… programming. Open your Python Shell window to type some things. Type the following code exactly as you see it. Then hit the enter key after each line.
If you type the # key in front of some text, the computer ignores you. This is useful for adding comments in your programs that will not be misunderstood as instructions by the computer; or if you just feel like being ignored.
If you noticed I typed the words “hey look a programmer” in quotation marks. Later I used; ‘ ‘ and got the same exciting result. When we hit enter, the computer just repeated what we typed back to us. It printed the characters as output on the screen. A sequence of characters, words, or sentences like this one is called a string. When we use single or double quote symbols to tell the computer what is in our string, we call these quote symbols “delimiters.” We tell the computer that we are entering or ending a string by using single or double quotation marks. The computer don’t care which kind you use at this point. We can now say we have declared or “delimited” the string. In IDLE strings are green and the output here is blue. The error message is red.
You can also add strings together using a math operator. We will talk more about that in a moment. Putting the string “cool” with ‘cat’ produced ‘coolcat’. If I want a space between them when they are added together, I should add one in my string; “cool “ + “cat” or “cool” +” cat” would generate ‘cool cat’
What happened when I typed, “wow?” The computer said;“Blah blah blah…is not defined.”This is the computer’s way of saying; “huh? I don’t understand.” Python attempts to give you an idea of what went wrong. When I typed the word, I did not include it inside of quotation marks to tell the computer that I was entering a string. So, the computer went, “Huh?”
Have you ever asked; “When am I going to use this kind of math?” In programming you should remember some simple math concepts to make your life easier. Don’t worry, I’ll be brief. To begin with, the world of math has animals called “operators” and “variables.”
Variables are like little like boxes or containers to put different things in. In math your teacher may have told you that 1 + x= some other number. The 1 is an integer, (a complete number as opposed to part of a number like ½), and the x is a variable. In programming, you get to name your variable anything you want. You can create an imaginary box with anything you want to put in it, and define/label that imaginary box, (or variable), by any name you choose.
Let’s try it. Let’s imagine a box of chocolate. We want to tell the computer that the box labeled “chocolate” has happiness and joy inside of it. To do this we use the = sign to define what a variable means for the computer.
In the Python shell window type: Chocolate = “happiness and joy”
Hit the enter key.
Now, let’s type the word without quotes;
Your computer should reply, ‘happiness and joy’103%;">You just defined a variable. This is very useful in programming. You will constantly be teaching the computer how to think as you write programs.
Variables are chunks of data stored in the computers memory. There are generally three types of data stored in variables. Variables can be in the form of integers or in a string as mentioned previously. The second type of data, called a float, refers to the non-whole numbers like decimals. Remember to use the = sign to assign a variable. If you want the computer to think the variable x means 5 is in the box named x, then you type:
x = 5
Now the computer holds a 5 in memory and when you type an x, it tells you ‘5’ if you hit the enter key. You can name a variable almost anything and use symbols like the _ underscore. But there are some rules. You can’t use special key words that Python understands as having special meanings. Don’t use these words:
“SyntaxError: invalid syntax...” is Python’s ways of saying, “Hey, you don’t know what you are talking about and neither do I! Speak Python!” Don’t take it personal, as a new programmer you will get used to these insults. You will find your computer has an attitude.
Other than these minor rules, you can name a variable anything you want. To sound more like proper geeks, we don’t always call them “names,” we sometimes call them “identifiers.” Remember identifiers are just names.
Operators do something, like add, multiply, divide, subtract, or compare. Notice that we already used the = sign to assign identifiers. So, we can’t use the equal sign as an operator. Instead we must use == to mean; “equals.” What other useful things can be done with operators? In the Python shell type:
Which is easier? (I could have really used this all those times in primary school when I had to write a few hundred sentences). Operators are life saving tools for the programmer. The * symbol is used to express multiplication in Python. You also had to add a space at the end of “words “ before the closing quotation marks or your 10 words look like one long one. The most common operators are:
You can also use operators with strings of data. Try it. Make two strings with any words you want between quotation marks and add them together. Like this:
Remember that we already used the = symbol to define or tell the computer the meaning of our variables. So we don’t confuse our little computer, we must use = = to express the traditional meaning of “equal to.”
Now try some of these until you get bored. Any math expression will do. But wait! Use only integers; no decimals at this point.
Due to the way a computer deals with floating point numbers, (numbers with a decimal point); you won’t always get the same results as a calculator. It is too complex to get into for a beginners book, so we will have to skip the explanations for now.
For now, remember that Python is not your math teacher; although they may look similar. In math, 7.0 is still an integer because numbers like 0, 5, 177, are integers. But computers are not so clever, so in Python the number 7 is an integer but since 7.0 includes a decimal point; it’s called a floating point number.
These logical operators generally mean the same thing in programming as they do in English. At this level, I will only introduce you to these as new terms and concepts.
Boolean expressions are sometimes called conditional expressions because they are based on the condition that something is either true or false.
Let’s play with some of this new knowledge. Open IDLE and type the following, but feel free to play with your own ideas after you try these:
He better watch it, I just found the off switch! (I told you the computer has an attitude)! Boolean values or “True and False” remarks are very useful in programming.