My Xfire profile

Archive for the ‘Code’ Category

Python Tutorial 2 - Numeric Expressions

Tuesday, March 18th, 2008

Python programming language logoPython’s interpreter can be used as a calculator quite easily. Of course, this is somewhat pointless since there are calculators built into most operating systems anyway. However, the numeric expressions that are built into Python become quite useful in general applications and are important to know.

Before you can start learning Python, you will need to install it.

The install is pretty straight forward, once you’ve got that done, you are ready to go.

Numeric Operators in Python

Python uses all of the normally expected operators, much the same as most other languages:

+ addition
- subtraction
* multiplication
/ division

Now, using these operators, we can type directly into the Python interpreter, IDLE, and it will return the result:


# Addition:
2 + 2
# Will return 4.

# Subtraction:
4 - 2
# Will return 2.

# Multiplication:
2 * 2
# Will return 4.

# Division:
4 / 2
# Will return 2.

Note that a # indicates a comment in Python.

If you need to take things a bit further, there are more operators commonly used in Python.

** is used to indicate to the power of, such as 8 to the power of 4, or 4 squared and so on.

Additionally, you can use brackets to override the default order that the expression will be interpreted in, the same was you would on a calculator.


# To the power of:
8 ** 4
# Will return 4096.

4 ** 2
# Will return 16.

# Using brackets to change the order:
4 * 2 + 5
# Will return 13, whereas.
4 * ( 2 + 5 )
# Will return 28.

Functions that you may find on a scientific calculator are available as either predefined functions, or need to be imported from a library.


5 + abs(-5)
# The "abs" function will return the absolute value,
# so this returns 10 rather than 0

abs(5 * -5)
# Returns 25 rather than -25.

Importing Additional Functions

Other less common operators are not loaded into Python by default, and so they have to be imported from a library of functions. This can become time consuming when you are writing directly into IDLE. If you open up a blank Python window (File > New Window), you will be able to type as many lines of code as necessary before sending it to IDLE. If you are writing expressions such as the ones above into a separate window, you will need to add a print command before hand, otherwise Python will just evaluate them and not display them, for example:


print 4 - 2
# Should be used so that IDLE will print the result,
# otherwise it will just interpret it without printing it.

To load less common operators, such as the square root operator, you have to load Pythons math library. If you just a single function, such as square root, this can be done as follows:


from math import sqrt

# The square root can then be found with this command:
sqrt(25)
# This will return 5.0.

If you need to use multiple, less common functions it may be easier to load the entire mathematics library.


import math
# This tells IDLE to load the mathematics library.

Once the library has been loaded there are quite a few more functions available, similar to those used in C.


math.ceil(2.2)
# The ceiling function will round the decimal up.

math.floor(2.2)
# The floor funtion will round the decimal down.

math.pi
# The constant, pi (3.1415926535897931).

math.pi*2
# Multiplies pi by 2.

pow(4, 2)
# The power function works similarly to the ** function.

# pow(4, 2) returns the same answer as 4 ** 2.

There are many more available including angular, hyperbolic, trigonometric, powers and other mathematic functions. A full list is available in the Python documentation.

Fractions

So far, we have mostly seen whole numbers as a result to our answers, why do you think this is? What happens if you tell IDLE to interpret the following expressions?


2.3 + 5.9
18.0 / 7.0
18 / 7

The answers and explanations in the next Python tutorial, Integers and Floats.

Thanks for reading, if you have any questions or comments, feel free to let me know below.

Popularity: 49% [?]

Share and Enjoy:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • TwitThis
  • Mixx
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

Python Tutorial 1 - Getting Python

Friday, March 14th, 2008

Python programming language logoPython object-oriented programming language at uni in ITB001: Problem Solving and Programming, I figure what better way to solidify what I am learning than to write about it. Someone might find it useful as well.

So if you see anything that is wrong, or anything like that, feel free to point it out, I won’t bite.

First up, you have to get Python, this is pretty straightforward, but I may as well make it the first of my tutorials since it is necessary.

If you use OS X, it comes with it, as do some Linux distributions, though it may be outdated. Grab the latest version for OS X, or Unix based operating systems, or Python for Windows from the Python website.

The install is pretty straight forward and should have you up and running in no time, just follow the instructions, all of the defaults should be fine.

After it is installed, when you run the Python Shell (IDLE), it should bring up a window called IDLE. This is Python’s interpreter window, so anything you run will be evaluated here. You can type directly into IDLE, however it is limited to writing a line at a time which is slow and painful and cannot be saved. It is really only effective for checking a line of code here and there.

If you go to File > New Window, it will open up a blank window. These windows can be saved as .py files and are where you will write the majority of code. You can write as many lines as you want in these and then have Python evaluate them whenever you are ready.

If you want to get straight into Python, the best documentation I have seen personally, and also as recommended by my lecturer, is the documentation material available on the Python website.

I will be going through Python in the same sequence as I have been going through it at uni. So stay tuned and the next tutorial will be up soon.

If you want to leave any thoughts or input, it would be greatly appreciated.

