Showing posts with label lesson. Show all posts
Showing posts with label lesson. Show all posts

Thursday, 24 March 2011

Python: Interaction and Variables

The majority of programs require some form of question and answer based interaction. And in Python it is a must know, it is also very easy to get to grips with too.

All variables have to be defined so they can be called up at a later stage in your program, this itself is not overly useful (other than perhaps when a large variable consistently comes up, like Pi).

This is no different with variables that need interaction. It is used so that whatever value the user inputs can be called up at a later point, makes sense right? There are only two main things you need to remember when setting interactive variables is whether the answer you want will be numerical or text based.
name = raw_input ("What is your name? ")
age = input ("How old are you? ")
The key differences here are 'raw_input' and 'input'. You use 'raw_input' for text based data and 'input' for numerical data, easy. The name is just what you apply to the variable that you use when you call it up, just remember that once the user inputs a name to this interactive variable it will not automatically be displayed, in order to do that you must print their variable, my example should make more sense.
name = raw_input ("What is your name? ")
age = input ("How old are you? ")
print "Hello,", name, age, "is a great age!"
This small program would look like this when ran:
What is your name? Mersenne
How old are you? 16
Hello, Mersenne 16 is a great age!
Remember how that if you want to show the solution to a mathematical problem you separate the problem with a comma? Well that's the same with variables you have defined, if you can remember these few simple rules you can begin to create more complex programs that involve user interaction.

Sunday, 20 March 2011

Python: Mathematical Terminology

This will be my first actual "mini-python" lesson, I'm going to assume you've read my previous post on Python, if you haven't please read it: an introduction to Python. Got everything you need downloaded? Then I'll begin.

Again this is just going to cover the absolute basics, but they are incredibly vital to know. Maths is the core behind the majority of programs, regardless of what you want to program you're bound to come across maths at some point when programming (especially with my posts, seeing as I am mathematical nerd).

Most of the mathematical characters are fairly obvious; "*" meaning times, "+" meaning add, "-" meaning minus, "/" meaning divide and "=" meaning equals. Some of the less obvious ones are to the power of, "**" and to show the remainder when divided is "%". But once you can remember these it's very easy to apply these.

Another important thing to remember is that if you want to 'print' the answer to mathematical you do not use quotes, if you just want to show the expression you do.
print "2 + 2 =", 2+2
Will show:
2 + 2 = 4
Simple right? Just remember to separate the different strings you want to print with a comma. Now let's create a basic program using these concepts.
print "The area of a circle with radius 15 is", 3.142 * 15**2
That would show:
The area of a circle with radius 15 is 706.85834715
Try this out with different numbers, expressions and signs. Only through doing this yourself will you begin learning. In the next lesson I will bring in variables and begin coding a small working program where the user inputs data.