Python numbers

Nov 23, 2016 am 10:42 AM
python

Python numeric data type is used to store numerical values.

The data type is not allowed to be changed, which means that if the value of the numeric data type is changed, the memory space will be reallocated.

The following examples of numeric objects will be created when variables are assigned:

var1 = 1

var2 = 10

You can also use the del statement to delete some numeric object references. The syntax of

del statement is:

del var1[,var2[,var3[....,varN]]]]

You can delete single or multiple items by using del statement Objects, for example:

del var

del var_a, var_b

Python supports four different numerical types:

Integer (Int) - often called an integer or integer , is a positive or negative integer without a decimal point.

Long integers - infinite-sized integers, with an uppercase or lowercase L at the end of the integer.

Floating point real values ​​- The floating point type consists of an integer part and a decimal part. The floating point type can also be expressed using scientific notation (2.5e2 = 2.5 x 102 = 250)

Complex numbers ( (complex numbers )) - The imaginary part of a complex number ends with the letter J or j. Such as: 2+3i

int

long

float

complex

10 51924361L 0.0 3.14j

100 -0x19 323L 15.20 45.j

-786 0122L -21.9 9.322e-36j

080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j

-0490 535633629843L -90. -.6545+0J

-0x260 -0523 18172735L -32.54e100 3e+26J

0x69 -4721885298529L 70.2-E12 4.53e-7j

Long type also A lowercase "L" can be used, but it is recommended that you use an uppercase "L" to avoid confusion with the number "1". Python uses "L" to display long integers.

Python also supports complex numbers. Complex numbers are composed of real parts and imaginary parts. They can be represented by a + bj, or complex(a,b). The real part a and the imaginary part b of complex numbers are both floating point types

Python Numeric type conversion

int(x [,base ]) Convert x to an integer

long(x [,base ]) Convert x to a long integer

float(x) Convert x convert to A floating point number

complex(real [,imag ]) Create a complex number

str(x) Convert object x to string

repr(x) Convert object x to expression string

eval(str) Used to evaluate a valid Python expression in a string and return an object

tuple(s)                 Convert the sequence s to a tuple    

list(s)                                . Convert an integer to a character

unichr(x) Convert an integer to a Unicode character

ord(x) Convert a character to its integer value

hex(x) Convert an integer to a hexadecimal Convert an integer to an octal string

oct(x) Convert an integer to an octal string

abs(x) Return The absolute value of the number, such as abs(-10) returns 10

ceil(x) Returns the upward integer of the number, such as math.ceil(4.1) returns 5

cmp(x, y) If x < y returns - 1, if x == y return 0, if x > y return 1

exp(x) Returns e to the power of x) Returns the rounded down integer of the number, such as math.floor(4.9) returns 4

log(x) For example, math.log(math.e) returns 1.0, math.log(100,10) returns 2.0

log10( x) Returns the logarithm of x based on 10, such as math.log10(100) returns 2.0

max(x1, x2,...) Returns the maximum value of the given parameter, which can be a sequence.

min(x1, x2,...) Returns the minimum value of the given parameter, which can be a sequence.

modf(x) Returns the integer part and decimal part of x. The numerical signs of the two parts are the same as x, and the integer part is expressed in floating point type.

pow(x, y) The value after x**y operation.

round(x [,n]) Returns the rounded value of the floating point number x. If the n value is given, it represents the number of digits rounded to the decimal point.

sqrt(x) Returns the square root of the number In games, security and other fields, it is often embedded into algorithms to improve algorithm efficiency and improve program security.

Python includes the following commonly used random number functions:

Function

Description

choice(seq) Randomly select an element from the elements of the sequence, such as random.choice(range(10)), from 0 to Pick an integer at random from 9.

randrange ([start,] stop [,step]) Get a random number from the set in the specified range, increasing by the specified base. The default value of the base is 1

random() Randomly generate the next real number, which is Within the range [0,1).

seed([x]) Change the seed of the random number generator. If you don't understand the principle, you don't have to set the seed specifically, Python will choose the seed for you.

shuffle(lst) Randomly sort all elements of the sequence

uniform(x, y) Randomly generate the next real number, which is in the range [x, y].

Python trigonometric functions

Python includes the following trigonometric functions:

Function

Description

acos(x) Returns the inverse cosine of x in radians.

asin(x) Returns the arcsine radians value of x.

atan(x) Returns the arc tangent of x in radians.

atan2(y, x) Returns the arc tangent of the given X and Y coordinate values.

cos(x) Returns the cosine of x in radians.

hypot(x, y) Returns the Euclidean norm sqrt(x*x + y*y).

sin(x) Returns the sine of x in radians.

tan(x) Returns the tangent of x in radians.

degrees(x) Convert radians to angles, such as math.degrees(math.tan(1.0)), return 30.0

radians(x) Convert angles to radians

Python math constants

constants

Description

pi Mathematical constant pi (pi, generally expressed as π)

e Mathematical constant e, e is a natural constant (natural constant).

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

HadiDB: A lightweight, horizontally scalable database in Python HadiDB: A lightweight, horizontally scalable database in Python Apr 08, 2025 pm 06:12 PM

HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

Does mysql need the internet Does mysql need the internet Apr 08, 2025 pm 02:18 PM

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Can mysql workbench connect to mariadb Can mysql workbench connect to mariadb Apr 08, 2025 pm 02:33 PM

MySQL Workbench can connect to MariaDB, provided that the configuration is correct. First select "MariaDB" as the connector type. In the connection configuration, set HOST, PORT, USER, PASSWORD, and DATABASE correctly. When testing the connection, check that the MariaDB service is started, whether the username and password are correct, whether the port number is correct, whether the firewall allows connections, and whether the database exists. In advanced usage, use connection pooling technology to optimize performance. Common errors include insufficient permissions, network connection problems, etc. When debugging errors, carefully analyze error information and use debugging tools. Optimizing network configuration can improve performance

How to solve mysql cannot connect to local host How to solve mysql cannot connect to local host Apr 08, 2025 pm 02:24 PM

The MySQL connection may be due to the following reasons: MySQL service is not started, the firewall intercepts the connection, the port number is incorrect, the user name or password is incorrect, the listening address in my.cnf is improperly configured, etc. The troubleshooting steps include: 1. Check whether the MySQL service is running; 2. Adjust the firewall settings to allow MySQL to listen to port 3306; 3. Confirm that the port number is consistent with the actual port number; 4. Check whether the user name and password are correct; 5. Make sure the bind-address settings in my.cnf are correct.

See all articles