Python Numeric Data Type
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Python Numeric Data Type

Kishore V

Numeric Data Types in Python

Python supports three built-in numeric data types that are used to store numbers:

  • int – whole numbers
  • float – decimal numbers
  • complex – numbers with a real and imaginary part

A numeric variable is created automatically when a numeric value is assigned to it.

Example: Creating Numeric Variables

10 4.75 (6+2j)

Checking the Type of a Number

To find out which numeric type a variable belongs to, you can use the type() function.

Example: Verifying Data Types

<class 'int'> <class 'float'> <class 'complex'>

Integer Numbers (int)

An integer is a whole number that can be positive, negative, or zero. Integers in Python can be very large and do not have a fixed size limit.

Example: Integer Variations

<class 'int'> <class 'int'> <class 'int'>

Floating Point Numbers (float)

A float is a number that contains a decimal point. It can also represent numbers written in scientific notation.

Example: Decimal Values

<class 'float'> <class 'float'> <class 'float'>

Scientific Notation

Python allows floats to be written using e or E to represent powers of 10.

<class 'float'> <class 'float'> <class 'float'>

Complex Numbers (complex)

Complex numbers consist of a real part and an imaginary part. The imaginary part is represented using the letter j.

Example: Complex Variables

<class 'complex'> <class 'complex'> <class 'complex'>

Converting Between Numeric Types

Python allows you to convert numbers from one type to another using built-in functions such as int(), float(), and complex().

Example: Type Casting

12.0 8 (12+0j) <class 'float'> <class 'int'> <class 'complex'>

⚠️ Note: Complex numbers cannot be converted into integers or floats.

Generating Random Numbers

Python does not generate random numbers automatically, but it provides a built-in module called random for this purpose.

Example: Generating a Random Integer

14
Our website uses cookies to enhance your experience. Learn More
Accept !