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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1234
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles