一.python 数据类型:数值,字符串,列表,元组,字典。以下操作是在linux 下 ipython中进行

1.数值

1》123  与  “123”的区别

答:123为数值,“123”在python中看做字符串

 

2》数值类型

整型,长整型,浮点型,复数型

 

整型的范围:-2147483648 (-2**31)----4294967296(2**32)

 

长整型:当一个数值大于整整的范围时,他就会变成长整型,如下:

In [3]: a = 19999999999999999999999
In [5]: a   //打印a的数值
Out[5]: 19999999999999999999999L
In [4]: type(a)  //查看a的类型,为长整型。
Out[4]: long

 

浮点型:浮点型就是带小数点的,科学计数法也属于浮点型。

In [6]: a = 0.345
 
In [7]: type(a)
Out[7]: float
 
In [8]: 3e+7
Out[8]: 30000000.0

 

除数与被除数只要有一个是浮点型,那结果就为浮点型。

In [10]: 3 / 2
Out[10]: 1
In [11]: type(3 / 2)
Out[11]: int
 
In [12]: 3 / 2.0
Out[12]: 1.5
 
In [13]: type(3 / 2.0)
Out[13]: float

 

复数型--complex:在后面加个“j”,就表示复数型。如:-3.14j     8.32e-32j

In [14]: a = 35j
 
In [15]: type(a)
Out[15]: complex

 

2.字符串--string

1》定义字符串的三种方法:单引号,双引号,三引号

-str = ‘this  is  a  string’    //单引号与双引号在python下没有任何区别
-str = “this is  a  string”
-str = ‘’‘this  is  a  string’‘’  //三重引号,除了能定义字符串外,还可以用作注释
 
In [22]: a = '''helll
 ///三个引号,可以自行添加换行符
In [24]: print(a)
helll
world

 

2>通过索引了,来取字符。叫切片

In [25]: a = 'abcde'
 
In [27]: a[0]  //第一个索引为0
Out[27]: 'a'
 
In [26]: a[1]
Out[26]: 'b'
 
In [28]: a[2]
Out[28]: 'c'
 
In [29]: a[-1]  //负1表示最后一个索引
Out[29]: 'e'

3>序列和切片。

序列 abcde   从前往后分别对应 0(a)1(b)2(c)3(d)4(e) ,从后往前对应-1(e) -2(d) -3(c) -4(b) -5(a)

 

从左往右,取字符串中连续的几个字母:

In [31]: a = 'abcdef'
 
In [32]: a[0:2]   //取ab两个字符,索引为2的不包括在内,所以c没显示出来。
Out[32]: 'ab'
 
In [33]: a[:2]   //还可以省略第一个索引。
Out[33]: 'ab'
 
In [34]: a[1:]  //从第一个开始取,取到最后。可以把最后一个索引省略。
Out[34]: 'bcdef'
 
In [35]: a[:]  //第一个索引省略,第二个索引省略,表示取全部。
Out[35]: 'abcdef'

从右往左取

In [36]: a[:-1]   //从第一个索引开始取,但不包括最后一个索引。也就是不取f
Out[36]: 'abcde'
 
In [37]: a[1:-1]   //从第一个索引开始取,取到最后,但不包括最后一个f字母
Out[37]: 'bcde'
 
In [39]: a[::1]    //从第一个开始取,取到最后,步长为1
Out[39]: 'abcdef'
 
In [40]: a[::2]  //从第一个开始取,取到最后,步长为2
Out[40]: 'ace'

从左往右取出b和c

In [45]: a
Out[45]: 'abcde'
 
In [46]: a[-4:-2]   //最后的-2是不包括在内的,默认从左到右的顺序,所以先写-4,在写-2
Out[46]: 'bc'

 

从右到左取出d和c

In [48]: a
Out[48]: 'abcde'
 
In [49]: a[-2:-4:-1]    // 负1 表示从右往左取,
Out[49]: 'dc'
 
In [7]: a = 'abcdef'   //取最后两位
In [8]: a[-2:]
Out[8]: 'ef'

 

二, 函数。以下操作是在pycharm中进行。

1> round

# 默认保留1为小数
#以四舍五入的方式计算
g = 3.5
h = 2.45
print('g',round(g))
print('h',round(h))
 
>>>  ('g', 4.0)
 ('h', 2.0)

2>round(float浮点型,精度)

#先进行四舍五入的运算,小数点最后一位必须为偶数

代码如下:

a = 3.046  正常的浮点型是四舍五入。但是碰到.5的情况,要求精度的前一位是偶数则四舍五入,如果要求精度的后一位是奇数则舍弃。
 
b = 2.143
c = 3.447
d = 3.567  .5情况,要求的精度是2,精度的前一位是7,为奇数舍弃。所以取值为d=3.56
e = 2.555  但是碰到.5的情况,如果要取舍的位数前的小数是奇数,则直接舍弃,如果偶数这向上取舍
 
f = 1.545
print('a',round(a,2))
print('b',round(b,2))
print('c',round(c,2))
print('d',round(d,2))
print('e',round(e,2))
print('f',round(f,2))
 
>>>  ('a', 3.05)
 ('b', 2.14)
 ('c', 3.45)
             ('d', 3.57)
 ('e', 2.56)
 ('f', 1.54)

三. 字符串常用方法。pycharm中操作

1》示例:

#/usr/bin/python
#coding=utf-8
#@Time   :2017/10/11 16:20
#@Auther :liuzhenchuan
#@File   :demon-str.py
s = 'hello'
print(dir(s))
print(s[0],s[1],s[2])

运行如下::

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
('h', 'e', 'l')

 

 

2》常用字符串方法

#find    查找
#replace    替换
#split     以什么为分割符
#join
#strip
#format
 
-----find 解析
rfind:从右边开始找
lfind:从左边开始找
s1 = 'dingeigliuzhenchuan08jo'
print(s1.find('liu'))
print(s1.find('abdliu'))

运行如下:

7   

-1   //找不到就会返回负1

 

 

----replace 解析。替换

s1 = 'dingeigliuzhenchuan08jo'
print(s1.replace('liu','hello'))

 

运行如下:

dingeighellozhenchuan08jo

 

---split解析,返回为列表。以什么为分割符

s2 = 'ttion:eottn:aacng'
print(s2.split(':'))

运行如下:

['ttion', 'eottn', 'aacng']

 

 

----join()   连接字符串数组。将字符串,元组,列表中的元素以指定的字符(分隔符)连接生成一个新的字符串

----os.path.join()   将多个路径组合后返回

s3 = '12:34:56'
print('word'.join(s3))
print('word'.join(s3.split(':')))

运行如下:

1word2word3word4word5word6

12word34word56

 

 

---strip() 方法用于移除字符串头尾指定的字符(默认为空格)

strip() 方法语法:

   str.strip([chars])

   chars:移除指定的字符串

rstrip():从右边开始去掉

lstrip():从左边开始去掉

#/usr/bin/python
#coding=utf-8
#@Time   :2017/10/12 21:12
#@Auther :liuzhenchuan
#@File   :字符串.py
s1 = '  11ab  11cd  11  '
print(s1)
print(s1.rstrip())
print(s1.lstrip())
print s1.strip('  11')  //移除两个空格和两个11的字符

运行如下:

  11ab  11cd  11    //打印s1字符串

  11ab  11cd  11    //从右边开始去掉空格,左边留有空格

11ab  11cd  11      //从左边开始去掉空格,右边留有空格

ab  11cd

 

 

---format()   字符串格式化
%s:代表的是字符串
%d:代表的是整数‘
%f:代表的是浮点型
 
#format  格式化字符串
name = 'liuzhenchuan'
print 'hello '+name
print 'hello %s' % name
print('hello {0}').format(name)

运行如下:

hello liuzhenchuan

hello liuzhenchuan

hello liuzhenchuan

 

 

多个变量的情况

#format  格式化字符串
name = 'liuzhenchuan'
age = 20
print('hello {0},my age is: {1}'.format(name,age))
print '{name}:{age}'.format(name='ajing',age='20')

运行如下:

hello liuzhenchuan,my age is: 20

ajing:20