Data types In Python
Clear concept of data types in python
Python Numbers
Python provides three primary types for numeric data:
- int: whole numbers (positive or negative, without decimals)
- float: decimal numbers
- complex: numbers with a real and imaginary part
Python determines a variable's type based on the assigned value, and you can check this type using type()
.
Numeric Types Example:
x = 1 # int
y = 2.8 # float
z = 1j # complex
To verify the type of any number, you can use the type() function:
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'float'>
print(type(z)) # Output: <class 'complex'>
1. Integers (int)
Integers are whole numbers, and Python integers can be of unlimited length, meaning they can be very large or very small, without decimals.
Example of Integers:
x = 1
y = 35656222554887711
z = -3255522
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'int'>
print(type(z)) # Output: <class 'int'>
2. Floating Point Numbers (float)
Floats represent numbers with a decimal point. They can be positive or negative.
Example of Floats:
x = 1.10
y = 1.0
z = -35.59
print(type(x)) # Output: <class 'float'>
print(type(y)) # Output: <class 'float'>
print(type(z)) # Output: <class 'float'>
Floats can also be written in scientific notation using "e" to denote powers of 10. Example of Scientific Notation Floats:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x)) # Output: <class 'float'>
print(type(y)) # Output: <class 'float'>
print(type(z)) # Output: <class 'float'>
3. Complex Numbers (complex)
Complex numbers consist of a real and an imaginary part, written with a "j" to denote the imaginary part. Example of Complex Numbers:
x = 3 + 5j
y = 5j
z = -5j
print(type(x)) # Output: <class 'complex'>
print(type(y)) # Output: <class 'complex'>
print(type(z)) # Output: <class 'complex'>
Summary
Python makes it easy to work with numbers by automatically interpreting int, float, and complex types based on the value you assign. Using type() helps to verify each type, ensuring accuracy in operations and code functionality.