程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Data type of Python

編輯:Python

python Numeric type

author:Once Day date:2022 year 2 month 16 Japan

The purpose of this document is to summarize the relevant contents , Scattered knowledge is difficult to remember and learn .

This document is based on windows platform .

View a full range of documents :python Basics _CSDN Blog .

List of articles

      • python Numeric type
        • 1. Variables and constants
        • 2. Numeric type
        • 3. Numeric type conversion function
        • 4. Numeric type operation
        • 5.bool type
        • notes : The content of this article is collected and summarized on the Internet , For learning only !

1. Variables and constants

Variable : While the program is running , The amount by which the value changes

Constant : While the program is running , A quantity whose value does not change

stay Python in , Variable is variable , It has no type , What we say " type " Is the type of object in memory that the variable refers to .

therefore Python Variables in do not need to declare types . Assignment creates a variable :

var_a=100

Variables should be assigned values before they are used , References to unassigned variables will appear NameError.

python Variable points to an object , This object has types and properties , Therefore, different types of values can be assigned to the same variable successively .Python Everything in is the object , Variables are references to objects !

>>> a=100
>>> a
100
>>>a='onceday'
>>>a
'onceday'

python There is no real constant , Some means are needed to simulate the use of . Follow up .

2. Numeric type

Python The numeric type is used to store numeric values . The value type is not allowed to be changed , If you change the value of the digital data type , Memory space will be reallocated .

Python Supports three different number types , Integers 、 Floating point and complex number :

  • integer (Int) - It's usually called an integer or an integer , It's a positive or negative integer , Without decimal point .Python3 There is no limit to the size of an integer , Can be viewed as Long Type used , therefore Python3 No, Python2 Of Long type .
  • floating-point (float) - Floating point type consists of integer part and decimal part , Floating point types can also be represented by scientific notation (2.5e2 = 2.5 x 102 = 250)
  • The plural ( (complex)) - The plural consists of the real part and the imaginary part , It can be used a + bj, perhaps complex(a,b) Express , The real part of a complex number a Deficiency part of harmony b It's all floating point .

Python3 The integer of can be regarded as Long type ( Longer integers )

When it comes to numbers , It can be expressed in octal or hexadecimal :

  • Hexadecimal use 0x The prefix and 0-9,a-f Express , for example :0xff00,0xa5b4c3d2.

  • Octal uses 0o The prefix and 0-7 Express , for example 0o12

Python A small integer object pool will be created automatically during initialization , Convenient to call , Avoid late duplicate generation ! The scope is -5~256 about , This is a common range .

Python There is an integer buffer , The integer just deleted , It won't be removed and recycled immediately , It's buffering in the background for a while , Waiting for the next possible call .

3. Numeric type conversion function

Conversion of data types , Just use the data type as the function name .

  • int(x) take x Convert to an integer .
  • float(x) take x Converts to a floating point number .
  • complex(x) take x To a complex number , The real part is x, The imaginary part is 0.
  • complex(x, y) take x and y To a complex number , The real part is x, The imaginary part is y.x and y It's a number expression .

Be careful : Cast may lose precision .

4. Numeric type operation

Python The interpreter can be used as a simple calculator , You can enter an expression in the interpreter , It will output the value of the expression .

An expression is a normal mathematical expression , But there are a few things to note :

  • The result of floating-point operation may be different on different machines .
  • // What you get is not necessarily a number of integer type , It has something to do with the data type of denominator . Such as 6.0//3=2.0
  • Different types of mixed operations convert integers to floating-point numbers .

5.bool type

stay Python In language , Boolean type has only two values ,True And False.

for example 0、0.0、-0.0、 An empty string 、 An empty list 、 An empty tuple 、 An empty dictionary , These are judged to be False. and -1、"False" Judged to be True.

You can use bool Function test .** Boolean types can be used and、or and not operation .** You can even perform four operations , here

True=1,False=0.

>>> True *3
3
>>> False -1
-1

notes : The content of this article is collected and summarized on the Internet , For learning only !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved