- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python Numbers (int, float, and complex type)
Numeric types in Python are used to store numeric values. The three numeric types in Python are:
Note: The type() function is used to find the type of a value or variable. For example:
x = 45 y = 34.54 z = 12j print(type(x)) print(type(y)) print(type(z))
The output is:
<class 'int'> <class 'float'> <class 'complex'>
Python int type
Any whole number is considered of the "int" type. For example, 12, 1, 0, -1345431234, 23456765432134565432, etc.
Advantages of the "int" type in Python
- Python arithmetic operations performed on "int" values are faster than those performed on other numeric data types.
- Memory efficiency: "int" values consume less memory than other numeric data types such as "float."
- Integral division: When performing division with the "int" type, the resulting value is an integer, which can be advantageous in certain situations.
- Supports bitwise operations
Disadvantages of the "int" type in Python
- The range of values that the "int" type can represent is limited, so large numbers cannot be represented by this data type.
- Not suitable for non-integer values: The "int" data type can only represent whole numbers, so it cannot be used to store values that are not integers.
- No decimal support: "int" values cannot store decimal values; other data types, such as "float," must be used for this purpose.
- Memory consumption can be high: Memory usage can be high for very large "int" values, which can be detrimental in memory-constrained environments.
Python float type
Any number with a decimal (except the complex number) is classified as float.For example, 1.9, 2.0, -4253464.2433, etc.
Note: Numbers like 781E4, 23E3, -3542.6E10, etc. are also considered floating-point numbers, where e or E indicates the power of 10. Therefore, these numbers are of the "float" type.
The number 781E4 is equal to 7810000.0. Similarly, the number -3542.6e5 is equal to -354260000.0.
Note: A number in the form of 123E32 can also be called a scientific number.
Advantages of the "float" type in Python
- Supports decimal numbers: The "float" type can be used to store decimal numbers, which the "int" type cannot.
- Can show a wide range of values: The "float" type can show a wide range of values, from very small to very large.
- Arithmetic operations: "Float" values can be used to do arithmetic operations quickly.
- Widely used: The "float" type is used a lot in science and engineering when decimal accuracy is important.
Disadvantages of the "float" type in Python
- Precision problems: The way decimal numbers are stored in binary format can cause precision problems with the "float" type. This can lead to rounding errors and inaccurate calculations.
- Not suitable for exact decimal values: Some decimal values, like 0.1, can't be represented exactly with the "float" type, which can cause calculations to come up with unexpected results.
- More use of memory: The "float" type takes up more memory than the "int" type, which can be bad in situations where memory is limited.
- Not as fast as "int" type: Adding and subtracting with "float" values is slower than with "int" values.
Python complex type
Any number with an imaginary part is considered complex. For example, 23+6J, 10J, -32J, etc.
Advantages of the "complex" type in Python
- The "complex" type is appropriate for representing complex numbers and performing arithmetic operations on them.
- Using the "complex" type, complex numbers can be created and manipulated easily in Python.
- Supports complex arithmetic operations The "complex" type supports arithmetic operations on complex numbers, including addition, subtraction, multiplication, and division.
- Complex numbers are utilized in numerous disciplines, including mathematics, engineering, and physics, and the "complex" type in Python is a useful tool for representing and manipulating them.
Disadvantages of the "complex" type in Python
- Complex numbers are rarely used in everyday programming tasks, and their application is limited to specific fields.
- Certain mathematical functions do not support complex numbers, limiting their applicability in certain circumstances.
- Performing arithmetic operations on complex numbers may be slower than with other data types, such as "int" and "float."
- Complex numbers require more memory than other data types, which can be disadvantageous in environments with limited memory.
« Previous Tutorial Next Tutorial »