第五天学习:python数据类型(一)
一、整型
整型算术运算中,取整,去掉小数点后面数字
a = 100 b = 30 c = -4 print(a) print(b) print(a+b) print(a.__abs__() + c.__abs__()) print(dir(a)) print(abs(a) + abs(c)) print(a/b) 结果: D:\Python27\python.exe D:/PycharmProjects/test/test.py 100 30 130 104 ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] 104 3 Process finished with exit code 0
dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
二、浮点型
round()
默认保留一位小数
>>> round(2.555)
3.0
采用四舍五入方法
>>> round(2.4)
2.0
round(float,精度)
小数点精度的最后一位必须为偶数
>>> round(2.555,2)
2.56
#5进一位,为2.56,精度的最后一位为偶数6
>>> round(2.545,2)
2.54
#5进一位,为2.55,精度最后一位为非偶数5,则实际为2.54,舍弃进位
特殊情况:
>>> round(2.675,2)
2.67
机器中浮点数不一定能精确表达,因为换算成一串1和0后可能是无限位数的,机器已经做出了截断处理。那么在机器中保存的2.675这个数字就比实际数字要小那么一点点。这一点点就导致了它离2.67要更近一点点,所以保留两位小数时就近似到了2.67。
以上。除非对精确度没什么要求,否则尽量避开用round()函数。近似计算我们还有其他的选择:
- 使用math模块中的一些函数,比如math.ceiling(天花板除法)。
- python自带整除,python2中是/,3中是//,还有div函数。
- 字符串格式化可以做截断使用,例如 "%.2f" % value(保留两位小数并变成字符串……如果还想用浮点数请披上float()的外衣)。
- 当然,对浮点数精度要求如果很高的话,请用decimal模块
三、布尔值
一般用于返回值判断
布尔值:
True 真
False 假
以下为False
1、None;
2、False;
3、任何为0的数字类型,如:0,0.0,0j;
4、任何空序列,如:'',(),[];
5、任何空字典,例如:{};
6、用户定义的类实例,如果类定义了__bool__()或者__len__()方法,并且该方法返回0或者布尔值False。
其它所有值被解释器看作True。
四、字符串
定义字符串:'A',"A",'''A'''
字符串常用方法
下标:
>>> str1 = 'abcdefg'
>>> print(str1[0],str1[1])
('a', 'b')
常用方法:
find
replace
split
join
strip
format
实例:
>>> a = '123456abcdrfdshenzhen' >>> print(a.find('shen')) 13 >>> print(a.replace('shen','SHEN')) 123456abcdrfdSHENzhen >>> print(a.split('e')) ['123456abcdrfdsh', 'nzh', 'n'] >>> print('hello '.join(a.split('e'))) 123456abcdrfdshhello nzhhello n >>> b = ' abcd ' >>> print(b.lstrip()) abcd >>> print(b.rstrip()) abcd >>> print(len(b.lstrip())) 7 >>> print(b.strip()) abcd
#format
>>> name = 'guangdongshenzhen' >>> print('hello' + name) helloguangdongshenzhen >>> print('hello ' + name) hello guangdongshenzhen >>> print('hello %s') % name hello guangdongshenzhen
%s 字符串
%d 整型
%f 浮点数
format 函数可以接受不限个参数,位置可以不按顺序
>>> '{} {}'.format('hello','world') 'hello world' >>> '{0} {1}'.format('hello','world') 'hello world' >>> '{1} {0}'.format('hello','world') 'world hello'
设置参数:
>>> print('name:{name}, url:{url}'.format(name='baidu',url='www.baidu.com' )) name:baidu, url:www.baidu.com >>> print('hello {0}, my name is {1}'.format('name','age')) hello name, my name is age
注释方法:
井号:#
三个单引号:''' '''