python数据类型
来源:互联网 发布:子弹算法 编辑:程序博客网 时间:2024/06/14 18:01
整形:
int类型的数字都为整形.
a=raw_input() 接收的都是字符串
a=input() 接收的都是数字类型
abs()这个方法为取绝对值的方法:
b=-20
print(abs(b))
输出为20
浮点型:
float类型(带小数点的数字)为浮点型
round()方法默认保留一位小数,并且按四舍五入的方法来保留
a=1.22
b=1.51
c=2.555
d=1.545
print(round(a))
print(round(b))
#保留2位小数
print(round(c,2))
print(round(d,2))
输出结果为:1.0 2.0 2.56 1.54
关于round有一些说法,可以参考:http://www.runoob.com/w3cnote/python-round-func-note.html
布尔类型 ##:
True False 一般用于逻辑判断
a=”
b=’aa’
print(not a)
print(not b)
输出结果为True False,由此可见空的布尔值为false,字符串的布尔值为true
字符串:
用引号定义的变量都是字符串
str1=’abcdefg’
str2=’aa’
strt3=’bb’
print (type(str1))
print (dir(str1))
print (str1[0],str1[1],str1[2])
#find()查找一个字符串,找到结果显示要第一个匹配字符的下标,找不到直接返回-1
a = ‘abdavidcdbeckham’
print (a.find(‘david’))
print (a.find(‘sd’))
输出结果为2、-1
#replace替换()
print(a.replace(‘beckham’,’dada’))
输出结果:abdavidcddada
#split 字符串分割,以cd为分割符,分割完为列表
print (a.split(‘cd’))
输出结果:[‘david’, ‘beckham’]
#join() 字符串拼接
print(‘hello ‘.join(a.split(‘cd’)))
输出结果为:adavidhello beckham
#strip() 去掉字符串前后空格,rstrip()去右边空格,lstrip()去左边空格
b= ’ bcdabc ’
print b
print(b.strip())
输出结果将字符串两端空格去掉
#format()
name =’david’
age = 26
print ‘hello ’ + name
#%s 代表字符串 %d代表的是整形 %f代表的是浮点型
print(‘hello %s’) %name
输出都是:hello david
print(‘hello {0},age is {1}’).format(name,age)
print(‘hello {0},age is {1}’.format(name,age))
输出都是;:hello liaochao,age is 26
print (‘{name}:{age}’.format(name=’david’,age=32))
输出为:david:32
- Python 数据类型
- python--数据类型
- python 数据类型
- python数据类型
- Python---数据类型
- python数据类型
- python数据类型
- [python] 数据类型
- python数据类型
- python 数据类型
- Python数据类型
- Python数据类型
- Python-数据类型
- Python数据类型
- Python数据类型
- python数据类型
- python 数据类型
- python数据类型
- 输入一串字符,以回车换行符为结束标志,并统计输入的英文字符和数字字符
- QUARTUS Ⅱ仿真(一)
- Ubuntu默认防火墙安装、启用、配置、端口、查看状态相关信息
- 文件和byte数组之间相互转换
- live555学习阶段二之二SDP流程(H264)
- python数据类型
- Freedom of Choice URAL
- 我的Hello World
- Intellij IDEA Debug调试方法
- React Native 混合编程 之访问iOS设备
- MySql学习记录4-字符串处理函数
- python之实战----广义线性模型
- 使用vscode 开发electron 之 启动配置
- 1050. 螺旋矩阵(25)