search
HomeBackend DevelopmentPython TutorialDetailed explanation of python's numeric type variables and their methods

Foreword

Python data types are not allowed to be changed, which means that if the value of the Number data type is changed, the memory space will be reallocated. Not much to say below, let’s take a look at the detailed introduction.

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

var1 = 1
var2 = 10

You can also use the del statement to delete some Number object references.

You can delete single or multiple objects by using the del statement, for example:

del var 
del var_a, var_b

Python supports four different numeric types:

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

Long integer (long) - an integer of infinite size. The integer ends with an uppercase or lowercase L, such as: 51924361L.

Floating point type (float) - 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 10^2 = 250)

Complex number (complex) - A complex number consists of a real part and an imaginary part, you can use a + bj, or complex(a,b) means that the real part a and the imaginary part b of the complex number are both floating point types.

Python Number 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 float


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 sequence s to a tuple


list(s ) Convert sequence s to A list


chr(x ) 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 string


oct(x ) Convert an integer to an octal string

Python math functions:

function Return value (description)


abs( x) Returns 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


exp(x) Returns the x power of e (ex), such as math.exp(1) returns 2.718281828459045


fabs(x) Returns the absolute value of the number, such as math.fabs(-10) returns 10.0


floor(x) Returns the rounded 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, for example, 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 To access, you need to import the math module and call this method through a static object.

Since I am not sure which method is required, it is best to introduce the math module when using python math functions in the future.


2. Mathematical functions that can be directly accessed:

abs(x)  返回数字的绝对值,如abs(-10) 返回 10
cmp(x, y) 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1
max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。 
min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。 
round(x [,n]) 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。

Example:

#!/usr/bin/python 
#coding:uft-8 import math # 导入 math 模块   
print "max(80, 100, 1000) : ", max(80, 100, 1000) 
print "min(80, 100, 1000) : ", min(80, 100, 1000) 
print "round(80.23456, 2) : ", round(80.23456, 2) 
print "math.exp(-45.17) : ", math.exp(-45.17) 
print "math.pow(100, 2) : ", math.pow(100, 2)

Python random number function:

Function Description

choice(seq)    从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。

randrange ([start,] stop [,step])   从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1

random()       随机生成下一个实数,它在[0,1)范围内。

seed([x])      改变随机数生成器的种子seed。

shuffle(lst)   将序列的所有元素随机排序

uniform(x, y)  随机生成下一个实数,它在[x,y]范围内。

注意:

1、python的随机数函数是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

实例:

#!/usr/bin/python 
# -*- coding: UTF-8 -*- 
import random   
print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]) 
# 输出 100 <= number < 1000 间的偶数 
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2) 
# 生成第一个随机数 
print "random() : ", random.random() 
# 生成同一个随机数 random.seed( 10 ) 
print "Random number with seed 10 : ", random.random() 
list = [20, 16, 10, 5]; 
random.shuffle(list) 
print "随机排序列表 : ", list
print "uniform(5, 10) 的随机数为 : ", random.uniform(5, 10)

Python三角函数:

 函数            描述

acos(x)     返回x的反余弦弧度值。

asin(x)     返回x的反正弦弧度值。

atan(x)     返回x的反正切弧度值。

atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。

cos(x)      返回x的弧度的余弦值。

hypot(x, y) 返回欧几里德范数 sqrt(x*x + y*y)。

sin(x)      返回的x弧度的正弦值。

tan(x)      返回x弧度的正切值。

degrees(x)  将弧度转换为角度,如degrees(math.pi/2) , 返回90.0

radians(x)  将角度转换为弧度

注意:

1、Python三角函数是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象调用该方法。

实例:

#!/usr/bin/python 
#coding: UTF-8 import math   
print "degrees(3) : ", math.degrees(3) 
print "radians(-3) : ", math.radians(-3) 
print "sin(3) : ", math.sin(3) 
print "cos(3) : ", math.cos(3) 
print "tan(3) : ", math.tan(3) 
print "acos(0.64) : ", math.acos(0.64) 
print "asin(0.64) : ", math.asin(0.64) 
print "atan(0.64) : ", math.atan(0.64) 
print "atan2(-0.50,-0.50) : ", math.atan2(-0.50,-0.50) 
print "hypot(0, 2) : ", math.hypot(0, 2)

Python数学常量:

常量              描述
 pi      数学常量 pi(圆周率,一般以π来表示)
 e       数学常量 e,e即自然常数(自然常数)。

注意:

1、Python数学常量也是不能直接访问的,需要导入 math 模块,然后通过 math 静态对象访问。

实例:

#!/usr/bin/python 
#coding: UTF-8 
import math   
print math.pi print math.e

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能有所帮助,如果有疑问大家可以留言交流。


Statement
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
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.