Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Number Data Type in Python
Number data types store numeric values. Number objects are created when you assign a value to them. For example −
var1 = 1 var2 = 10 print(var1, var2)
1 10
You can also delete the reference to a number object by using the del statement. The syntax of the del statement is −
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement. For example −
number1 = 42
number2 = 3.14
# Delete single object
del number1
# Delete multiple objects
del number2
print("Objects deleted successfully")
Objects deleted successfully
Python Number Types
Python supports three different numerical types (Note: long type was merged with int in Python 3) −
- int (signed integers)
- float (floating point real values)
- complex (complex numbers)
Examples of Number Types
| int | float | complex |
|---|---|---|
| 10 | 0.0 | 3.14j |
| 100 | 15.20 | 15.20+0j |
| -786 | -21.9 | 9.322e-36j |
| 0o80 | 32.3e18 | 876j |
| 0x260 | -90.0 | -0.6545+0j |
| 0x69 | -32.54e100 | 3e+26j |
Working with Different Number Types
# Integer examples
integer_num = 42
hex_num = 0x2A # Hexadecimal (42 in decimal)
octal_num = 0o52 # Octal (42 in decimal)
print(f"Integer: {integer_num}")
print(f"Hexadecimal 0x2A: {hex_num}")
print(f"Octal 0o52: {octal_num}")
# Float examples
float_num = 3.14
scientific_notation = 1.5e-3
print(f"Float: {float_num}")
print(f"Scientific notation: {scientific_notation}")
# Complex number examples
complex_num1 = 3 + 4j
complex_num2 = 2.5j
print(f"Complex number 1: {complex_num1}")
print(f"Complex number 2: {complex_num2}")
Integer: 42 Hexadecimal 0x2A: 42 Octal 0o52: 42 Float: 3.14 Scientific notation: 0.0015 Complex number 1: (3+4j) Complex number 2: 2.5j
Key Points
- In Python 3, there's no separate long type − integers can be of arbitrary size
- Use
0xprefix for hexadecimal numbers and0oprefix for octal numbers - A complex number consists of an ordered pair of real floating-point numbers denoted by
x + yj, where x and y are real numbers and j is the imaginary unit - Python automatically handles type conversion between different number types when needed
Conclusion
Python supports three main number types: int, float, and complex. Understanding these types is fundamental for numeric operations and calculations in Python programs.
Advertisements
