Data Types

One of the things that can make your programs fail to work as expected is the whole “data types” issue.

The computer stores information in different ways depending on what type of data it is. Basically there are three types that we need to be aware of.

Strings. The computer stores these as a “string” of characters. Strings can contain letters, numbers, pretty much any sort of character. The thing is, if you try to do actual maths with a string, even if it just contains a number, the computer will complain.

Integers. This is what we call whole numbers. The computer wants to be as efficient as possible. If we are only going to be using whole numbers, this data type is fine.

Floating Point Numbers (or “floats”). If we are going to need to work with numbers that have decimals, we need to use “floating point” variables

This little program shows what happens if you get the data types confused.

If you enter this program and run it, you’ll see what is happening. When it is treating the variables as strings, adding them together just prints them out one after another (we call this  “concatenating” strings). When I use the format “int(num_1)”, it tells the computer to convert the string into an integer and then do the maths. If you give the computer numbers with decimals, you’ll see an error when it tries to add them.

To avoid this, we can tell the computer to treat them as floats.

Here, “float(num_1)” tells the computer to treat the contents of num_1 as a floating point number. Of course, if we didn’t enter a number when we were asked, we’ll get an error!

The computer tries to convert the string of letters into a number, but of course it can’t, so there’s an error.

Just for the sake of it, here’s what happens if we enter integers, but get the computer to treat them as floats (don’t worry, this won’t cause an error).

The computer is quite happy to convert these numbers into floats, the answer is “98.0” which shows that we are working with floating point numbers.

Before we leave this topic, I will show you what happens if you use multiplication on strings. (You won’t be surprised to hear that you can’t divide or subtract strings!).

As a wise person once said, a computer does what you tell it to do, not what you want it to do!

Happy Coding,

antiloquax

About these ads

Tags: , ,

4 Responses to “Data Types”

  1. Venetta Laukitis Says:

    You have mentioned very interesting points! ps nice internet site.

  2. antiloquax Says:

    Thanks for your comment Venetta!

  3. Eldridge Spreitzer Says:

    Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who knows kindly respond. Thanks

  4. antiloquax Says:

    I don’t know anything about RSS, sorry. I have started a new blog over at http://raspberrypy.tumblr.com/ (co-authored by another Puppy Linux enthusiast). That might interest you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: