Numeric Data Types in Python
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

Numeric Data Types in Python

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:

# Variables assigned successfully (No output printed)

Checking the Type of a Number

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

Example:

<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:

<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.

Example:

<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:

<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:

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:

14 # (Note: Output will be a random number between 1 and 20)

This program generates a random integer between 1 and 20 (both inclusive).

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