Numeric Data Types
by
Jyostna Devi Bodapati
Python Programming Specialization
Python Programming – Data Structures
Numeric Data Types
▪ Numeric data types represent numeric data such as integers, floating point
or complex numbers
▪ Objects that store numerical type of data come under this category
Numeric
int float complex
Python Programming Specialization
Python Programming – Data Structures
Data Type: int
Python Programming Specialization
Python Programming – Data Structures
Numeric Data Type: int
▪ Integers are represented by int class
▪ Integers are the whole numbers without any fractional part
▪ Can be positive or negative numbers
▪ In Python, Integers can be of any length
▪ It’s length is restricted to the system memory
Python Programming Specialization
Python Programming – Data Structures
Numeric Data Type: int
▪ Examples:
>>> a = 10
>>> b = 100
>>> c = 11111
>>> d = 3333
>>> e = 234257843112578886
Python Programming Specialization
Python Programming – Data Structures
Integers with other bases
Python Programming Specialization
Python Programming – Data Structures
<class int>: Binary data
▪Binary: Integers with base 2
▪Binary digits followed by 0b or 0B are treated as binary in
python
>>> a = 0b101 #binary
>>> b = 0B100 #binary
Note: a is 5 and b is 4
Python Programming Specialization
Python Programming – Data Structures
<class int>: Octal data
▪Octal: Integers with base 8
▪Integers (0-7) followed by 0o or 0O are treated as octal in
python
>>> c = 0o231 #octal
>>> d = 0O347 #octal
Note: C is 153 and d is 231
Python Programming Specialization
Python Programming – Data Structures
<class int>: Hexa-decimal
▪Hexa-decimal: Integers with base 16
▪Integers (0-9, A, B, C, D, E, F) followed by 0x or 0X are treated as
octal in python
>>> e = 0x23 #hexa
>>> f = 0X3A # hexa
Note: e is 35 and f is 58
Python Programming Specialization
Python Programming – Data Structures
Data Type: float
Python Programming Specialization
Python Programming – Data Structures
Numeric Data Type: float
▪ Real numbers are treated as float class
▪ Contains both integer and fractional part
▪ Can be positive or negative numbers
▪ Python floating precision numbers are accurate upto 15 decimal places
▪ Either floating point representation or scientific representation is valid in
python
Python Programming Specialization
Python Programming – Data Structures
Numeric Data Type: float
▪ Examples:
>>> a = 2.3
>>> b = 100.0
>>> c = 0.5e+7
>>> d = 0.00e-4
>>> x = 0.30000000000000004
Python Programming Specialization
Python Programming – Data Structures
Complex Numbers
Python Programming Specialization
Python Programming – Data Structures
Numeric DataType: complex
Numerals with real and imaginary parts are treated as complex class
General Form: (real part) + (imaginary part)j
Ex: -5 + 3j
Numeric Data types in Python

Numeric Data types in Python

  • 1.
  • 2.
    Python Programming Specialization PythonProgramming – Data Structures Numeric Data Types ▪ Numeric data types represent numeric data such as integers, floating point or complex numbers ▪ Objects that store numerical type of data come under this category Numeric int float complex
  • 3.
    Python Programming Specialization PythonProgramming – Data Structures Data Type: int
  • 4.
    Python Programming Specialization PythonProgramming – Data Structures Numeric Data Type: int ▪ Integers are represented by int class ▪ Integers are the whole numbers without any fractional part ▪ Can be positive or negative numbers ▪ In Python, Integers can be of any length ▪ It’s length is restricted to the system memory
  • 5.
    Python Programming Specialization PythonProgramming – Data Structures Numeric Data Type: int ▪ Examples: >>> a = 10 >>> b = 100 >>> c = 11111 >>> d = 3333 >>> e = 234257843112578886
  • 6.
    Python Programming Specialization PythonProgramming – Data Structures Integers with other bases
  • 7.
    Python Programming Specialization PythonProgramming – Data Structures <class int>: Binary data ▪Binary: Integers with base 2 ▪Binary digits followed by 0b or 0B are treated as binary in python >>> a = 0b101 #binary >>> b = 0B100 #binary Note: a is 5 and b is 4
  • 8.
    Python Programming Specialization PythonProgramming – Data Structures <class int>: Octal data ▪Octal: Integers with base 8 ▪Integers (0-7) followed by 0o or 0O are treated as octal in python >>> c = 0o231 #octal >>> d = 0O347 #octal Note: C is 153 and d is 231
  • 9.
    Python Programming Specialization PythonProgramming – Data Structures <class int>: Hexa-decimal ▪Hexa-decimal: Integers with base 16 ▪Integers (0-9, A, B, C, D, E, F) followed by 0x or 0X are treated as octal in python >>> e = 0x23 #hexa >>> f = 0X3A # hexa Note: e is 35 and f is 58
  • 10.
    Python Programming Specialization PythonProgramming – Data Structures Data Type: float
  • 11.
    Python Programming Specialization PythonProgramming – Data Structures Numeric Data Type: float ▪ Real numbers are treated as float class ▪ Contains both integer and fractional part ▪ Can be positive or negative numbers ▪ Python floating precision numbers are accurate upto 15 decimal places ▪ Either floating point representation or scientific representation is valid in python
  • 12.
    Python Programming Specialization PythonProgramming – Data Structures Numeric Data Type: float ▪ Examples: >>> a = 2.3 >>> b = 100.0 >>> c = 0.5e+7 >>> d = 0.00e-4 >>> x = 0.30000000000000004
  • 13.
    Python Programming Specialization PythonProgramming – Data Structures Complex Numbers
  • 14.
    Python Programming Specialization PythonProgramming – Data Structures Numeric DataType: complex Numerals with real and imaginary parts are treated as complex class General Form: (real part) + (imaginary part)j Ex: -5 + 3j