Numbers
What Are Python Numbers?
- In Python, there are three numeric types:
- int: Integer, a whole number without decimals.
- float: Floating point number, a number with decimals.
Creating Numeric Variables:
Variables of numeric types are created when you assign a value to them.
pythonx = 1 # int y = 2.8 # float
Checking the Type:
Use the
type()function to verify the type of any object.pythonprint(type(x)) print(type(y))- Output:
<class 'int'> - Output:
<class 'float'>
- Output:
Integer:
Integers are whole numbers without decimals, of unlimited length.
pythonx = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z))- Output:
<class 'int'> - Output:
<class 'int'> - Output:
<class 'int'>
- Output:
Float:
Floats are numbers with one or more decimals and can also be scientific numbers with an "e" to indicate the power of 10.
pythonx = 1.10 y = 1.0 z = -35.59 print(type(x)) print(type(y)) print(type(z)) x = 35e3 y = 12E4 z = -87.7e100 print(type(x)) print(type(y)) print(type(z))- Output:
<class 'float'> - Output:
<class 'float'> - Output:
<class 'float'> - Output:
<class 'float'> - Output:
<class 'float'> - Output:
<class 'float'>
- Output:
Summary:
- Python supports three numeric types: integers, floats, and complex numbers. You can convert between these types and generate random numbers using the
randommodule. Practice creating and working with numeric variables to understand their properties and uses!