current position:Home>On the built-in object type of Python -- number (one of the differences between py2 and PY3)
On the built-in object type of Python -- number (one of the differences between py2 and PY3)
2022-06-24 05:06:51【Python advanced】
Python It's an object-oriented programming language , Everything in a program can be treated as an object .Python Built in objects can be divided into simple types and container types , Simple types are mainly numerical data , A container type is a collective that can contain other object types , Such as sequence 、 Tuples 、 Mapping, etc. .
In simple type objects ,Python The numeric data of includes integers (int)、 Long integer (long)、 floating-point (float)、 The plural (complex) And Boolean (bool) etc. . In container type objects , A sequence is a class of objects whose elements are stored in sequence , Mainly including string (string)、 list (list)、 Tuples (tuple) Other types . A mapping is a structure that accesses values through keys ,Python The only mapping structure in is the dictionary (dict) object . In addition to simple type and container type objects , Another special object is None, Is an empty object .
All objects have data properties and built-in methods . The method of an object is what we often call a function , For performing operations , Act on objects . The properties and methods of an object can be defined by the dot operator (.) To call .
Today, Xiaobian will show you something about Python Simple type objects in built-in objects , That is, the number type .Python The values in can be various numbers , Include integers 、 long integer 、 Floating point numbers 、 The plural 、 Boolean type number and so on . I wrote an article the day before yesterday , About how to execute in an interactive environment Python Program , The following figure shows some examples , First, let's take a look at the number type objects .
One 、 Constant
Python The numeric constants in support of multiple formats , Our common constants are integer and floating-point . Actually Python Scientific counting is also supported in , Floating point numbers are often represented by scientific notation , Such as 1.2e-3 or 1.2E-3 Express 1.2*10 Of -3 Power . In addition to the default decimal number , You can also use binary 、 octal 、 Hexadecimal to denote constants , However, prefix these numbers to distinguish them from decimal numbers , Binary system 、 octal 、 The hexadecimal prefixes are 0b、0、0x.Python The numeric constants in also support complex and fractional forms , But there is one thing to pay attention to when calculating fractions , Look at the example below .
py2 Fraction operation in version
py3 Fraction operation in version
Do you feel a little confused ? stay py2 The result of the integer division operation in is still an integer , It's rounding , Instead of rounding . When one of the numbers involved in the operation is a floating-point number ,Python Will automatically convert another number implicitly to a floating-point number , So the result of the second operation is a floating point number . stay py3 in , The meaning of division is the same as what we usually see , Not rounding . let me put it another way , The divisor and dividend of division are in py3 Floating point number conversion has been done in , And then divide them , The resulting quotient is a floating point number .
Python There is also a double slash in the operation of //, It also represents division , This division yields the maximum integer quotient , This division pattern is also called floor division . stay py2 This mode is supported in , When two integers are divided , The result is only the integer part of the quotient .% Represents the remainder operator , You can get the remainder . As shown in the figure below :
floor Division and remainder operations
Two 、 fraction
Fractions consist of two parts, numerator and denominator . stay Python in , function Fraction(x, y) It's a score x/y. Before using this function , You need to import the score module fractions, After the score is built , Can be used in expressions . Of course , You can also convert a floating-point string to a fraction , As shown in the figure below :
fraction Fraction Function operation
3、 ... and 、 Boolean type
Boolean objects have only two values , to (True) And wrong (False). For null data types , Its Boolean values are False. Generally speaking , Boolean values are often used in condition judgment , It is usually used as a test condition for a branch or loop of a program .
Four 、 The plural
The plural number is composed of the rational part and the irrational part . The rational part is called the real part , This part is optional ; The irrational part is called the imaginary part , This part is required , The suffix of the imaginary part is generally j perhaps J Express . Complex numbers also support four operations in constants, etc , The result of running between complex numbers is still complex , As shown in the figure below :
Four operations in complex numbers
Numbers are the most basic numeric type , No matter what kind of programming language is inseparable from it . I hope you guys can master it well , In the part of fractional operation, we should pay special attention to mastering py2 and py3 The difference in the version , This knowledge point is often encountered in the interview questions , In fact, this part is one of the differences between the two versions .
copyright notice
author[Python advanced],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/175/20210824190016503y.html
The sidebar is recommended
- Python analyzes and replaces images in web pages through regular expressions
- Code for Python to automatically connect to SSH
- Python code for rotating pictures
- Python gets the size and size of remote pictures
- Simple text processing using Python
- Python gets the date of the last day of the previous month
- Python UDP server
- Python code for connecting to Oracle Database
- Python code for connecting to Oracle Database
- Python zip and unzip data
guess what you like
There are several ways to implement Python multithreading
Python script to get process memory usage
Using mongodb to store crawler data in Python
Python contains Chinese string length
Python asynchronous task queue
Python urllib downloads files from a remote server to the local
This Python class is used to get the code execution time
Python User defined class implementation
Python3 calculates the probability of the same flower
Calculating the CPU utilization of windows with Python
Random recommended
- Python simulates keyboard input and mouse operation
- Python get windows special folder path code
- Python segmented download file
- Python setting environment variables for processes
- Python uses multithreading to execute CMD command to shut down
- Python gets the number of days in the previous month
- Python generates 128 barcode (code128)
- [Python] code sharing for drawing 2D scatter diagram
- [Python] output the names of students with the highest or lowest scores and students with lower than average scores
- [Python] implement the maximum and minimum distance algorithm
- Python parsing JSON data tutorial
- Deep learning project: how to use Python and opencv for face recognition
- Pychart developing Django project template common filter tutorial
- Using Python to call cloud API to monitor the traffic usage of lightweight application servers
- Face core app access - server Python demo
- Can Python's "King" status be maintained in the next decade?
- How do Python crawlers make money? Six Python crawlers make money. It's not a problem to engage in sidelines
- Java or python, which is more suitable for AI development?
- How to make beeps in Python for windows ECS
- Three sorts (select, bubble, insert) Python version
- Python automatic switching environment
- Detailed explanation of python3 rounding problem
- [Master Wu's Python bakery] day 2
- [Master Wu's Python bakery] day 1
- [Master Wu's Python bakery] day 3
- [Master Wu's Python bakery] day 4
- [Master Wu's Python bakery] day 5
- [Master Wu's Python bakery] day 6
- [Master Wu's Python bakery] day 7
- [Master Wu's Python bakery] day 8
- Introduction and examples of socket programming in Python
- Python notes - permissionerror
- Python notes - deprecationwarning
- Python notes - Open Python project
- Python notes - PIL Library
- Python notes - with as statement
- How to export IPython history to Py file?
- Python multithreading combined with dataloader to load data
- Make Python not echo commands that get password input
- In c/c++ and python programming, null and none cannot be distinguished clearly