Python基本数据类型之数字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WandDouDou/article/details/82700169

一、Python 支持三种不同的数值类型:

  • 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点。Python3 整型是没有限制大小的,可以当作 Long 类型使用,所以 Python3 没有 Python2 的 Long 类型。
  • 浮点型(float) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
  • 复数( (complex)) - 复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。

二、根据每个数据类型列举出来的实例,便于分类查看

1、整数类型

2、布尔类型

3、浮点数类型

4、复数类型

5、字符串与数字类型转换

6、常用数学函数

7、随机数函数

8、三角函数

# !/usr/bin/env python
# -*- coding:utf8 -*-
# 数字
import math
import random

print("------整数练习------")
# int (整数), 如 1, 通常被称为是整型或整数,是正或负整数,不带小数点。Python3 整型是没有限制大小的,可以当作 Long 类型使用
# <editor-fold desc="整数">
number1 = 0xA0F  # 十六进制
print(number1, "是16进制的整数")
number2 = 0o37  # 八进制
print(number2, "是8进制的整数")
number3 = 74  # 正整形
print(number3, "是正整数")
number4 = -245  # 负整形
print(number4, "是负整数")
number5 = 123456789123456789  # 长整形
print(number5, "是长整形整数")
# </editor-fold>

print("\n------布尔练习------")
# bool (布尔), 如 True。
# <editor-fold desc="布尔">
bool1 = True
print(bool1)
bool2 = False
print(bool2)
# </editor-fold>

print("\n------浮点数练习------")
# float (浮点数), 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
# <editor-fold desc="浮点数">
float1 = 15.20
print(float1, "是正浮点数")
float2 = -21.9
print(float2, "是负浮点数")
float3 = -32.54e100
print(float3, "科学记数法浮点数")
# </editor-fold>

print("\n------复数练习------")
# complex (复数), 如 1 + 2j、 1.1 + 2.2j,复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型
# <editor-fold desc="复数">
complex1 = 3.14j
print(complex1)
complex2 = 9.322e-36j
print(complex2)
complex3 = -.6545 + 0J
print(complex3)
complex4 = 3e+26J
print(complex4)
complex5 = 4.53e-7j
print(complex5)
# </editor-fold>

print("\n------数字常用方法练习------")
# <editor-fold desc="数字常用方法">
# int 将其他类型的值转换成int类型
print("\nint练习")
str1 = "123"
num1 = int(str1)
print("str1转换后的值为", num1)

# type 查看值的类型
print("\ntype练习")
print("str1的类型为", type(str1))
print("num1的类型为", type(num1))

# base 将值转换成多少进制,2代表二进制,8代表八进制,10代表十进制,16代表十六进制
print("\nbase练习")
num2 = "0101"
print("转换成二进制:", int(num2, base=2))
print("转换成八进制:", int(num2, base=8))
print("转换成十进制:", int(num2, base=10))
print("转换成十六进制:", int(num2, base=16))

# bit_length 一个字节为8位,一个汉字占3个字节 bit_length的输出是当前的数字转换成2进制以后总共有多少位(从左边第一个不是零的位置开始数)
print("\nbit_length练习")
num3 = 1  # 01
print("1的位数为:", num3.bit_length())
num4 = 4  # 100
print("4的位数为:", num4.bit_length())
# </editor-fold>

print("\n------数字数学函数练习------")
# <editor-fold desc="数字的数学函数">
print("\n------abs(x):返回数字的绝对值------")
print("abs(-40) : ", abs(-40))
print("abs(100.10) : ", abs(100.10))

print("\n------math.ceil(x):返回一个大于或等于 x 的的最小整数------")
print("math.ceil(-45.17) : ", math.ceil(-45.17))
print("math.ceil(100.12) : ", math.ceil(100.12))
print("math.ceil(100.72) : ", math.ceil(100.72))
print("math.ceil(math.pi) : ", math.ceil(math.pi))

# --------------------没明白----------------------
print("\n------math.exp(x):返回x的指数------")
print("math.exp(2) : ", math.exp(2))
print("math.exp(-45.17) : ", math.exp(-45.17))
print("math.exp(100.12) : ", math.exp(100.12))
print("math.exp(100.72) : ", math.exp(100.72))
print("math.exp(math.pi) : ", math.exp(math.pi))

print("\n------math.floor(x):返回数字的下舍整数,小于或等于 x------")
print("math.floor(-45.17) : ", math.floor(-45.17))
print("math.floor(100.12) : ", math.floor(100.12))
print("math.floor(100.72) : ", math.floor(100.72))
print("math.floor(math.pi) : ", math.floor(math.pi))

# --------------------没明白----------------------
print("\n------math.log(x):返回x的自然对数,x > 0------")
print("math.log(math.e) : ", math.log(math.e))
print("math.log(100,10)) : ", math.log(100, 10))
print("math.log(100.12) : ", math.log(100.12))
print("math.log(math.pi) : ", math.log(math.pi))

print("\n------math.log10(x):返回以10为基数的x的对数------")
print("math.log10(63) : ", math.log10(63))
print("math.log10(100.72) : ", math.log10(100.72))
print("math.log10(math.pi) : ", math.log10(math.pi))

print("\n------max( x, y, z, .... ):返回给定参数的最大值,参数可以为序列------")
print("max(80, 100, 1000) : ", max(80, 100, 1000))
print("max(-20, 100, 400) : ", max(-20, 100, 400))
print("max(-80, -20, -10) : ", max(-80, -20, -10))
print("max(0, 100, -400) : ", max(0, 100, -400))

print("\n------min(x1, x2, x3...)	:返回给定参数的最小值,参数可以为序列------")
print("min(80, 100, 1000) : ", min(80, 100, 1000))
print("min(-20, 100, 400) : ", min(-20, 100, 400))
print("min(-80, -20, -10) : ", min(-80, -20, -10))
print("min(0, 100, -400) : ", min(0, 100, -400))

print("\n------math.modf(x):返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示------")
print("math.modf(35) : ", math.modf(35))
print("math.modf(100.72) : ", math.modf(100.72))
print("math.modf(math.pi) : ", math.modf(math.pi))

print("\n------math.pow(x, y):返回x的y次方的值------")
print("pow(100, 2) : ", pow(100, 2))
print("math.pow(100, -2) : ", math.pow(100, -2))
print("math.pow(2, 4) : ", math.pow(2, 4))
print("math.pow(3, 0) : ", math.pow(3, 0))

print("\n------round(x [,n]):返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数------")
print("round(70.23456) : ", round(70.23456))
print("round(56.659,1) : ", round(56.659, 1))
print("round(80.264, 2) : ", round(80.264, 2))
print("round(100.000056, 3) : ", round(100.000056, 3))
print("round(-100.000056, 3) : ", round(-100.000056, 3))

print("\n------math.sqrt(x):返回数字x的平方根------")
print("math.sqrt(100) : ", math.sqrt(100))
print("math.sqrt(7) : ", math.sqrt(7))
print("math.sqrt(math.pi) : ", math.sqrt(math.pi))

# </editor-fold>

print("\n------数字随机数函数练习------")
# <editor-fold desc="数字随机数函数">
print("\n------random.choice():返回一个列表,元组或字符串的随机项------")
print("从 range(100) 返回一个随机数 : ", random.choice(range(100)))
print("从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 : ", random.choice([1, 2, 3, 5, 9]))
print("从字符串中 'Wangdoudou' 返回一个随机字符 : ", random.choice('Wangdoudou'))

print("\n------random.randrange ([start,] stop [,step]):返回指定递增基数集合中的一个随机数,基数缺省值为1------")
print("从 1-100 中选取一个奇数:randrange(1,100, 2) : ", random.randrange(1, 100, 2))
print("从 0-99 选取一个随机数:randrange(100) : ", random.randrange(100))

print("\n------random.random():返回随机生成的一个实数,它在[0,1)范围内------")
print("第一个随机数:random() : ", random.random())
print("第二个随机数:random() : ", random.random())

print("\n------random.seed():改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数,如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed------")
random.seed()
print("使用默认种子生成随机数:", random.random())
random.seed(10)
print("使用整数种子生成随机数:", random.random())
random.seed("hello", 2)
print("使用字符串种子生成随机数:", random.random())

print("\n------random.shuffle(lst):将序列的所有元素随机排序------")
m_list = [20, 16, 10, 5];
random.shuffle(m_list)
print("随机排序列表1 : ", m_list)
random.shuffle(m_list)
print("随机排序列表2 : ", m_list)

print("\n------random.uniform(x, y):随机生成下一个实数,它在[x,y]范围内------")
print("uniform(5, 10) 的随机浮点数 : ", random.uniform(5, 10))
print("uniform(7, 14) 的随机浮点数 : ", random.uniform(7, 14))
# </editor-fold>

print("\n------数字三角函数练习------")
# <editor-fold desc="数字的三角函数">
print("\n------math.acos(x):返回x的反余弦弧度值,x -- -1到1之间的数值。如果x是大于1,会报错------")
print("math.acos(0.64) : ", math.acos(0.64))
print("math.acos(-0.38) : ", math.acos(-0.38))
print("math.acos(0) : ", math.acos(0))
print("math.acos(-1) : ", math.acos(-1))
print("math.acos(1) : ", math.acos(1))

print("\n------math.asin(x):返回x的反正弦弧度值,x -- -1到1之间的数值。如果x是大于1,会报错------")
print("math.asin(0.64) : ", math.asin(0.64))
print("math.asin(-0.38) : ", math.asin(-0.38))
print("math.asin(0) : ", math.asin(0))
print("math.asin(-1) : ", math.asin(-1))
print("math.asin(1) : ", math.asin(1))

print("\n------math.atan(x):返回x的反正切弧度值------")
print("math.atan(0.64) : ", math.atan(0.64))
print("math.atan(0) : ", math.atan(0))
print("math.atan(10) : ", math.atan(10))
print("math.atan(-1) : ", math.atan(-1))
print("math.atan(1) : ", math.atan(1))

print("\n------math.atan2(y, x):返回给定的 X 及 Y 坐标值的反正切值------")
print("math.atan2(-0.50,-0.50) : ", math.atan2(-0.50, -0.50))
print("math.atan2(0.50,0.50) : ", math.atan2(0.50, 0.50))
print("math.atan2(5,5) : ", math.atan2(5, 5))
print("math.atan2(-10,10) : ", math.atan2(-10, 10))
print("math.atan2(10,20) : ", math.atan2(10, 20))

print("\n------math.cos(x):返回x的弧度的余弦值,返回值在 -1 到 1 之间------")
print("math.cos(3) : ", math.cos(3))
print("math.cos(-3) : ", math.cos(-3))
print("math.cos(0) : ", math.cos(0))
print("math.cos(math.pi) : ", math.cos(math.pi))
print("math.cos(2*math.pi) : ", math.cos(2 * math.pi))

print("\n------math.hypot(x, y):欧几里德范数 sqrt(x*x + y*y)------")
print("math.hypot(3, 2) : ", math.hypot(3, 2))
print("math.hypot(-3, 3) : ", math.hypot(-3, 3))
print("math.hypot(0, 2) : ", math.hypot(0, 2))

print("\n------math.sin(x):返回的x弧度的正弦值,返回值在 -1 到 1 之间------")
print("math.sin(3) : ", math.sin(3))
print("math.sin(-3) : ", math.sin(-3))
print("math.sin(0) : ", math.sin(0))
print("math.sin(math.pi) : ", math.sin(math.pi))
print("math.sin(math.pi/2) : ", math.sin(math.pi / 2))

print("\n------math.tan(x):返回x弧度的正切值,返回值在 -1 到 1 之间------")
print("math.tan(3) : ", math.tan(3))
print("math.tan(-3) : ", math.tan(-3))
print("math.tan(0) : ", math.tan(0))
print("math.tan(math.pi) : ", math.tan(math.pi))
print("math.tan(math.pi/2) : ", math.tan(math.pi / 2))
print("math.tan(math.pi/4) : ", math.tan(math.pi / 4))

print("\n------math.tan(x):将弧度转换为角度------")
print("math.degrees(3) : ", math.degrees(3))
print("math.degrees(-3) : ", math.degrees(-3))
print("math.degrees(0) : ", math.degrees(0))
print("math.degrees(math.pi) : ", math.degrees(math.pi))
print("math.degrees(math.pi/2) : ", math.degrees(math.pi / 2))
print("math.degrees(math.pi/4) : ", math.degrees(math.pi / 4))

print("\n------math.radians(x):将角度转换为弧度------")
print("math.radians(3) : ", math.radians(3))
print("math.radians(-3) : ", math.radians(-3))
print("math.radians(0) : ", math.radians(0))
print("math.radians(math.pi) : ", math.radians(math.pi))
print("math.radians(math.pi/2) : ", math.radians(math.pi / 2))
print("math.radians(math.pi/4) : ", math.radians(math.pi / 4))
# </editor-fold>

三、输出结果:


------整数练习------
2575 是16进制的整数
31 是8进制的整数
74 是正整数
-245 是负整数
123456789123456789 是长整形整数

------布尔练习------
True
False

------浮点数练习------
15.2 是正浮点数
-21.9 是负浮点数
-3.254e+101 科学记数法浮点数

------复数练习------
3.14j
9.322e-36j
(-0.6545+0j)
3e+26j
4.53e-07j

------数字常用方法练习------

int练习
str1转换后的值为 123

type练习
str1的类型为 <class 'str'>
num1的类型为 <class 'int'>

base练习
转换成二进制: 5
转换成八进制: 65
转换成十进制: 101
转换成十六进制: 257

bit_length练习
1的位数为: 1
4的位数为: 3

------数字数学函数练习------

------abs(x):返回数字的绝对值------
abs(-40) :  40
abs(100.10) :  100.1

------math.ceil(x):返回一个大于或等于 x 的的最小整数------
math.ceil(-45.17) :  -45
math.ceil(100.12) :  101
math.ceil(100.72) :  101
math.ceil(math.pi) :  4

------math.exp(x):返回x的指数------
math.exp(2) :  7.38905609893065
math.exp(-45.17) :  2.4150062132629406e-20
math.exp(100.12) :  3.0308436140742566e+43
math.exp(100.72) :  5.522557130248187e+43
math.exp(math.pi) :  23.140692632779267

------math.floor(x):返回数字的下舍整数,小于或等于 x------
math.floor(-45.17) :  -46
math.floor(100.12) :  100
math.floor(100.72) :  100
math.floor(math.pi) :  3

------math.log(x):返回x的自然对数,x > 0------
math.log(math.e) :  1.0
math.log(100,10)) :  2.0
math.log(100.12) :  4.6063694665635735
math.log(math.pi) :  1.1447298858494002

------math.log10(x):返回以10为基数的x的对数------
math.log10(63) :  1.7993405494535817
math.log10(100.72) :  2.003115717099806
math.log10(math.pi) :  0.49714987269413385

------max( x, y, z, .... ):返回给定参数的最大值,参数可以为序列------
max(80, 100, 1000) :  1000
max(-20, 100, 400) :  400
max(-80, -20, -10) :  -10
max(0, 100, -400) :  100

------min(x1, x2, x3...)    :返回给定参数的最小值,参数可以为序列------
min(80, 100, 1000) :  80
min(-20, 100, 400) :  -20
min(-80, -20, -10) :  -80
min(0, 100, -400) :  -400

------math.modf(x):返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示------
math.modf(35) :  (0.0, 35.0)
math.modf(100.72) :  (0.7199999999999989, 100.0)
math.modf(math.pi) :  (0.14159265358979312, 3.0)

------math.pow(x, y):返回x的y次方的值------
pow(100, 2) :  10000
math.pow(100, -2) :  0.0001
math.pow(2, 4) :  16.0
math.pow(3, 0) :  1.0

------round(x [,n]):返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数------
round(70.23456) :  70
round(56.659,1) :  56.7
round(80.264, 2) :  80.26
round(100.000056, 3) :  100.0
round(-100.000056, 3) :  -100.0

------math.sqrt(x):返回数字x的平方根------
math.sqrt(100) :  10.0
math.sqrt(7) :  2.6457513110645907
math.sqrt(math.pi) :  1.7724538509055159

------数字随机数函数练习------

------random.choice():返回一个列表,元组或字符串的随机项------
从 range(100) 返回一个随机数 :  61
从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 :  2
从字符串中 'Wangdoudou' 返回一个随机字符 :  u

------random.randrange ([start,] stop [,step]):返回指定递增基数集合中的一个随机数,基数缺省值为1------
从 1-100 中选取一个奇数:randrange(1,100, 2) :  5
从 0-99 选取一个随机数:randrange(100) :  13

