Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Numeric data types
Sundeep
Sundeep

Posted on • Originally published atlearnbyexample.github.io

     

Numeric data types

Python is a dynamically typed language. The interpreter infers the data type of a value based on pre-determined rules. In the previous chapter,string values were coded using single quotes around a sequence of characters. Similarly, there are rules by which you can declare different numeric data types.

int

Integer numbers are made up of digits0 to9 and can be prefixed withunary operators like+ or-. There is no restriction to the size of numbers that can be used, only limited by the memory available on your system. Here's some examples:

>>>4242>>>00>>>+100100>>>-5-5
Enter fullscreen modeExit fullscreen mode

For readability purposes, you can use underscores in between the digits.

>>>1_000_000_0001000000000
Enter fullscreen modeExit fullscreen mode

Underscore cannot be used as the first or last character, and cannot be used consecutively.

float

Here's some examples for floating-point numbers.

>>>3.143.14>>>-1.12-1.12
Enter fullscreen modeExit fullscreen mode

Python also supports the exponential notation. Seewikipedia: E scientific notation for details about this form of expressing numbers.

>>>543.15e205.4315e+22>>>1.5e-51.5e-05
Enter fullscreen modeExit fullscreen mode

Unlike integers, floating-point numbers have a limited precision. Python will automatically convert very small or very large floating-point numbers to the exponential notation.

>>>0.00000000012345678901234567891.2345678901234568e-10>>>31415926535897935809629384623048923.6492343242343.1415926535897936e+34
Enter fullscreen modeExit fullscreen mode

You might also get seemingly strange results as shown below. Seedocs.python: Floating Point Arithmetic Issues and Limitations andstackoverflow: Is floating point math broken? for details and workarounds.

>>>3.14+25.140000000000001
Enter fullscreen modeExit fullscreen mode

Arithmetic operators

All arithmetic operators you'd typically expect are available. If any operand is a floating-point number, result will be offloat data type. Use+ for addition,- for subtraction,* for multiplication and** for exponentiation. As mentioned before, REPL is quite useful for learning purposes. It makes for a good calculator for number crunching as well. You can also use_ to refer to the result of the previous expression (this is applicable only in the REPL, not in Python scripts).

>>>25+1742>>>10-82>>>25*3.382.5>>>32**421645504557321206042154969182557350504982735865633579863348609024>>>5+27>>>_*321
Enter fullscreen modeExit fullscreen mode

There are two operators for division. Use/ if you want a floating-point result. Using// between two integers will give only the integer portion of the result (no rounding).

>>>4.5/1.53.0>>>5/31.6666666666666667>>>5//31
Enter fullscreen modeExit fullscreen mode

Use modulo operator% to get the remainder. Sign of the result is same as the sign of the second operand.

>>>5%32>>>-5%31>>>5%-3-1>>>6.5%-3-2.5
Enter fullscreen modeExit fullscreen mode

Seedocs.python: Binary arithmetic operations andstackoverflow: modulo operation on negative numbers for more details.

Arithmetic operator precedence follows the familiarPEMDAS orBODMAS abbreviations. Precedence, higher to lower is listed below:

  • Expression inside parentheses
  • exponentiation
  • multiplication, division, modulo
  • addition, subtraction

Expression is evaluated left-to-right when operators have the same precedence. Unary operator precedence is between exponentiation and multiplication/division operators. Seedocs.python: Operator precedence for complete details.

Integer formats

The integer examples so far have been coded using base 10, i.e.decimal format. Python has provision for representingbinary,octal andhexadecimal formats as well. To distinguish between these different formats, a prefix is used.

  • 0b or0B for binary
  • 0o or0O for octal
  • 0x or0X for hexadecimal

All four formats fall under theint data type. Underscores can be used for readability for any of these formats.

>>>0b1000_1111143>>>0o108>>>0x1016>>>5+0xa15
Enter fullscreen modeExit fullscreen mode

As a consequence, decimal format numbers cannot be prefixed by0, other than0 itself.

>>>000000>>>09File"<stdin>",line109^SyntaxError:leadingzerosindecimalintegerliteralsarenotpermitted;usean0oprefixforoctalintegers
Enter fullscreen modeExit fullscreen mode

If code execution hits a snag, you'll get an error message along with the code snippet that the interpreter thinks caused the issue. In Python parlance, anexception has occurred. The exception has a name (SyntaxError in the above example) followed by the error message.

Other numeric types

Python's standard data type also includes complex type (imaginary part is suffixed with the characterj). Others likedecimal andfractions are provided as modules.

Some of the numeric types can have alphabets likee,b,j, etc in their values. As Python is a dynamically typed language, you cannot use variable names beginning with a number. Otherwise, it would be impossible to evaluate an expression likeresult = initial_value + 0x12 - 2j.

There are many third-party libraries that are useful for number crunching in mathematical context, engineering applications, etc. See my listpy_resources: Scientific computing for curated resources.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Addicted to writing books and teaching. Likes fantasy books, comics and anime
  • Joined

More fromSundeep

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp