Thursday, March 26, 2015

float() - Python Tutorial

float() built-in function in python



 


Float() Built-in Function In Python


The float() built-in function converts integers and strings to floating point numbers.  This basic function simply just changes the type of object to a floating point number type in Python.


Float() Syntax


float(argument)


The argument can be a integer or string.  You can also include a float but it will do nothing to the floating point number. If you call the float() function on a integer the when be convert to float which it will contain a decimal and a zero.


Float() Examples


#float examples

>>> float(1)
1.0
>>> float("6")
6.0
>>> float("5.6")
5.6

If you have any questions about the float() built-in function leave a comment below and we will assist you.


 



Wednesday, March 25, 2015

Addition in Python - Python Numbers - Python Tutorial

Addition in PythonAddition in Python


We are getting back into the basics in this Python tutorial. In the first three tutorials in this chapter, on Python numbers we focussed on floating point numbers and decimal numbers. Our plan was to start out very basic with addition but we had some questions about the accuracy of floating point numbers which made us push back this tutorial on addition in Python.


Addition in Python is very simple and you should not  have any issues with addition in Python. To add numbers we use the plus(+) symbol to add the numbers together. The plus symbol is regarded as an arithmetic operator in the Python programming language.


Example of Addition in Python


In these examples, we will be using our Python interpreter. The interpreter is a very useful tool when it comes to working with math and numbers.


Adding Integers 


#Adding Integers in Python

>>> 2 + 2
4
>>> 33 + 22
55
>>> 67 + 32
99
>>> 55 + 22
77
>>> 19 + 8978
8997

#adding positive integer to negative integer
>>> 340000 + -40000
300000

Adding Floats


We will run into some issues adding floats, but we have learned in our previous tutorials how to fix these issues. When going through this these examples use the skills that you have learned in the previous Python tutorials and fix these numbers.


#Adding Floats

#Fix this one
>>> 5.6 + 87.3
92.89999999999999

#If you add an integer and a float together we will get a float back
>>> 76.5 + 65
141.5

#fix this one as well
>>> 678.34 + 34.21
712.5500000000001

#adding a positive float to a negative float

>>> 75.3232 + -2.3
73.0232

############################
Answers to above problems below
############################
>>> round(5.6 + 87.3, 1)
92.9
>>> round(678.34 + 34.21, 2)
712.55

This is a very short tutorial and the reason is that addition in Python is so simple that we really did not have much more to show you. Just remember with addition and floats you can run into some weird returns so either use the built-in function round() or the decimal module.


Build An Addition Program

 


If you have any questions please leave a comment below.



int() - Python Tutorial

int() in pythonint() Python Built-in Function


int() is a built-in function that will convert a floating point number or string to an integer type. If a user or our program returns a string or a float we can use the int() function to convert the number to integer. The int() function rounds the float down to the floor check the examples below.


int() Built-in Function Example


#int() Examples

#int() example
>>> int(8.7)
8

#int() example
>>> int(9.876)
9

#int() example
>>> int(5.3)
5

#int() example
>>> int(6)
6

#int() example
>>> int("5677")
5677

#This does not work we can not convert a float string but check next example
>>> int("5.98")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.98'

#The fix to above example
>>> int(float("5.98"))
5

If you have any questions about int() built-in function leave a comment below.


 



Python Tutorial: How To Convert Number Types In Python - Python Numbers #35

How to Convert Number Types in Python - Python Numbers - Python Tutorial

How to Convert Number Types in PythonHow To Convert Number Types in Python


In this Python tutorial, we are going to explore how to convert number types in Python. When coding sometimes we need in order to convert our current integer to a float or our current float to integer. We can also convert strings to either integers or floating point numbers.  Python has a built-in function that will give us the ability to do this.


Int() Built-in Function


The int() built-in function gives the ability to convert either a floating point number to an integer or convert a string to an integer. The int() function will always round down when converting floating point numbers. We also cannot convert floating point number strings to integers but we can do it we will look at that later in this tutorial. Let’s take a closer look at some int() examples.


#int() built-in function

#convert floats to integers
>>> int(4.5)
4
>>> int(1.45643)
1

#convert string integers to integers
>>> int("5")
5
>>> int("9")
9


