1. python的数据类型
数值
字符串
列表
元组
字典
2.数值类型
整型
长整形
浮点型
a=1.0
b=5.0
c=2.545
round(a) 输出的是浮点型,四舍五入,最后一个小数点是偶数。
round(c,2)保留2位小数点,在四舍五入,最后一位是偶数
a = 2.4440
b = 5.1230
c = 10
print (round(c))
print(a/b)
print (round((a/b),2))
复数型complex
python对复数提供内嵌支持,这是其他大部分软件所没有的
-3.14j 8.32e-36j
>>num = 3.14j
>>type(num)
<type 'complex'>
整数
a = 456
type(a) = int
a.
字符串
定义字符串三种方法:
str = ‘this is a string’
str = “this is a string”
str = ‘“this is a string”’
三重引号除了能定义字符串还能做注释
-
字符串是个序列
a='abcde'
0是第一个取值,2是第二个。不包括c
0可以省略a[:2]
a[1:] "bcde"
a[-1] 是从后面取
a[: : ] 后面是step值如图:
默认是从左到右 也可以从右到左
-1 表示反方向,从右到左。如图
b.字符串常用的操作方法
我们可以通过函数dir() 来查看字符串的操作
a = "12478python"
print (dir(a))
常用的有:
find 查找字符串,如果找到返回字符串首字母匹配的下标信息,如果找不到返回-1
a = "12478python"
print(a.find("python"))
>>5
a = "12478python"
print(a.find("java"))
>> -1
replace 替换字符串
a = "12478python"
print(a.replace("python","java"))
>>12478java
split
拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表
a = "www.vfx.ttd.com"
print(a.split('.'))
>>['www', 'vfx', 'ttd', 'com']
分割次数3次
a = "www.vfx.ttd.com.ffd"
print(a.split('.',3))
>>['www', 'vfx', 'ttd', 'com.ffd']
strip 当左边有空格右边有空格,我们想去掉空格就用这个方法
s =" kk henrenzhen"
print (s.strip())
经常使用lstrip去掉左边的空格,rstrip去掉右边空格
format
%s 代表的是字符串,%d代表是整形
name = "zhoukaivfx"
print ("hello" + name)
print ("hello %s") %name
print ('hello {0}').format(name)
输出都一样的。