### Author Topic: SotK's Python Tutorials - Part 1: Introduction, Basic Maths and Variables  (Read 5002 times)

#### Son of the King ##### SotK's Python Tutorials - Part 1: Introduction, Basic Maths and Variables
« on: May 08, 2011, 02:37:31 AM »
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 + 13`
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 + 13>>> 3 - 21>>> 2 * 48>>> 4 / 22.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 // 22>>> 3 // 21>>> 1 // 20`

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 + 2Traceback (most recent call last):  File "<pyshell#10>", line 1, in <module>    a + 2NameError: 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 + 27>>> a = True>>> aTrue`
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>>> aTrue>>> b = a>>> bTrue>>> b = False>>> bFalse>>> aTrue`
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.

« Last Edit: May 09, 2011, 03:59:06 AM by Son of the King »

#### Jubal ##### Re: SotK's Python Tutorials - Part 1: Introduction, Basic Maths and Variables
« Reply #1 on: May 08, 2011, 10:31:47 AM »
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...