Go on to the next tutorial, Numeric Expressions In Python.

Popularity: 20% [?]

Share and Enjoy:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • TwitThis
  • Mixx
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

CSS Tables - Niceties

Wednesday, March 5th, 2008

I am looking forward to release of Internet Explorer 8, and what will hopefully be a reasonably quick take up of it.

In the myriad of new standards compliant features is CSS table support. I’ve used these countless times in Firefox, Safari and Opera, only to find that I have to completely rewrite the CSS, far more complexly, in order for it to work in Internet Explorer and look fairly similar.

Sure, I could just use HTML tables, but they feel clunky, restrictive and outdated to me. They have their uses, and layout design is not one of them.

Float tags do their job, that’s for sure, but they require far more code and are in general far more complex to get the same effect as the simple CSS tables which are far easier to use than HTML tables as well.

The new TerraMedia website that is currently under development was written using CSS tables initially. To get the same effect in IE6 and IE7 without using HTML tables, I’ve had to add a additional background image that would not have otherwise been needed, as well as use completely different CSS for the design that took twice as long to write.

If you haven’t used, or even heard of CSS tables before, they are really quite simple.

Say you had the following HTML:


<div id="container">
<div id="left"></div>
<div id="centre"></div>
<div id="right"></div>
</div>

If you used CSS tables, all you would have to write in the stylesheet is this:

#container {
disply: table;
}
#left, #centre, #right {
display: table-cell;
}

Then you can go and specify any further styles however you want them. That’s all there is to it. That will give the exact same appearance as a three-column HTML table. The difference being that it is far easier to change the appearance of if you need to, as well as the positioning and so on.

For more information on CSS tables, there is a blog about it by Kevin Yank over at Sitepoint.

Of course, without complete browser compatibility, using these does pose a problem. With IE8 coming up though, hopefully it will become easier to utilize this excellent tool.

I noticed some of the commenters over at the Sitepoint blog found that the sample tabular website there didn’t always render correctly in Firefox, sometimes pushing the third column below the other two.

I haven’t had this experience myself, but Kevin posted a fix, saying that Firefox has a long-standing render problem that requires the

display: table-row

tag to be added to an additional containing div, within the original one. The additional tag should do the same as the HTML <tr> tag. I however think that the problem with his example could be fixed if he used either all fixed widths or all percentages. As he uses a combination of both, where the percentages add up to 99%, I think this would be the cause of the problem. I have found in my own usage of CSS tables that the use of widths is no different to the use of widths in floats and divs in general, if the widths add up to more than the width of the screen, it’s going to either go off of the screen, or it’s going to get pushed below everything else if it exceeds the width of it’s containing element.

It will be a while before they will be widely usable, considering how widely IE6 is still used, even after how long IE7 has been out. However, it is a step in the right direction and will hopefully result in some nice new standards compliant designs.

Popularity: 20% [?]

Share and Enjoy:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • TwitThis
  • Mixx
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

New and Fixed Things

Wednesday, February 20th, 2008

FractalOkay, as I mentioned before that I had fixed the margins, they are definitely fixed and working in everything now.

I have also fixed the comment boxes so they are now aligned to the left rather than the centre as Tim suggested. I forgot to have a look at these since I am almost always logged in and don’t actually see them, so thanks Tim.

I have also fixed the first post on the index page always being a fixed length unless the post is longer than the length. I was having trouble figuring out the cause of this since it only started happening after adding the Adsense code, but it only happened on the index page, not on any other pages. It turns out it was just a simple clear: both; style on a div at the end of the post text that is only present on the index page. I’m not sure why I originally put it in there, but removing it seems to have fixed it all up without causing any problems.

On another note, something that’s not a fix, I have added a 2050×1680 section to the fractal gallery. I’ve never rendered fractals this big before, in part because the time it used to take was simply not worth it since I had no use for them anyway, but really, seeing them this big is stunning. Thanks again to Tim for suggesting doing them at higher resolutions.

There is only one fractal in there at the moment, but more will follow soon enough.

Enjoy.

Popularity: 7% [?]

Share and Enjoy:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • TwitThis
  • Mixx
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

Wordpress 2.5 Demo

Wednesday, February 20th, 2008

Wordpress 2.5 is in it’s alpha stages at the moment and is looking quite interesting.

One of the biggest changes is that the admin interface is getting a complete overhaul and is looking very nice indeed.

Chris Johnston has setup a demo site where you can try it out.

This may not be of interest to everyone, but for anyone that uses Wordpress, it is definitely worth a look.

If you haven’t used Wordpress before and want a look anyway. It doesn’t explain how to access the admin control panel from the main page. Simply add wp-admin after the / at the end of the url and you will be at the login screen. Or go directly to the admin control panel at http://wp.chrisjohnston.org/wp-admin. You will need to check the main page, linked to above, for the login details.

Enjoy.

Popularity: 5% [?]

Share and Enjoy:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • TwitThis
  • Mixx
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

Still As Life is proudly powered by WordPress
Entries (RSS) and Comments (RSS).