SotK's Python Tutorials - Part 1: Introduction, Basic Maths and Variables

Started by Son of the King, May 08, 2011, 02:37:31 AM

Previous topic - Next topic

Son of the King

SotK's Python Tutorials
Part 1: Introduction, Basic Maths and Variables




Introduction:

This tutorial intends to explain the basics of programming, and also teach you some Python to use this knowledge with. Mainly the Python part, since the basic "building blocks" of programming are fairly easy to understand.

To start off, if you want to program in Python, you're going to need to download Python. Since this tutorial will be using Python 3.x you will need to download the latest version (3.2) for the operating system you will be using. I also suggest you install the IDLE GUI so that you have a basic environment in which to develop your program, rather than relying on Notepad and Command Prompt (assuming you are running Windows).




Some Basic Maths:

Once you have Python installed, you're probably going to want to do something with it. Firstly, open IDLE to bring up the Python Shell. As a very basic demonstration, enter a basic sum and press return. You should see something like this:

Code (First Maths) Select
>>> 2 + 1
3


Multiplication is done with an asterisk (2 * 4) and division is done using a forward slash (4 / 2). Addition and subtraction are done using the +/- keys with the same syntax. Also, ** is used to find the square of a number, for example 2 ** 3 reads 23.

Code (Basic Operations) Select
>>> 2 + 1
3
>>> 3 - 2
1
>>> 2 * 4
8
>>> 4 / 2
2.0


Notice the result of 4 / 2 was given as a fractional number (i.e. with decimal places). This is because / actually performs a 'floating point division', that is it returns the result as a floating point number (more on this later) rather than a simple integer, even if the answer is a whole number as in the example. The operator for an integer division is // . Integer division has drawbacks if used for mathematical calculations, in that fractional answers are rounded down to the nearest integer.

Code (Integer Division) Select
>>> 4 // 2
2
>>> 3 // 2
1
>>> 1 // 2
0





Suddenly, Variables!

As I'm sure most of you know, the logical next step from doing maths with numbers is to do it with letters! In programming, as in maths, using letters in place of numbers is perfectly possible. Go ahead and try. Just try to add a letter to a number like you were adding numbers before.

Code (Oops...) Select
>>> a + 2
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    a + 2
NameError: name 'a' is not defined


Your first (probably) error! This is because a + 2 confuses the interpreter. The a refers to a place in the computer's memory where a variable is stored. A variable is simply a piece of data. It can be a number, a letter, a true/false (Boolean) value, or even a string of letters. There are many other data types and structures that you can use as variables to make your program work how you want it to.

To fix the error, you first need to define a as a variable. To do this, just type a = 5 (or any number).

Code (Defining a Variable) Select

>>> a = 5


You can now type an expression such as a + 2 and the correct answer will be returned. In Python, you do not need to declare the type of your variable when you are writing your code. This means that you can use a as a number, and then use the same variable as a boolean (true/false).

Code (Variables) Select
>>> a = 5
>>> a + 2
7
>>> a = True
>>> a
True


Note that True is a keyword in Python, whilst true is not. Capitals are important. The same is true of variable names, MyVariable is not the same as Myvariable.

Variables can also be assigned the value of a different variable. For example you could do this:

Code (More Variables) Select
>>> a = True
>>> a
True
>>> b = a
>>> b
True
>>> b = False
>>> b
False
>>> a
True


Note that changing b did not change a. You can also write an equation such as y = x + 5 to define a variable. Integer or floating point variables such as this work just like the numbers did earlier.

That's the lesson in basic algebra and maths over with, the next section will cover programming constructs such as the IF statement and various loops, as well as actually writing a basic Python program rather than messing around with stuff in the interpreter.


Jubal

Sweet, I've been considering messing around with some Python for a while. :)
The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...