Sunday, November 12, 2017

DATA TYPE PART TWO

Numbers
In programming, we often need to use number in various ways like keeping scores of game, recording the number of a visit to a website, recording the number of downloaded application, store information on web applications etc.

Python works on numbers in several ways depending on how the numbers are used. Let take a look at the different ways python treats numbers.


Integer numbers:


Integer is any of the natural numbers (positive or negative) or zero. A whole number is an integer.


We can add (+), subtract (-), multiply (*) and divide integers in python.


Open a python interpreter in your window terminal and try the following:

>>> a = 3

>>>b = 2

>>>a + b

5

>>>a-b

1

>>>a * b

6

>>>a / b

1.5

Python uses two multiplication symbol to represent exponents i.e.

>>>a ** b

9

>>>a ** a

27

Just like every other programming language, python also supports the order of operation and therefore we can use multiple operation in one expression. We can use parenthesis to modify the order of operation so that python knows which operation to perform first.


>>>a + b * a

8

>>>(a + b) * a

10


Floats:

Any number with decimal point is referred to as a float in python. Float is used in most programming language and it refers to the fact that a decimal number point can appear at any position in a number. Every programming language needs to be carefully designed to properly manage floating point number and behave properly no matter where the decimal point may appear.


We can do the following in our python interpreter and if preferably, we may write in our text editor and run it explicitly


>>>0.5 + 0.3

0.8

>>>20 * 0.1

2


Avoiding TypeError with the str() function

Sometimes, we’ll want want to use a variable with an integer or float value within a string of message. e.g. we may want to display the length of a table and also convey a message to the reader to show that it is the length of a table that we are talking about. We may decide to write our code like:


length = 15

message = “The table is ” + length + “cm long”

print(message)


save the program as length.py. Before running the code, our expectation may be that the program would print “The table length is 15cm long”. But if we run the code above, it will generate error as follows


Traceback (most recent call last):


File "length.py", line 2, in <module>


message = "The table is  " + length + "cm long"


TypeError: Can't convert 'int' object to str implicitly


The error indicated above is a TypeError which means python cannot recognize the kind of information we are using. In the example, we are trying to pass a variable with an integer value within a string, the error was raised because python is not sure of how to interpret the value. Python knows that the variable value could either represent the numerical value 15 or the character 1 and 5.


When using integer within a strings like the example above, we need to explicitly specify to python to use the integer variable as string by using the str() function on the variable. We do this by wrapping the variable in the str() function. This tells python to interpret non strings values as strings.


Lets modify our code as follows

length = 15

message = “The table is ” + str(length) + “cm long”

print(message)


with the above code, python now understand that we want to display the variable length as a string. if the code is run, this will be the output:


The table is 15cm long


NOTE: we can print numbers directly, but if we need to print numbers and strings, we will need to explicitly convert the number to string using the str() function
Share:

0 comments:

Post a Comment