测试Inbox

python部分基础知识

2017-03-20  本文已影响50人  五娃儿

一、py列表

1、python -V 查看本机py版本。ps:V是大写的

2、直接输入python 则进入到解析器,退出 :quit()

3、py列表 类似于数组,但是又有区别

4、列表更加强大的功能 单一列表

>>> cast = ['cleese','palin','jones','idle']

>>> print cast//输出列表中所有内容

['cleese', 'palin', 'jones', 'idle']

>>> print len(cast)//输出列表长度

4

>>> print cast[5]

Traceback (most recent call last):

File "", line 1, in

print cast[5]

IndexError: list index out of range//cast中的参数为5,实际长度为4,所以提示out of range

>>> print cast[3]//输出cast中的第三个元素的值

idle

5、python列表增加字段 append

>>> cast.append('dubailing')

>>> print cast

['cleese', 'palin', 'jones', 'idle', 'dubailing']

6、python列表中删除字段

>>> cast.pop('jones')

Traceback (most recent call last):

File "", line 1, in

cast.pop('jones')

TypeError: an integer is required//参数错误,不能指定删除某个字段

>>> cast.pop()

'dubailing'

>>> print cast

['cleese', 'palin', 'jones','idle']

7、将另一个表中的字段加到某表中

>>> cast.extend(['haha','nihao'])

>>> print cast

['cleese', 'palin', 'jones', 'haha', 'nihao']

8、python删除表中指定项

>>> cast.remove('palin')

>>> print cast

['cleese', 'jones', 'haha', 'nihao']

>>>

9、在某个指定位置插入某个字段//在列表元素位置为1的地方插入test字段,输出后,发现第二位为test,想插入到什么位置,只需修改元素位置号即可

>>> cast.insert(1,'test')

>>> print cast

['cleese', 'test', 'jones', 'haha', 'nihao']

10、python列表中可以存储任意类型的数据

11、python for 循环:

name = ['we','ssd','gg']

for i in name:

print i

//若想输出每个值,则可以使用 print name[0] 这种方法,but 若列表中有多个值,使用此方法就不美丽了,最好的就是使用for循环

12、>>> print cast

['cleese', '1975', 'test', '1979', 'jones', '1983', 'haha', 'nihao']

>>> while coutn < len(cast)://使用while循环 方式输出cast列表内容、但是处理迭代问题,使用for循环是最好的,while循环需要提供额外的控制

print cast[coutn]

coutn +=1

cleese

1975

test

1979

jones

1983

haha

nihao

13、python 对大小写是敏感滴哦,所以在变成过程中一定要注意

14、列表中嵌套列表

>>> movies = ['the holy grail',1975,'terry jones &terry gilliam',91,['graham chapman',['michel palin','john','terry','eric','jones']]]

>>> print movies

['the holy grail', 1975, 'terry jones &terry gilliam', 91, ['graham chapman', ['michel palin', 'john', 'terry', 'eric', 'jones']]]

>>> for i in movies:

print i

the holy grail

1975

terry jones &terry gilliam

91

['graham chapman', ['michel palin', 'john', 'terry', 'eric', 'jones']]

//如何将列表中的列表读取出来

使用到if....else语句,但是如何判断里面还有没有内嵌列表呢?那就要使用到python的一个内置函数

15、isinstance(要判断的对象,对比类型)  python的一个内置方法。(dir(__builtins__))

>>> print movies

['the holy grail', 1975, 'terry jones &terry gilliam', 91, ['graham chapman', ['michel palin', 'john', 'terry', 'eric', 'jones']]]

>>> isinstance(movies,list)//因为movies是一个列表所以返回结果为true

True

>>> isinstance(movies,int)因为movies是一个列表而对比类型为int,两者不一致,所以结果为false

False

>>>

代码如下:

>>> for eachname in movies:

if isinstance(eachname,list):

for nexteach in eachname:

if isinstance(nexteach,list):

for each in nexteach:

print each

else:

print nexteach

else:

print eachname

the holy grail

1975

terry jones &terry gilliam

91

graham chapman

michel palin

john

terry

eric

jones

//一层层的判断,实际处理过程是先处理内列表,在处理外列表,由内而外进行处理,但是这样子就引申出一个问题,那就是重复代码,如何解决呢?函数来帮忙

16、定义函数

>>> def print_lol(the_list):

for eachname in the_list:

if isinstance(eachname,list):

print_lol(eachname)

else:

print eachname

>>> print_lol(movies)

the holy grail

1975

terry jones &terry gilliam

91

graham chapman

michel palin

john

terry

eric

jones

//使用函数完美的解决了代码重复问题

二、函数模块

函数模块,则可以理解为一个含有.py的文件,模块中要含有注释代码,先介绍这是个什么模块,包含什么函数,其作用是什么,其次在标注这个函数的作用

函数模块的使用,可以通过import 方式导入,一般导入文件代码放在上方

在py编程过过程中可以将代码放置一行,并通过分隔符进行分割,但是这样子不利于阅读,建议每行只包含一行py代码

关于注释 可以使用三个成对的单引号或者双引号进行注释,当然也可使用#进行注释

BIF内置函数:range,返回一个迭代器,根据需要生成一个指定范围内的数字

>>> for num in range(4):

print num

0

1

2

3

三、文件异常处理

1、>>> import os//导入os模块

>>> os.getcwd()//当前工作目录是什么

'D:\\Python27'

>>> os.chdir('../headfistpython/chapter3')

>>> os.getcwd()

'D:\\headfistpython\\chapter3'

2、读取文件:

>>> data = open('sketch.txt')

>>> print data.readline()//读取一行

hello

>>> print data.read()//读取整个文件

haha

ahaha

>>> data.close()//关闭文档

文件处理-》打开文件-》读取文件内容-》处理后关闭文件

3、split(sep,[maxsplit])

4、find 通过find的返回值 判断

5、异常处理

对于会出现异常的情况,放入try/cath模块中-

其次增加更多的异常代码的处理

下面是对 一个解析的文件是否存在进行处理

>>> import os

>>> if os.path.exists('du.txt')://判断文件是否存在,如果存在则继续执行,不存在则提示文件丢失

data = open('du.txt')

for each in data:

if not each.find(':') == -1:

(role,num)= each.split(':',1)

print role

print num

print(role,num)

data.close()

else:

print ('The data file is missing')

man

is  this the tight room foe an atgumen?

('man', ' is  this the tight room foe an atgumen?\n')

other man

I've told you once.

('other man', " I've told you once.\n")

man

No you haven't!

('man', " No you haven't!\n")

other  man

Yes  I  have.

('other  man ', 'Yes  I  have.\n')

man

When?

('man', '  When?\n')

other man

now let's get one thing quites clear:i most definitely told you!

('other man', " now let's get one thing quites clear:i most definitely told you!\n")

man

no you didn't!

('man', " no you didn't!")

//修改打开文件名称

则提示

The data file is missing

使用try/catch

>>> try:

data = open('du.txt')

for each in data :

try:

(role,num)= each.split(':',1)

print role

print '++++++++++++++++++++++++++'

print num

print '++++++++++++++++++++++++'

print (role,num)

except:

pass

except:

print 'the is not exists'

the is not exists

>>>

//无论使用if else 还是try catch  均能达到处理异常的目的,但是使用try的成本低

strip()//剔除字符串中不想要的空白符

num = num.stript()

上一篇下一篇

猜你喜欢

热点阅读