Today we will discuss the different kinds of number variables in Python. How to interact with them, and what they are used for. There are four kinds of numbers in the python language:
Integers are the basic kinds of numbers in any programming language. Integers are whole numbers, which means they do not have a decimal point. They can be either positive or negative. In Python 2 there is a maximum int size of 9223372036854775807. You can see this by running:
You will notice that this number is 2^63 power. Or in Python notation 2**63. This number comes about since your computer is most likely a 64 bit CPU. 9223372036854775807 is the largest number you can notate using 64 bits. If you have used other programming languages, you may have run into an issue where you mad a max int value of 2147483648. This value equal to 2^31, or the maximum value you can specify in 32 bits.
In Python3, there is no longer a maximum value for int numbers.
If you are working with other data types, such as strings or floats, and you want to convert your number into an int, you can do so using the int function:
#Declare a variable and set the value equal to a floating point number
#Print x’s value
#Convert x into an integer
#Print x’s value again to show the difference
Notice when we print the output of x to the screen after converting to an int, there is no longer any decimal. This is because an int is always a whole number.
In Python2 there were two types of integers. There were regular integers which had a max value of 9223372036854775807. Then there were Long integers which did not have a maximum value.
In Python3, Integers no longer have a maximum value, so the long integer types have been retired.
Floating Point numbers
A float is a number that contains a decimal point. In lower level languages such as C++ you have to declare a number as a float at the start. In Python, you don’t really have to worry about it. You can declare a float simply by adding a decimal point to the number when you declare it. For example:
You can also use the float function to convert an existing integer number to a float. For example:
#show integer on screen
#convert x to a float
#print x to screen to show it is now a float
The output of the above function will first show that x is equal to 10. But then after converting it to a float, x will be equal to 10.0.
Complex numbers are those which include imaginary numbers. Imaginary numbers are used in mathematics, but cannot be shown on a number line. They are typically used in quadratic equations and electrical calculations. An imaginary number is usually notated as the square root of -1. But it really could be the square root of any negative number. For more information on the uses of imaginary/complex numbers, you can look here.
You can convert an existing number to a complex number by using the complex function. There are two ways to call the complex function as it can take either one input or two inputs.
First, you can call:
You will see the notation: 10+0j. You get this output because the default for the imaginary part is 0. You can also specify the imaginary part by passing in two arguments to the complex function. For example:
The output from the above code will show 10+11j
For more information on working with complex numbers, you can check out the following paper from UCSC: http://scipp.ucsc.edu/~johnson/phys160/ComplexNumbers.pdf