Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of numbers in Python

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of a sample analysis of numbers in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Figures

In Python, a number is not a true object type, but a set of classifications of similar types. Python supports not only the usual data types (integers and floating point numbers). And you can use constants to directly create numbers and manipulate numeric expressions

 integers and floating point numbers

 complex number

Fixed precision decimal number of 

 rational fraction

 collection

 Boolean type

The infinite integer precision of 

 various digital built-in functions and modules.

Numeric constant:

Digital constant

123maxim, 23pr, 0je 99999. Integer (infinite size)

1.23pr 1.pr 3.14e Rue 10pr 4E210 floating point number

Octal, hexadecimal, and binary constants in 0o177 Python3.0 0x9ffrecover0b11000

Plural output of 3mu 4jpr 3j

Python expression operator:

Numeric variable expression:

The  variable is created the first time it is assigned

The use of  variables in expressions will be replaced with their values

The  variable must be assigned before it can be used in an expression

 variables, like objects, do not need to be declared at the beginning

>

Conversion of numbers:

> int ('100') # convert a number from text to the number 100 > str (100) # convert a number from a number to the text' 100'

Digital display format:

> num=1/3.0 > num0.3333333333333333 > print (num) 0.3333333333333333 >'% ewoods% numbness 3.333333eWhile 01' >'% 4.2f% numbing 0.3333'>'{0num 4.2f} '.format (num)' 0.33'>

Digital comparison: general and continuous

The general comparison Operand is relatively large and returns a Boolean result.

> 1 > 2.0 > = 1True > 2.0==2.0True > 2.0 false

Continuous comparison:

> Xerox 10 > Yidong 20 > zodiac 30 > x > xzFalse

A function that gets the maximum and minimum values of a number:

> max (x _ rect _ y _ z) 30 > min (x _ ther _ y _ Z) 10 >

Division:

Traditional division, Floor division and true division:

Traditional division:

X / Y

Traditional division and true division, in Python2.6 and previous versions, this operation omitted the decimal part for integers and kept the decimal part for floating-point numbers. True division in the Python3.0 version (decimal parts are maintained regardless of type).

Floor division:

X / / Y

Added operations in Python2.2, which can be used in both Python2.6 and Python3.0. Regardless of the Operand type, the operation always omits the fractional part of the result, leaving the smallest integer part that can be divisible.

Traditional division is eliminated in Python3.0, and / and / / real division and floor division are implemented respectively.

> > 10 / 42.5 > 10 / 42 > 10 / 4.02.5 > 10 / 4.02.0 > 5 / 2.02.5 > 5 / 2.02.0 > 5 /-2.0-2.5 > 5 /-2.0-3.0 > >

Integer precision:

Python3.0 integers support infinite sizes, and long Python2.6 integers display an "L" at the end.

Plural:

The complex number is divided into two floating point numbers (real part and imaginary part) and the suffix of j or J is added to the imaginary part. The plural of non-zero real parts can be written as two parts connected by +.

> 1j*1j (- 1x 0j) > 2+3j*3 (2x 9j) > (5x 6j) * * 2 (- 11x 60J) >

Hexadecimal, octal, and binary constants

> 0o1recollection 0o20recover0o377 (1,16,255) > 0x01recorder 0x10pender 0xFF (1,16,255) > 0b1hero0b10000.0b11111111 (1,16,255) > oct (64), hex (64), bin (64) ('0o100cycles,' 0x40cycles, '0b1000000') >

Int converts numbers to binary:

> int ('64') 64 > int (' 100 million page8) 64 > int ('40 million pommel 16) 64 > > int ('1000000 Liuzhuo 2) 64 > int (' 0o100 Fengzheng8), int ('0x40 Fenglue 16), int (' 0b1000000FL2) (64, 64, 64)

Eval function:

> eval ('64'), eval (' 0o100'), eval ('0x40'), eval (' 0b1000000') (64, 64, 64) > >

The conversion base is called by character:

> >'{0rangeO}, {1lazx}, {2lazb} '.format (64meme 64jue 64)' 100meme 40pint 1000000' >'% o% xmeme% X'% (64255255) '100fuffre FF' >

Bit operation

Use integers as binary bit strings for displacement and Boolean operations.

> x > y > 14 > > y > 22 > > y > 31 > > y > 40 > y > 50 > x=0b0001 > x1 > bin (x > bin (x | 0b0010) '0b11' > bin (x ^ 0b0010) '0b11' > bin (x & 0b0010)' 0b0' >

Displays the number of digits in the binary number:

> Xero100 > bin (X), X.bit_length () ('0b1100100mm, 7) > Yellow101 > bin (Y), Y.bit_length () (' 0b1100101)

Decimal number

Decimals are floating-point values with fixed precision.

Decimal object: the decimal is created after the function is called by importing the decimal module. Have a fixed number of digits and a decimal point.

Floating-point numbers lack accuracy because there is limited space to store values.

> 0.1-0.1-0.35.551115123125783e-17 > from decimal import Decimal > Decimal ('0.1') + Decimal (' 0.1') + Decimal ('0.1')-Decimal (' 0.3') Decimal ('0.0') >

Automatically adjusts when a string has a large number of decimal places in a decimal object.

> Decimal ('0.1') + Decimal (' 0.1') + Decimal ('0.100')-Decimal (' 0.3') Decimal ('0.000')

Global precision settings:

> import decimal > decimal.Decimal (1) / decimal.Decimal (7) Decimal ('0.142857142857145714571429') > decimal.getcontext (). Prec = 4 > decimal.Decimal (1) / decimal.Decimal (7) Decimal (' 0.1429') >

Score

Fractions are used in a decimal-like manner in Python and also exist in modules, and a fraction can be generated by importing its constructor and passing a numerator and denominator.

> from fractions import Fraction > x=Fraction (1pm 3) > y=Fraction (4pm 6) > xFraction (1pm 3) > yFraction (2,3) > print (x minute y) 1max 3 2pm 3 >

The mathematical expression of the score:

> x+yFraction (1,1) > > x-yFraction (- 1,3) > x*yFraction (2,9) >

Create a fraction object from a floating-point string:

> Fraction ('.25') Fraction (1,4) > Fraction (' 1.25') Fraction (5,4) > Fraction ('.25') + Fraction (' 1.25') Fraction (3,2) > zonal 2.5 > o=Fraction (* z.as_integer_ratio ()) > oFraction (5,2)

Built-in mathematical tools:

> import math > math.pi,math.e (3.141592653589793,2.7182828459045) > math.sin (2*math.pi / 180) 0.03489949670250097 > math.sqrt (14444), math.sqrt (3), math.sqrt (4) (12.0,1.7320508075688772,2.0) > pow (2jue 4), pow (2jue 5), 2prime4 (16,32,16,32) > abs (- 42.0), sum (1jue 2jue 3repar4) # Summation (42.0,21) > min # find the minimum number 1 > max (32.021) # find the maximum number 5 > >

Decimal truncation method:

> math.floor (2.567), math.floor (- 2.567) (2,-3) > > math.trunc (2.567), math.trunc (- 2.567) (2,-2) > int (2.567), int (- 2.567) (2,-2) > round (2.567), round (2.467), round (2.567) (3,2,2.57) >% .1f'% 2.567 '{0pur.2f}' .format (2.567) ('2.6,' 2.57') >

Range- generates random number function

> import random > random.random () 0.6746601025987419 > random.random () 0.569446993489575 > > random.random () 0.31398115374410795 > > random.randint (1d10) 4 > > random.randint (1d10) 1 > random.choice (['banane','apple','orange'])' banane' > random.choice (['banane','apple','orange'])' orange' > random.choice (['banane','apple','orange'])' orange' > random.choice (['banane','apple']) 'orange'])' apple' >

Set

Set, which is an unordered set of unique and immutable objects (collection), supports operations corresponding to mathematical set theory. An item can only appear once, no matter how many times it is added to the collection.

The collection is unordered and does not match keys to values, neither a sequence nor a mapping type.

> x = set ('abcde') > y = set (' bdxyz') > x in xFalse y ({'a','e','d','b','c'}, {'y','x','d','b','z'}) >'e' in xTrue > >'g'in xFalse > x-y {'c','e','a'} > x | y {'a','e', y {'a','x' 'During, 'baking,' centering,'z'} > x & y {'baked,' d'} > x ^ y {'clocked,' axed, 'yearly,' eyed, 'xpressed,' z'} > x > yborex

< y(False, False)>

> > # Add method inserts items, update calculates sets by location, and remove deletes an item based on the value. > z = x.intersection (y) # same as x & y > z {'baked,' d'} > z.add ('yert') > z {' baked, 'dashed,' yert'} > z.update (set ('ace')) > z {' baked, 'clocked,' eyed, 'axed,' dumped, 'yert'} > z.remove (' b') > z {'baked,' estranged, 'axed,' d' 'yert'} > a=set (' 123') > a=set ([1pje 2jue 3]) > a {1jre 2jue 3} > a.union ([3jue 4]) {1je 2jue 3,4} > a.intersection ((1m 3jue 5)) {1pm 3} > a.issubset (range (- 5pm 5)) True >

{} is still a dictionary in Python, and the empty collection must be created by the built-in function set and displayed in the same way.

> b = {1 type (b) > type ({}) > a = {} > a {} > type (c) > d=set () > dset () > type (d) >

Conversion of collections and lists:

> L = set (L) {1, 3, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 7, 7, 7, 7, 8, 7, 7, 7, 8, 7, 7, 7, 8, 7, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 8, 7, 7, 7, 8, 7, This is the end of this article on "sample Analysis of numbers in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report