+254 728 680 519

Data types and Objects Part 1

Have you ever had that friend who tells you there is something in math which she has never applied in life and normally the example given is something like BODMAS? If you are that friend, well all that just changed. Math is very important in programming. We have already covered basically what is programming and in a few words, it is something which gives you output or result based on certain inputs that have gone through processing to give us the desired result using a computer. The processing normally involves Math. If you dint use BODMAS in school to remember which operator precedes what in an expression, well it stands for (Brackets, of, division multiplication addition and subtraction.) In this article, we will talk about the data type-numbers (I hope you have done your homework if not go back and read the previous post). But the main goal is not to focus on the syntax in this case how numbers are applied within python but shed light on the last piece of the puzzle which ‘Python is object-oriented’. Remember we defined Python as a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming. The definition of what is in bold should be at your fingertips, so we remaining with the last piece of the puzzle which is the underlined bit.

Data Types

1. Numbers

  1. Integers – these are whole values or numbers e.g. 2, 3, 4,-100 (a number without fractions; an integer.), and their operation or uses is just like in math as per the examples given in the diagram below using basic operators addition and subtraction.
  2. The others are floats, reals, and complex numbers. In this post, we will look at only integers and floats (decimal numbers to loosely equate it to math). It is assumed the rest are read from the assignments given. Nothing good comes easy my friend. 

An integer is said to be immutable that is it cannot be changed after it is created the opposite is mutable. It can always be altered

Before we dive into the other Number types, let’s spend some time here. Below is a list of all the BODMAS operations when used in python? 

Order of precedence BODMAS operations in Python






            ( 3+3 )*  5=30

          *, /

          Multiplication and division

           3*3 = 9,   22/8=2.75


          Addition and subtractions

            3+3 = 6 ,15-3=12

I encourage you to make your own examples with different mathematical expressions. This is only the tip.

Let's do some of our own. Take a look below;

Let's dig a little bit deeper and see what is happening by introducing higher precedence by using brackets to ensure we are starting with division then next start with multiplications under example one(3*3/4).

The end result is the same if we start with division then multiplication or multiplication then division. Try the second example. is this the case? so how does python calculate this equation? I put the two operators at the same level on our precedence table meaning they have the same weight opposing BODMAS rule where division must come first then multiplication. Python uses left-sided binding which basically means when two operators have the same weight, python computes the left first then right( left-to-right associativity). This is very important it gives you the basic idea of how the program executes code compared to syllabuses that teach 'Do First Do Last' methods as is the case on how I was taught this concept. Top to bottom left to right is how python works and it follows all the rules to the letter and is elegantly built. That is what makes it so cool. Having said that this is not the case for all operators some use right-to-left associativity (we will look into this when we study power operator  '**'). Try the second without changing the meaning it will sink in. So much for BODMAS, well, it seems she was right.frown They always are!

Now get me clear I am not saying BODMAS doesn't give you the correct answer at the end but how we come to this answer is not clear. From now henceforth use the associativity rule depending on the operator you are using. Take a look below

This also applies to addition and subtraction. 

I also want you to notice something. Whenever we have a division operator or float data type within the expressions we always get a float as a result. Try it and see what you get in a multiplication only scenario with only integers using the operators so far covered.

Everything in python is an object- if you get this concept, then you are well underway of being a programmer in a very short period so make sure you grasp this. Objects are data structures that contain data, in the form of attributes, and code, in the form of functions known as methods. To put in a simpler way is that an object has an ID, TYPE, and VALUE. Everything in python is an object and so far we have used integer’s e.g. 2 so in this case our object is 2. From the definition, we expect three things from an object as below.

We are using the function id(object) and type(object) to get the identification memory location of the object and its data type respectively in this case 2 which is an integer.

ID is identification just like your ID Number so objects are always accessed from a specific location which is as is the case from the above diagram where the id of 2 is '1797404944 'and the function type which gives us 'int' to get the data type as python understands it. This identity has to be unique and constant for this object during the lifetime. Just keep this in mind. The ID and type never change in the course of the objects' lifetime but the value may change in mutable data types cases.

The type output of <class 'int'>  means class integer. Another way to define an Object is that it is an instance of a class so think of classes as a factory of objects. So 3,4,5,6 all belong to this class int which forms a number integer types and 3.0, 3.9, 5.6 belong to <class 'float'>  which forms the number float type. Within a certain type, we can do some basic operations with them as we just did. We are able to add, subtract, multiply, divide (if you dint use your interactive shell as a calculator go back and do that now.). Can you divide your name? No, But you can divide your age. If data objects were superheroes integers powers would include the ability to operate on them in this unique way as per our table precedence above. What other classes’ do we have out there well everything in python is an object so we have a bunch of them e.g. letters also known as strings belong to the class ‘factory’ string and boy can you do a bunch of things with strings.

These powers are what we call methods within a class. Guess what! Classes are also objects. a concept called meta-classes. This opens another can of worms, so for now, let's stick with the light stuff. The last thing we need to look at is our attributes. Attributes are what we use to distinguish between the different data types so in this case looking at the data type int from class int and comparing it with type float from the class float, one difference is that integers don’t have a decimal place when floats do. That is what actually makes them decimals. If we insert a decimal value the whole meaning changes and the data is no longer an integer it becomes a float which is the math equivalent of a decimal value. Another example is that these two are stored differently within python but more on that later for now, take a look below;

I have introduced a variable ('a') to what we have learned so far and if you have understood what we have talked about in this post, this answer should come automatically. In the diagram what changed? 20 became 20.0 or 'a' changed from 20 to 20.0. More on it on the next post

Now, this is just part one of a three-post series of us building that car up there using object-oriented programming it's not for shows you know (looks like Volkswagen right? I love german i.e cars and dogs to be specific. )

Also just completed one of our projects. It is fully automated don't have to do a damn thing apart from bug fixes and updates of course, so check it out would love some comments. Click this link - Nakuone wink

Happy learning!



Playing Basketball, Arsenal fan, Python, and coffee!