Float() Built-in Function


The float() built-in function gives us the ability to convert integers to floating point numbers and convert strings to floating point numbers let’s take a closer look at this example.


#float() built-in function

#Convert integer to float
>>> float(6)
6.0

#convert integer to float
>>> float(-78)
-78.0

#convert integer in a string to a floating point number
>>> float("5")
5.0

#convert a floating point number string to a actual floating point number
>>> float("6.7")
6.7

Converting a String Floating Point Number To A Real Integer


#just using int will not work
>>> int("6.7")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '6.7'

#first convert to float then to integer
>>> int(float("6.7"))
6

Checking The Type Of An Object


We can also use the type() built-in function to check the type of an object.


#type()

>>> type(6)
<class 'int'>
>>> type(())
<class 'tuple'>
>>> type(7.6)
<class 'float'>
>>> type("")
<class 'str'>

Conclusion


 


In this Python tutorial, we took a look at How to Convert Number Types in Python. These built-in functions help us with our code so we do not have to build our own function to convert the number types. If you have any questions about this tutorial please leave a comment below.



Python Decimal Module - Python Numbers - Python Tutorial

Python Decimal ModulePython Decimal Module


In the previous two Python tutorials, we looked at issues that arise when working collaboratively with floating point numbers in the Python programming language. When working with floating point numbers we have seen that Python sometimes returns a value that we are not suspecting. This is doesn’t happen often but when we get a number that is a few ten thousandths off this can be concerning especially if you are working with numbers that need to have precise returns like a accounting program or a program that controls trajectory of a spacecraft. It would be concerning if we could not get the correct amount of money or if our spacecraft was off course by a tad bit we would probably miss the earth on the return and fly right into the sun.

In this tutorial, we are going to look at the Python decimal module which needs to import into your program for the functions to work. Decimals are basically floating point numbers but, they have fixed number of decimal points.  We can think of them as fixed-precision floating point numbers. We can also control how Python rounds the numbers unlike with the round() function where we saw if can round in the wrong direction.  With the decimal module, we will be capable of more precise numbers with a little bit more work.


Importing The Decimal Module


We have seen how to import a few modules in previous tutorials, in this tutorial we are going to follow the same steps. To make the decimal module to work we first need to import the module and we do this by entering the following “import decimal” with no quotes.  Once we have imported the Python decimal module we will be able to access the functions included in the module.


Importing The Decimal Module Example


#import the whole module. We will use this method in this tutorial

>>> import decimal

#import a specific function in the module
>>> from decimal import Decimal

In this Python decimal module, tutorial we will just use import decimal. This will make it easier to focus on the decimal module over importing specific functions.


Working With Python Decimal Module


First we need to look at an example where we would need to use the decimal module. Let’s look at the example we have been using over and over in our last two tutorials.


Why We Need Decimal Module Example


#below you will find two examples why we would need to use the decimal module

#answer should be 16.3
>>> 7.6 + 8.7
16.299999999999997

Ways To Fix This Using the Decimal Module


Method 1


In this example, we set a global precision on our decimal module and we may be limited in using this method. Take a look at how it works.


#first example

#issue
>>> 7.6 + 8.7
16.299999999999997

#import Decimal Module
>>> import decimal

#set the precision globally this tells decimal module we want 3 numbers returned.
>>> decimal.getcontext().prec = 3

#Now add the two numbers together using the decimal module
>>> decimal.Decimal(7.6) + decimal.Decimal(8.7)
Decimal('16.3')

In the above example, we take our problem floating point number and we use the decimal module to return the correct number. Let’s take a closer look at the process we use above.


Import Decimal Module


First we import the Python decimal module.  This needs to be done so we can use the functions the module contains.


Code: import decimal


Set Global Precision


When we set global precision the program to only show three numbers with our code.  Global means that our program will only show three numbers throughout our program or until we change it. This method may not be the best if you are working with numerous different numbers.


Code: decimal.getcontext().prec = 3


The Equation


Here we perform a simple addition equation.  We do have some words in front of our floats.  The first word decimal says go to the module decimal and then the second word Decimal says get the Decimal function which is inside the decimal module.


Code: decimal.Decimal(7.6) + decimal.Decimal(8.7)


Method 2


In this example, we will set the precision for our numbers temporarily so we will look at global with temporary precision adjustment.


#Temporary Precision

#import decimal module
>>> import decimal

#set global
>>> decimal.getcontext().prec = 3

#our equation for this example but we want the dollar and cents in this equation. We only get 3 numbers which are the dollars
>>> decimal.Decimal(78.96) + decimal.Decimal(90.99)
Decimal('170')

#temporary fix setting our precision for one equation
>>> with decimal.localcontext() as ctx:
... ctx.prec = 5
... decimal.Decimal(78.96) + decimal.Decimal(90.99)
...
Decimal('169.95')

#see only temporary
>>> decimal.Decimal(78.96) + decimal.Decimal(90.99)
Decimal('170')

Import Decimal Module


Is the same as above example


code: import decimal


Set Global Precision


Is the same as above example


Code: decimal.getcontext().prec = 3


The Equation


Here we have an equation and in this equation we want to find out how much a client owes you. This equation does not show cents and rounds up so your client is paying you more than is actually owed to you. This may not be the best choice when running a business


codedecimal.Decimal(78.96) + decimal.Decimal(90.99)


The Temporary Precision Solution


What in the world is that funky looking code. We are nowhere near learning about exceptions in Python but I thought I would share this one to show you the possibilities in Python and the decimal module. Let’s take a closer look “With” is an exception statement in Python. So, what I did here was say “with” the “decimal module” look inside the decimal module and get the “localcontext()” function and set this function “as” a variable called “cxt” so we can call it in the statement. The next line you must indent in your Python interpreter(I use two spaces) we set the precision using “cxt” variable like this cxt.precision = 5. After, we define the precision we can run our equation like before. Then in Python interpreter press return twice.


This also may not be the best solution for your program seems like a lot of code to do something simple.


Method 3


This is our final example in this tutorial. If you are looking for more precise decimal numbers then quantize may be our best option. We use quantize to return the exact amount of decimal numbers.


#using quantize in decimal module

#import decimal
>>> import decimal

#assign a variable an equation
>>> a = decimal.Decimal(78.96 * 89.65)

#Then use quantize to set exact decimal position
>>> a.quantize(decimal.Decimal('0.00'))
Decimal('7078.76')

Import Decimal Module


Same as before


code: import decimal


A Variable assigned To An Equation


Here we allocate a variable to an equation. We use a variable to cut down on the amount of code we need to type, but we could have done it all on one line. This is just simpler.  So with equation, we want to know the monetized amount so we want two numbers after the decimal point.


Quantize The Decimal


Here we use a method to round the number to a certain set of numbers after the decimal. We use a method called quantize to achieve this. We set how we want the method to return the value with ‘0.00’ it must be in a string format. If we want more numbers after the decimal for example we want four we could do ‘0.0000’ to make this happen.


Last Example


We also looked at another problem using the built-in function round().  round(2.675, 2) this function does not round as we expect so is there a way to fix this?  Of course there is. 


#Last Example Fixing Rounding Issues

#This should round up to 2.68
>>> round(2.675, 2)
2.67

#Lets take a look at what this number actually represents
>>> b = decimal.Decimal(2.675)
>>> b
Decimal('2.67499999999999982236431605997495353221893310546875')

#as you can see it rounds down because of the 4 instead of the five

#the fix
b.quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_UP)
Decimal('2.68')

In this example we once again use the quantize module but this time we use rounding to tell Python to round the number up.  We can use the following rounding commands ROUND_CEILING, ROUND_UP, ROUND_HALF_UP, ROUND_FLOOR, ROUND_DOWN, ROUND_05UP, ROUND_HALF_EVEN.


In Conclusion


We have taken a brief look at the Python decimal module. This is not a full tutorial on this module and we do have one planned for the future.  That tutorial will need to be broken down into several mini tutorials do the shear size of the Python decimal module. If you want to read more about the Python decimal module visit https://docs.python.org/3.4/library/decimal.html#module-decimal If you have any questions about today’s tutorial, please leave a comment below.



Python Tutorial: Manually Adjust Operator Precedence - Python Numbers #34