Python3 中有六个标准的数据类型:Number(数字)、String(字符串)、List(列表)、Tuple(元组)、Sets(集合)、Dictionary(字典)。
- 不可变数据(四个):Number(数字)、String(字符串)、Tuple(元组)、Sets(集合);
- 可变数据(两个):List(列表)、Dictionary(字典)。
数字类型包括:int(整型)、float(浮点数)、complex(复数)、bool(布尔值)。
注:python2中还有一个长整型,python3已经没有了。
#a=10 #b=10.0 #c=True #d=1+2j #print(type(a)) #print(type(b)) #print(type(c)) #print(type(d))
运行结果如下:
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
数值取整的三种方法:向下取整int,四舍五入round,向上取整ceil
import math
a=2.23 b=6.66 print(int(a),int(b)) #2,6 print(round(a),round(b)) #2,7 print(math.ceil(a),math.ceil(b)) #3,7
二、String 字符串类型:
在单引号\双引号\三引号内,输入一串字符即构成一个字符串。例如,name='张三' 这样name这个变量就是字符串类型。单引号,双引号使用起来没有区别,三引号一般用于多行注释。
字符串有很多常用的方法,经常使用的如下:
1、去除空格:
str.strip():删除字符串两边的指定字符,括号的写入指定字符,默认为空格 str.lstrip():删除字符串左边的指定字符,括号的写入指定字符,默认为空格 str.rstrip():删除字符串右边指定字符,默认为空格
''' str.strip():删除字符串两边的指定字符,括号的写入指定字符,默认为空格 str.lstrip():删除字符串左边的指定字符,括号的写入指定字符,默认为空格 str.rstrip():删除字符串右边指定字符,默认为空格 ''' >>> a=" abc " >>> b = a.strip() # b='abc' >>> b = a.lstrip() # b = 'abc ' >>>b = a.rstrip() #b = ' abc'