Home » PythonPi Introductory Course » PythonPi: Lesson Three

PythonPi: Lesson Three

Well then, you’ve done your first programming lesson and now we are ready to introduce you to some further programming concepts. In this course we aim to teach you things as they come in useful. Today we are going to work with some of the ideas that programming allows.

One of the first things you must understand about computers is that they really don’t understand anything the way we do. Everything that happens on a computer is made up of electrical switches.

A programming language allows us to communicate with these switches in a more understandable way. In Lesson Two when you learned to print out Hello World you may have wondered what the point was? And why it took longer to print in a programming language than it did with a word processor. The reason for this is, when you tell a computer to print using Python, you are actually telling the computer to organise the switches in a way that outputs that word to the screen. It’s a lot more complicated than it seems.

Fortunately, you won’t have to understand what every switch does because Python understands for you. Using Python you can write code using logic and real words, which will be converted into computer talk. The only problem is, for Python to understand your words and logic, you must stick by some rules (The Syntax) to make sure the computer understands Pythons instructions.

Variables:

A variable is something that will be used in a program. A variable can be a number such as “10”, a string such as “Hello World” or a decimal number (known as a floating point) such as “15.0”.

To set a variable value in Python we use the “=” key. Equals allows us to set a variable to be something.

For example:

age = 12;

The above line of code will set up a variable called age with a value of 12. That means that if you printed age, it should show 12.  Type the above line of code into your IDLE3 followed by the below line.

print(age);

notice how we don’t use quotation marks on a variable when printing. This is because we don’t want to print “age” but what it contains.  You should see the number 12 displayed instead of the word age.

Now we are going to try using string variables. Set up a variable that contains your name. For example, I might put:

name = “PythonMan”;

Now print out your name followed by the age. You can do this using a comma to separate variables within the print brackets.

print(name, age);

The result I would get is “PythonMan 12” because that is the age and name I had given to the variables.


Leave a comment

Your email address will not be published. Required fields are marked *