Monday, June 9, 2014

2.2 Types of errors and Mathematical operations in Python Programming Language

This second tutorial on Python programming will give you an idea on what kinds of errors you might face when you program. Also, we will start on the basic mathematical operation you can do on Python. Basically, it sounds like an easy tutorial and since most of us took Mathematics in schools, it should not take long to learn this. However, practice is needed to familiarize ourselves in the Python environment till we get used to it.

So, for the first part, the errors we may encounter when we are programming. As you see from the previous tutorial, the systems some times prompt us an error when we type some thing that Python can not understand. Generally, there will be three types of errors that you might encounter when programming:

1. Syntax error - The first one being syntax error which we see in tutorial one. When you type some thing which does not obey the rules in Python and, hence, Python do not understand what you are talking about. Normally, Python will prompt an error with regards to the invalid syntax.

2. Logic error - This error occurs when you have perfect syntax, and the computer understands what you typed in your program. However, you might not arrange your command line in a logical manner and hence resulting in this logic error.

3. Semantic error - This error might be not intuitive to most. In this case, it means that your codes are syntactically accurate and logically sound. However, the result you get from your program is not what you intended to get. Thus, resulting in this semantic error.

We will get to learn more as we code and encounter such errors. Also, we will learn how to fix the errors accordingly as our tutorial proceeds.

Next up, we will try some basic mathematical operation in Python. It's pretty similar to Java for those who have read the Java programming tutorial that I have posted last month. However, the structure of the code differs slight, in Python, we do not need the semi-colon at the end of each sentence.


As usual, we can launch our python command line program (you may refer to tutorial one if you forgot how to). Based on the above, we can see the basic operation that we can do in Python. There are six basic mathematical operations that we can do: addition, subtraction, multiplication, division, power and remainder.
There is an important point to note though, all the above numbers are integers. I think that there will not be a need to explain on addition, subtraction, multiplication, division and power. As for the remainder, it means that when 5 is divided by 2, what will be the reminder for this expression. This is denoted in a percent sign (%).


Now, we can actually play around with different numbers using the six basic operations. However, if you try more examples and explore more yourself, you will realize sometimes you get weird results from the program. From the above, we can see that when we take 9 divide by 10, we are expecting the answer to be 0.9, however, from what we see above, the response Python gave us is zero! What happened here?

This is actually due to floor division, when you only key in integers in the mathematical expression, the program will only return an integer result. The answer we were expecting is 0.9 which is not an integer.

From the above example, we can see that if you just type a number in float(with decimals) the program will return the result in float as well. This is true in any of the above three scenarios.

Lastly, there is the operator precedence rules. It is important to know which operators will be processed first in order for you to write a program that yields the result you intend to.
1. Parentheses
2. Power
3. Multiplication and Division
4. Addition and Subtraction
5. Left to Right


The above shows you an example of the order of precedence we have in Python. We always process the expressions with the brackets first, this is also known as the parentheses. After which, as there is a power in this expression, we proceed to solve the power first. (If the expression does not have power, you may just skip right to the next operator which are multiplication and division.) Then, followed by a division which is labelled 3 in the diagram. Since we have two addition in this expression, we will solve it from left to right. Hence, we solve 4 labelled in the diagram first before we move on to 5.

Well, these are most of the basic concept for mathematical operations in Python. We can practice more on our own with different numbers and a mix of different operation to familiarize ourselves with the environment and how the order of precedence works.

This marks the end of tutorial two. In the next tutorial, we will touch on mathematical operations with assignment statements as well as the common types of values we have in Python.

Till then, enjoy yourself, be relax and stay happy~

No comments:

Post a Comment