Python Numbers with Examples

Python Built-in Datatypes include Numeric types, Boolean Type, Sequence Type, etc. Python Numbers are the ones with Numeric Types. Variables stores data of different types, and what operations can be performed on that data is represented by the datatype.

To store Number types, such as integer, floats, etc., Python Numbers includes datatypes, like the int, float, long and complex data types. Let us see some examples before going through each datatype:

The output is as follows:

Following are the types:

  • Int
  • Float
  • Long
  • Complex

Let us begin with int datatype:

Int Datatype

Int (Integer) is a positive or negative whole number, without a fractional part and of unlimited length. For example, 1, 5, 200, -10, 25, -550, etc. Also includes the octal and hexadecimal numbers. Let us now see some examples of the Int datatype in Python:

The output as follows:

Let us see another example:

The output as follows:

Let us see another example to know the int type:

The output is as follows:

Long Datatype

The Long datatype are int types with unlimited size. Always remember to suffix the number with L (lowercase or uppercase) for a long type. For example, 1234L, 4657688L, -5456859L, etc.

Note: Use Uppercase L, since lowercase l will lead to confusion in number, for example, 56586881l, difficulty in differentiation between 1 and lowercase l. Better approach to use uppercase L.

Note: In Python 2.7, int and long int are available, but in Python 3, only one type int works.

Let us see an example of long datatype in Python (Run in Python 2):

The output is as follows (Run in Python 2):

Float datatype

Float is floating point positive or negative number, with decimal(s). The fractional part is also denoted as a notation i.e. E or e. This represents a shot form for the big float numbers.

For example, 325.6 is represented as:

For example, 2.615E8 is represented as:

Let us see an example for float datatype in Python:

The output is as follows:

Let us see an example to know the float type:

The output is as follows:

Complex Datatype in Python Numbers

Under complex datatype, numbers are a combination of real and imaginary parts. The imaginary component has a “j” as imaginary part. For example, 2 + 7j, 20j, 8-2j, -30j, etc.

Above, for 2 + 7j:

Let us see an example for complex datatype in Python:

The output is as follows:

Let us see another example to know the type:

The output is as follows:

Python Numbers include datatypes, such as int, float, long and complex.

Python Tutorial (English)

Python Tutorial (Hindi)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Lists with Examples
Type Conversion in Python
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading