Sunday, January 26, 2014

The beginning of all things

Objects

There are some very basic "data types" that python can manipulate. In this post I will discuss different types of numbers. 

Python is an object oriented programming language, meaning that all data (information) in a python program is wrapped in a "container" called an object. Every object consists of three parts, an identity, a type, and a value. So far, you've had experience with identity and value. When you assigned a number to a variable name, the name was the identity and the number was the value. Now we'll discuss the third property type.

The numbers we used in the birth year calculator were whole numbers or "integers". An integer is a "type" of object. If we wanted to use a number with a decimal, we would have to use a "floating point" number. A floating point number or "float" is a different data type. Both represent numbers even though they are different types of objects. 

There is a method for testing an object's type. We can test this by opening our command prompt. (Windows key + r, then type cmd and press "Ok") Type "python" to enter your interpreter.

Create two variables, a and b. Assign a a value of 5, and b a value of 5.0.



Now divide a by 2. Then divide b by 2. Notice that when a is divided by 2 the answer returned is 2, but when b is divided by 2 the answer returned is 2.5.


In order to test the type of an object you can enter type(object name), where object name is the name of the variable you are testing. Do this now with the a and b variables.


Notice that a is a type int object (short for integer), and b is a float type object (short for floating point). Because they are objects of different types they behave differently. Integer objects are only concerned with whole number values, so when two integer values are divided, any non-whole value is discarded. Even though 5 divided by 2 equals 2.5, the decimal is dropped and the answer returned is 2. On the other hand, when the floating point variable, b, is divided by 2 the decimal point is retained even though 2 is an integer.

When performing a calculation on two numbers, the more complex number will determine the 'type' of the result. 

Integer and Integer = Integer

Integer and Float = Float

Float and Float = Float

Object Methods and Properties

Objects often have different properties and methods associated with them. Properties are kind of like additional variables that exist within an object. If you had an object that represented a person, you might have properties like height, weight, and gender. In the case of our two object (int and float), each has different properties. For example, because a is an int object it has a numerator and a denominator property. To access the properties you can simply type a.<property>.

 

However, b is a float object so it doesn't have a numerator or denominator property. Instead it has other properties, real and imag which our int object doesn't have. You can access these properties the same way.

 

In addition to properties, objects can also have "methods". Methods are a type of function that can be used to manipulate or interpret data within an object. Our float object has a "method" called "as_integer_ratio". In order to "call" the method on b type b.as_integer_ratio(). The '()' are how methods and functions are used in python. If you type b.as_integer_ratio without the () and instead of running the method, python will simply tell you that what you are accessing is a method of float object.


Notice that the values returned from the as_integer_ratio method are 5 and 1. This is because in order to represent the value of b as an integer fraction you would use 5/1. If we set b = to 0.5 and run the method again the return values will be 1 and 2 because 0.5 is equal to 1/2.


In order to see all of the properties and methods available for a given object you can type dir(<object name>). Doing this is a good way to get acquainted with objects that you aren't familiar with. For now, just ignore anything with a double underscore. We will discuss these properties and methods later on. For a more detailed description of the properties and methods of an object type help(<object name>).



Objects are a complex subject. I won't go into the intricacies of them all at once. I will try to describe information as it becomes necessary. In this post we've discussed the main types of number objects, next time we will discuss "strings", getting input from a user, and write our first program.

No comments:

Post a Comment