My Xfire profile

Archive for the ‘Tutorials’ Category

Bookmark a Page With Javascript

Tuesday, June 3rd, 2008

I have been asked a few times how to add a bookmark link within a HTML page, so that people can just click the link and the website will be added to their bookmarks.

This is pretty straight forward to do, here is the code you would need:


<a href="javascript:window.external.AddFavorite
('http://blog.stillaslife.com/code/bookmark-a-page-with-javascript/','Bookmark a Page With Javascript | Still As Life')">
Bookmark this page!</a>

This will create something like this: Bookmark this page!

All you need to change is the link to be the link to the page being bookmarked, then the second part ‘Bookmark a Page With Javascript | Still As Life’ is the title of the page being bookmarked. This should be whatever you want the bookmark to be titled.

Nowadays this is becoming less common as social bookmarking sites such as those below this blog post are becoming more popular, of course there are still plenty of people using regular bookmarks.

It’s probably also worth noting, to my knowledge, this only works in Internet Explorer. Enjoy.

Popularity: 27% [?]

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 3 - Integers and Floats

Monday, March 24th, 2008

As you saw in the final section of the previous tutorial on numeric expressions in Python, Fractions, the following two expressions give different answers:


18.0 / 7.0
# This returns 2.571485714285716

18 / 7
# This returns 2

So what’s going on here? Well, this is an example of the two main number systems in Python, integers and floats.

A float can be distinguished from an integer because it has a fractional part, even if it is just .0, such as the .0 at the end of the 18 and 7 in the above example.

If you choose to leave out the fractional portion of a set of numbers, Python will always return it as an integer, unless you force it to return it as a float which I will explain in a moment.

If you put in the fractional part, even if it is just a 0, Python will always return it as a float, unless you force it to do otherwise.

Now what happens if you use both a float and an int? Such as 18 / 7.0?

Let’s try it and find out.


18 / 7.0
# I get the same as 18.0 / 7.0
# 2.571485714285716

Why does it do this?

By using a float with an integer, Python defaults to a float for the result.

Forcing a Number Type in Python

Forcing a number to be either an integer or a float is a pretty simple task. All you have to do is add the following to your expressions:


int (18.0 / 7.0)
# This will force Python to return an integer value
# instead of the default float.

float (18 / 7)
# This will force Python to return a float value
# instead of the default integer.

If you need to determine what number type something is, this can be determined very easily with the following function:


type (18)
# This will return <type 'int'>
# In other words, 18 is an integer.

type (18.0)
# This will return <type 'float'>
# So, 18.0 is a float.

If you convert a floating point number to an integer, the fraction is lost, so if you need to use it, it is a good idea to keep your numbers in the floating point format.

Does 1 = 1.0?

You would expect 1 and 1.0 to be the same, and logically, they are, right?

While the integer 1 is numerically the same as 1.0, their value is not equal. You can test this in Python by doing the following:


1 == 1.0
# This asks Python if 1 is exactly equal to 1.0
# It returns True.

1 is 1.0
# This asks Python if 1 and 1.0 hold the same value.
# It returns False.

Floating point numbers are encoded as finite approximations in binary. This can cause some slight rounding inaccuracies to occur that are machine based.

Have a look below at some examples:


1.0 / 3.0
# Python cannot encode the infinite fraction, try it and see.

(0.1 * 3.0) / 0.1
# This should return 3.0, I do not get exactly that though.
# Try it and see what you get. It will be 3.something.

If you have any comments thus far, please let me know, the same goes if you have any corrections or input for the tutorials.

Next up I will be looking at strings in Python, so stay tuned.

Popularity: 59% [?]

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 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: 69% [?]

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: 26% [?]

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).