------random.random():返回随机生成的一个实数,它在[0,1)范围内------
第一个随机数:random() :  0.6692551449533973
第二个随机数:random() :  0.8795005578640349

------random.seed():改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数,如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed------
使用默认种子生成随机数: 0.5781311079399765
使用整数种子生成随机数: 0.5714025946899135
使用字符串种子生成随机数: 0.3537754404730722

------random.shuffle(lst):将序列的所有元素随机排序------
随机排序列表1 :  [10, 16, 5, 20]
随机排序列表2 :  [20, 16, 5, 10]

------random.uniform(x, y):随机生成下一个实数,它在[x,y]范围内------
uniform(5, 10) 的随机浮点数 :  8.32548129941273
uniform(7, 14) 的随机浮点数 :  11.50216637642797

------数字三角函数练习------

------math.acos(x):返回x的反余弦弧度值,x -- -1到1之间的数值。如果x是大于1,会报错------
math.acos(0.64) :  0.8762980611683406
math.acos(-0.38) :  1.9605926232691573
math.acos(0) :  1.5707963267948966
math.acos(-1) :  3.141592653589793
math.acos(1) :  0.0

------math.asin(x):返回x的反正弦弧度值,x -- -1到1之间的数值。如果x是大于1,会报错------
math.asin(0.64) :  0.694498265626556
math.asin(-0.38) :  -0.38979629647426056
math.asin(0) :  0.0
math.asin(-1) :  -1.5707963267948966
math.asin(1) :  1.5707963267948966

------math.atan(x):返回x的反正切弧度值------
math.atan(0.64) :  0.5693131911006619
math.atan(0) :  0.0
math.atan(10) :  1.4711276743037347
math.atan(-1) :  -0.7853981633974483
math.atan(1) :  0.7853981633974483

------math.atan2(y, x):返回给定的 X 及 Y 坐标值的反正切值------
math.atan2(-0.50,-0.50) :  -2.356194490192345
math.atan2(0.50,0.50) :  0.7853981633974483
math.atan2(5,5) :  0.7853981633974483
math.atan2(-10,10) :  -0.7853981633974483
math.atan2(10,20) :  0.4636476090008061

------math.cos(x):返回x的弧度的余弦值,返回值在 -1 到 1 之间------
math.cos(3) :  -0.9899924966004454
math.cos(-3) :  -0.9899924966004454
math.cos(0) :  1.0
math.cos(math.pi) :  -1.0
math.cos(2*math.pi) :  1.0

------math.hypot(x, y):欧几里德范数 sqrt(x*x + y*y)------
math.hypot(3, 2) :  3.605551275463989
math.hypot(-3, 3) :  4.242640687119285
math.hypot(0, 2) :  2.0

------math.sin(x):返回的x弧度的正弦值,返回值在 -1 到 1 之间------
math.sin(3) :  0.1411200080598672
math.sin(-3) :  -0.1411200080598672
math.sin(0) :  0.0
math.sin(math.pi) :  1.2246467991473532e-16
math.sin(math.pi/2) :  1.0

------math.tan(x):返回x弧度的正切值,返回值在 -1 到 1 之间------
math.tan(3) :  -0.1425465430742778
math.tan(-3) :  0.1425465430742778
math.tan(0) :  0.0
math.tan(math.pi) :  -1.2246467991473532e-16
math.tan(math.pi/2) :  1.633123935319537e+16
math.tan(math.pi/4) :  0.9999999999999999

------math.tan(x):将弧度转换为角度------
math.degrees(3) :  171.88733853924697
math.degrees(-3) :  -171.88733853924697
math.degrees(0) :  0.0
math.degrees(math.pi) :  180.0
math.degrees(math.pi/2) :  90.0
math.degrees(math.pi/4) :  45.0

------math.radians(x):将角度转换为弧度------
math.radians(3) :  0.05235987755982989
math.radians(-3) :  -0.05235987755982989
math.radians(0) :  0.0
math.radians(math.pi) :  0.05483113556160755
math.radians(math.pi/2) :  0.027415567780803774
math.radians(math.pi/4) :  0.013707783890401887
 

猜你喜欢

转载自blog.csdn.net/WandDouDou/article/details/82700169