The number of Python learning

Source: Internet
Author: User
Tags floor division pow

Digital
The numbers in Python are typical, typically in a very small number, or a large number, and the C-language number type can report an overflow error if it represents a large number, but Python does not,
The complete tools for Python numeric types include
Integers and floating-point numbers
Plural
Decimal number for fixed precision
Rational Fractions
Collection
Boolean type
Infinite integer Precision
Built-in modules and functions for various numbers


Numeric Constants
1234,0,999999 integer (infinite size)
1.23,45.6 floating Point
0177,0x9ff,0b101010 octal, hex, binary version 2.6
0o177,0x9ff,0b101010 octal, hex, binary version 3.0
3+4j,3.0+4.0j plural


built-in math tools and extensions
An expression
+-*/>> * & etc.
built-in math functions
POW ABS round int hex bin etc.
Bin tells an integer to grab a binary string output
>>>m = 5
>>>bin (M)
' 0b101 '
Common Modules
Random Math, etc.

python expression operators
Just say a few special
X and Y Logic and
x or y logic or
Not x logical non-

The above three are not in the C language && | | !

Priority Issues
Priority is generally not considered, the safest way is to give yourself the order you want to add parentheses.

Mixed Type
Two operand types are different operations, generally speaking, the number of simple type is converted into a complex number outside the volume and then the operation

Digital Formatting
>>>num = 1/3.0
>>> '%4.2f '% num
' 0.33 '

>>> '%e '% num
3.333333e-001

>>> ' {0:4.2f} '. Format (num)
' 0.33 '


Division Operation
X/Y Traditional division integer retains only the integer part, minus the fractional part if it is a floating-point number, the fraction is retained

x//y Floor division does not take into account the type of operand, it always omits the number of decimal parts


other built-in math tools

In addition to the core types, Python also supports built-in functions and built-in modules.

Import Math

Math.PI
Math.e
Math.sin (2*math.pi/180)
MATH.SQRT (2)
Pow (2,4)
ABS (2.4)
SUM (1,2,4)
Min (4,5,6)
Max (3,4,5,6)
Math.floor (3.4) 3
Math.floor (-3.4)-4
Math.trunc (-3.4)-3
Math.trunc (3.4) 3
Int (2.3) 2
Int (-2.3)-2
Round (2.567) 3
Round (2.467) 2
Round (2.567,2) 2.56999999999



Import Random

Random.random ()
Random.randint (1,10) 5
Random.randint (1,10) 4
Random.choice ([' AA ', ' BB ', ' cc ']); ' AA '
Random.choice ([' AA ', ' BB ', ' cc ']); ' BB '


Fraction Type

From fractions import fraction

>>>x = fraction (1,3)
>>>x
Fraction (1,3)
You can also perform mathematical operations

>>>1/3
0.33333333
>>>fraction (1,3)
Fraction (1,3)


Collection

>>>x = set (' ABCD ')
>>>x
Set ([' A ', ' B ', ' C ', ' d '])

>>> ' a ' in X
True

>>>y = set (' Bdfe ')
>>>x-y
Set ([' A ', ' C '])
>>>x&y
Set ([' A ', ' B ', ' C ', ' d ', ' e ', ' f '])
>>>x|y
Set ([' B ', ' d '])

The number of Python learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.