table of Contents
Data Type Description
- Python data types can be divided into: numeric and non-numeric type
- Numeric
- Integer (int)
- Float (float)
- Boolean (bool)
- True (True)
- False (False)
- Complex type (complex)
- Mainly used for scientific computing
- Non-digital type
- String (str)
- List (list)
- Tuple (tuple)
- Dictionary (dict)
- In Python, all non-numeric type variable, have a common characteristic:
- It is a sequence that can be understood as a container
Digital Type
On 32-bit machines, the number of bits for 32-bit integer in the range of -2 31 to 2 31 - 1 ~ -2147483648 2147483647 i.e.
In the 64-bit system, 64 bits for an integer in the range of -2 63 to 2 63-1 ~ 9223372036854775807 i.e. -9223372036854775808
Type of digital creation
#!/usr/bin/env python3
# -*-coding:utf-8-*-
"""
@author:fyh
@time:2019/5/31
"""
# int类型创建
a = 10
b = 666
print(a) # 10
print(b) # 666
print(type(a)) # <class 'int'>
# float类型创建
c = 3.14
print(c)
print(type(c)) # <class 'float'>
# complex类型创建
d = 3 + 4j
print(d)
print(type(d)) # <class 'complex'>
# 布尔型
flag = True
print(flag)
print(type(flag)) # <class 'bool'>
type (obj) function can view the type of variable
Data type conversion
Sometimes, we need to built-in data type conversion, data type conversion, you only need to type the data as a function name.
- int (x) will be converted to an integer x
- a float (x) to convert to a floating-point number x
- Complex (x) will be converted to a complex number x, x is a real part, imaginary part is 0
- Complex (x, y) x and y to convert a complex number, x is the real part, imaginary part to y. x and y are numeric expression
- bool (x) will be converted to type bool x, it is non-zero True, False 0
#!/usr/bin/env python3
# -*-coding:utf-8-*-
"""
@author:fyh
@time:2019/5/31
"""
# int(x)
var1 = 3.14
print(int(var1)) # 3
# float(x)
var2 = 3
print(float(var2)) # 3.0
# complex(x)
var3 = 4
var4 = 2
print(complex(var3)) # (4+0j)
# complex(x, y)
print(complex(var3, var4)) # (4+2j)
# bool(x)
var5 = 1
print(bool(var5)) # True
var6 = 0
print(bool(var6)) # False
When numeric type conversion, x is the only type of digital conversion, if other types will complain:
str1 = "a"
print(int(str1)) # 会报错
computation
#!/usr/bin/env python3
# -*-coding:utf-8-*-
"""
@author:fyh
@time:2019/5/31
"""
print(2 + 2) # 4
print(50 - 5*5) # 25
print(8 / 5) # 1.6
# 在整数除法中,除法 / 总是返回一个浮点数,如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符 //
print(17 / 3) # 5.666666666666667
print(17 // 3) # 5
# 注意:// 得到的并不一定是整数类型的数,它与分母分子的数据类型有关系。
print(7.0 // 2) # 3.0
print(7 // 2.0) # 3.0
# %操作符返回除法的余数
print(17 % 3) # 2
# Python 可以使用 ** 操作来进行幂运算
print(5 ** 2) # 25
print(2 ** 7) # 128
Digital type related functions
Math-related functions in the math package
# abs(x) 返回数字的绝对值,如abs(-10) 返回 10
# ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5
# cmp(x, y) 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1
# exp(x) 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045
# fabs(x) 返回数字的绝对值,如math.fabs(-10) 返回10.0
# floor(x) 返回数字的下舍整数,如math.floor(4.9)返回 4
# log(x) 如math.log(math.e)返回1.0,math.log(100,10)返回2.0
# log10(x) 返回以10为基数的x的对数,如math.log10(100)返回 2.0
# max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。
# min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。
# modf(x) 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。
# pow(x, y) x**y 运算后的值。
# round(x [,n]) 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。
# sqrt(x) 返回数字x的平方根,数字可以为负数,返回类型为实数,如math.sqrt(4)返回 2.0
Small integer object pool
>>> a = 5
>>> b = 5
>>> id(a),id(b)
(1892969616, 1892969616)
>>> c = 500
>>> d = 500
>>> id(c),id(d)
(2499230475376, 2499234158160)
>>>
Small integer object pooling: CPython integer from -5 to 256, there is a small integer objects in the pool forever, it will not be released.
Pooling: caching mechanism.