1. 标识符。为什么Python 中不需要变量名和变量类型声明?
Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程。每个变量在内存中创建,都包括变量的标识,名称和数据这些信息。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。
2.标识符。为什么Python 中不需要声明函数类型?
Python中使用def关键字定义函数,函数包括函数名称和参数,不需要定义返回类型,Python能返回任何类型函数没有定义返回类型,实际上每一个函数都有返回值,默认为None(python的空值).
3.标识符。为什么应当避免在变量名的开始和和结尾使用双下划线?
在python中有特殊意义,表示系统定义名字,防止系统脚本出错
4.python 一行书写多个语句方式?
同一行书写多个语句使用(;)号隔开
5.语句。在Python 中可以将一个语句分成多行书写吗?
多行书写,在该行语句排头加入反斜杠(\)即可
6 变量赋值
(a)赋值语句 x, y, z = 1, 2, 3 会在 x、y、z 中分别赋什么值?
x=1 y=2 z=3
(b)执行z, x, y = y, z, x 后,x、y、z 中分别含有什么值?
z=2 x=3 y=1
7.标识符。下面哪些是Python 合法的标识符?如果不是,请说明理由!在合法的标识符中,哪些是关键字?
合法的标识符:第一个字符必须是字母或下划线(_);剩下的字符可以是字母和数字或下划线
int32、printf、_print、this、self、__name__、bool、true、type、thisIsAVar、R_U_Ready、Int、True、do、access是Python合法的标识符。
print、if、是Python合法的标识符且是关键字。
4.0XL、$aving$、0X40L、big-daddy、2hot2touch、thisIsn'tAVar、counter-1、-不是Python合法的标识符。
8.Python 代码。将脚本拷贝到您的文件系统中,然后修改它。可以添加注释,修改修改提示符(‘>’太单调了)等等,修改这些代码,使它看上去更舒服。
makeTextFile.py
#!/usr/bin/env python#-*-coding:utf-8-*-
importos
ls=os.linesep#导入os模块,获取当前系统行终止符#取得fname变量的文件名
whileTrue:
fname=raw_input("Enter filename:")try:ifos.path.exists(fname):print "ERROR:'%s' already exists" %fnameelse:break
except:print "*** file open error:",e#输入文件名,如果存在提示存在,不存在结束循环进入下个程序
all =[]print "\nEnter lines ('.' by itself to quit).\n"
#提示使用.quit
whileTrue:
entry=raw_input('>')if entry=='.':break
else:
all.append(entry)#循环,如果有.结束循环,其他情况添加.
fobj =open(fname,'w')
fobj.writelines(['%s%s'%(x,ls) for x inall])#输入输入进去的每一行
fobj.close()print 'DONE!
readTextFile.py
#!/usr/bin/env python
#-*-coding:utf-8-*-
fname=raw_input("Enter filename:")#提示输入需要阅读的文件名
try:
fobj=open(fname,'r')#在读的模式下打开文件
exceptIOError,e:print "*** file open error:",e#若出错提示错误
else:for eachLine infobj:printeachLine,
fobj.close()#打印出每一行,并关闭
9.移植。 如果你在不同类型的计算机系统中分别安装有Python, 检查一下,os.linesep 的值是否有不同。 记下操作系统的类型以及 linesep 的值。
RedHat
WindowsXP
10.异常。使用类似readTextFile.py 中异常处理的方法取代 readTextFile.py makeTextFile.py 中 对 os.path.exists()的调用。反过来, 用os.path.exists()取代readTextFile.py 中的异常处理方法。
#!/usr/bin/env python#-*-coding:utf-8-*-
'readTextFile.py -- read and display text file'
#fname变量获得文件名
importos
fname=raw_input('Enter filename:')ifos.path.exists(fname):#fobj对象读模式打开文件
fobj=open(fname,'r')for eachLine infobj:printeachLine.strip()
fobj.close()else:print "No this file"
11.字符串格式化 不再抑制 readTextFile.py 中 print 语句生成的 NEWLINE 字符,修改你的代码,在显示一行之前删除每行末尾的空白。这样,你就可以移除 print 语句末尾的逗号了。提示: 使用字符串对象的 strip()方法
#!/usr/bin/env python#-*-coding:utf-8-*-
'readTextFile.py -- read and display text file'
#fname变量获得文件名
importos
fname=raw_input('Enter filename:')ifos.path.exists(fname):#fobj对象读模式打开文件
fobj=open(fname,'r')for eachLine infobj:printeachLine.strip()
fobj.close()else:print "No this file"
12.合并源文件。将两段程序合并成一个,给它起一个你喜欢的名字,比方readNwriteTextFiles.py。让用户自己选择是创建还是显示一个文本文件。
#!/usr/bin/env python
#-*-coding:utf-8-*-
importos
ls=os.linesepwhileTrue:print """1.readTextFile
2.makeTextFile
3.quit
4.make the already Text File"""choose=raw_input("please find the choose")if choose =='1':print "You choose 1"
whileTrue:
fname=raw_input("enter the name:")ifos.path.exists(fname):print "the name is already exists!"
else:breakall=[]print"\n Enter lines('.' by itself to quit)\n"
whileTrue:
entry=raw_input(">")if entry =='.':break
else:
all.append(entry)
fobj=open(fname,'w')
fobj.writelines(['%s%s'%(x,ls) for x inall])
fobj.close()print "DONE!"
if choose=='2':print 'You choose 2'fname=raw_input("please choose the file:")
fobj=open(fname,'r')for eachLine infobj:printeachLine,
fobj.closeif choose=='3':break
if choose=='4':
fname=raw_input("please choose the file:")
all=[]whileTrue:
entry=raw_input(">")if entry=='.':break
else:
all.append(entry)
fobj=open(fname,'w')
fobj.writelines(['%s%s'%(x,ls) for x inall])
fobj.close()print "DONE!"