Numeric Data Types¶
Every variable in Python has a “type”. The type of a variable is based on the contents of that variable, and determines the ways in which that variable can be used. For example, we cannot perform certain operations on variables of certain types.
We will begin our discussion of data types with the data types intended to hold numbers. Python has three numerical variable types: int
, float
, and complex
. In this class, we will work only with the int
and float
variables.
An
int
variable is used to represent an integer.A
float
variable is used to represent a decimal number.
We will discuss the importance of variable types in more depth later. For now, we will show how to determine a variable’s type using the type()
function.
x = 7
type(x)
int
y = 3.7
type(y)
float
Python infers whether a numeric variable should be stored as an int
or a float
based on the value of that variable. Occasionally we might wish to assign a whole number value to a variable, but have it actually stored as a float. We can accomplish this by adding “.0” to the end of the number.
z = 14.0
type(z)
float