Friday, April 17, 2015

Python Numbers Basics - Python Basics - Python Tutorial

Python NumbersPython Numbers


In this Python tutorial we be covering numbers in the Python Programming Language. If you have done any programming in the past, numbers in Python may look very familiar to you.  Numbers in programming languages really do not change much but they are one of the most important data types in programming, and this holds true in Python as well.

We use numbers in almost every program. We write with Python understanding the difference between integers and floating-point numbers is important. We are required to know how to use mathematical equations with our numbers so in this tutorial on numbers we are going to focus on number types and the math we can apply to each number type.


Integers


An integer is a number that has no decimal point associated with it.  These are integers 34, 5, 89, -45, -23 and so on. A number is considered an integer as long as it does not contain a decimal point.


Booleans


Integers have a subfamily called Booleans as well. Booleans return either True or False which may be confusing because they do not look like an integer. The reason why they are a part of the integer family is because True is actually equal to the integer value 1 and False is equal to the integer value 0.


Floating Point Numbers


Floating point numbers are numbers with a decimal point connected with them. Floating point numbers have more precision than an integer which makes floating point numbers more desirable when you are looking for precision in your programs. These are floating point numbers 5.5, 7.53423, 45323.1, -4523.9, -0.2 and so on.


Complex Numbers


Complex numbers are another number type in the Python Programming Language.  We will not cover complex numbers in this tutorial.  We will save them for a later chapter in our Python tutorial series.


Math With Python


What good are Python numbers without math? In the rest of our tutorial, we will look at how to perform math in Python. You will be required to open your Python interpreter in your terminal or command prompt.To start the interpreter follow the steps for your operating system. Mac users: Once you open the terminal type python3 and press enter. Windows users: Once in your command prompt type python.


Addition


Addition gives the ability to add numbers together. We use the plus(+) sign to indicate we want to add in Python. Let’s take a look at some examples of addition.


#integers
>>> 2 + 2
4
>>> 6 + -4
2
>>> -3 + 69
66
>>> 5673 + 43234
48907

#floats
>>> 5.6 + 3.4
9.0
>>> 67.56423 + 89.34
156.90422999999998
>>> -23.3 + 4.5
-18.8

#floats + integers
>>> 5.6 + 5
10.6
>>> 9 + -4.2
4.8
>>> 7.0 + 5
12.0

Addition in Python is pretty straight forward.  Addition works just like it did in first grade. Note if you add a integer and and a floating point number together then you will get a float.


Subtraction


Subtraction in Python gives us the ability to subtract numbers.  We use the hyphen(-) symbol to indicate subtraction in Python. Lets take a look at some subtraction examples in Python.


#integers
>>> 5 - 7
-2
>>> 6 - 4
2
>>> 6789 - 2345
4444

#floats
>>> 5.7 - 8.9
-3.2
>>> 4.5 - 2.0
2.5
>>> 65739 - 9834
55905

#floats - integer
>>> 4 - 4.5
-0.5
>>> 56 - 33.33
22.67
>>> 4.0 - 5
-1.0

Multiplication


Multiplication gives us the ability to multiply numbers in Python.  We use the star(*) symbol to indicate we want to multiply numbers together.  Let’s take a look at some examples of multiplication in Python.


#integers
>>> 5 * 6
30
>>> 76 * 4353
330828
>>> 235 * -9878
-2321330

#floats
>>> 56.43 * 989.02
55810.3986
>>> 45.1 * -43.1
-1943.8100000000002

#floats * integers
>>> 56 * 43.6
2441.6
>>> 434 * 9834.32
4268094.88

Division


Division in Python gives us the ability to divide numbers in Python.  We use the backslash(/) symbol to indicate that we want to divide numbers. Let’s take a look a few examples of division in Python.


#integers
>>> 56 / 45
1.2444444444444445
>>> 89 / 43
2.0697674418604652
>>> 9 / -3
-3.0
>>> 9 / 3
3.0

#floats
>>> 76.4 / 9.0
8.488888888888889
>>> 653.55 / 54
12.102777777777776
>>> 5.6 / 2.3
2.4347826086956523

When we divide integers Python will return a float. We will look at how to change a float to integer in our numbers chapter.

Now that we covered the four basic mathematical operators in Python we can safely move on for now.  We will have a chapter that will dive very deep into the numbers and the other mathematical operators in Python but for now we have covered the important ones.

This tutorial has covered the very basic Python numbers and Python mathematical operators. Do not forget to check out our chapter on numbers for more in depth information on Python numbers. If you have any questions please leave a comment below.



No comments:

Post a Comment