python入门基础代码.docx
- 文档编号:1354754
- 上传时间:2022-10-21
- 格式:DOCX
- 页数:31
- 大小:20.82KB
python入门基础代码.docx
《python入门基础代码.docx》由会员分享,可在线阅读,更多相关《python入门基础代码.docx(31页珍藏版)》请在冰豆网上搜索。
python入门基础代码
python入门基础代码
#查找index函数的帮助
help(str.index)
#for循环和break语句
frommathimportsqrt
foriinrange(2,101):
flag=1
k=int(sqrt(i))
forjinrange(2,k1):
ifi%j==0:
flag=0
break
if(flag):
printi
#continue语句,满足条件跳出continue后面的语句
sumA=0
i=1
whilei<=5:
sumA=i
i=1
ifi==3:
continue
print'i=%d,sum=%d'%(i,sumA)
#循环中的else语句
k=5
foriinrange(1,10):
ifk==3:
break
else:
printi
#自定义函数/素材1-100间素数
frommathimportsqrt
defisprime(x):
ifx==1:
returnFalse
k=int(sqrt(x))
forjinrange(2,k1):
ifx%j==0:
returnFalse
returnTrue
foriinrange(2,101):
ifisprime(i):
printideff(x,y=True):
#默认参数要放在参数列表的最后
'xandybothcorrectwordsornot'
ify:
printx,'andybothcorrect'
printx,'isOk'
f(68)
f(68,False)
#传递函数
defaddme2me(x):
return(xx)
defself(f,y):
printf(y)
self(addme2me,2.2)
#lambda函数
my_add=lambdax,y:
xy
my_add(5,6)
#数据获取与表示
f=open(r'E:
\360Downloads\firstpro.txt','w')#新建一个文件
f.write('hello,world')#f为对象,write写入文件
f.close()
f=open(r'E:
\360Downloads\firstpro.txt','r')#r代表读出
p1=f.read(5)#5代表读出的字节数
p2=f.read()
printp1,p2
f.close
f1=open(r'E:
\360Downloads\companies.txt')
cname=f1.readlines()
foriinrange(0,len(cname)):
cname[i]=str(i1)''cname[i]
f1.close
f2=open(r'E:
\360Downloads\scompanies.txt','w')
f2.writelines(cname)
f2.close()
#网络数据获取
importurllib
r=urllib.urlopen('
html=r.read
#序列
#标准类型运算符
'apple'<'banana'
[1,2,3]!
=[4,5,6]#值比较
atuple=('ba',126.4)
btuple=atuple
btupleisnotatuple#对象身份比较
('86.40'<'122.64')and('apple'<'banana')#布尔运算
#序列类型运算符
week=['mondy','tuesday','wednesday','thursday','firday','sat
uday','sunday']
week[1],week[-2],'\n',week[1:
4],'\n',week[:
6],'\n',week[:
:
-1]#
序列值的序号是从0开始到N-1或者从-1到N
'apple'*3
'ba'in('ba','theboeingcompany')
#序列类型转换工厂函数
list('hello,world')#将字符串转成列表
tuple('hello,world')#将字符串转成元组#字符串
s1='''hello
'a'
''b''
world!
'''
s1#三引号可以保持里面字符串的原貌
s2=r'd:
\python\a.py'
s2
ccode=['axp','ba','cat','csco','cvx']
cprice=['86.40','122.64','99.44','23.78','115.91']
foriinrange(5):
print'%d%8s:
%8s'%(i,ccode[i],cprice[i])#%8s指的占8个
字符
print'iget%d%%!
'%32#两个%%会留下一个
astr='whatdoyouthinkofthissaying'nopain,nogain'?
'
tempstr=astr.split('\'')[1]#\'为转义字符
iftempstr.istitle():
print'itistitleformat'
else:
print'itisnottitleformat'
八进制数000代表的
字符\t代表横向制表符
printastr#列表
jscores=[9,9,8.5,10,7,8,8,9,8,10]
ascore=9
jscores.sort()
jscores.pop()#去掉最高分
jscores.pop(0)#去掉最低分
jscores.append(ascore)#合并
avescore=sum(jscores)/len(jscores)
avescoreweek=['monday','tuesday','wednesday','thursday',
'firday']
weekend=['saturday','sunday']
week.extend(weekend)#extend合并函数
fori,jinenumerate(week):
#enumerate函数产生从0开始
的序列
printi1,jnumlist=[3,11,5,8,16,1]
numlist.sort(reverse=True)#按逆序排列
numlist
fruitlist=['apple','banana','pear','lemon','avocado']
fruitlist.sort(key=len)
fruitlist#按字符长度排序
#列表解析
[x**2forxinrange(10)]
[x**2forxinrange(10)ifx**2<50]
[(x1,y1)forxinrange
(2)foryinrange
(2)]
#元组
atruple=(1,2,3)
atruple
atruple[1:
]
len(atruple)
btruple=(['monday',1],2,3)
btruple[0][1]
2014,#创建元组2014
alist=['axp','ba','cat']
atruple=('axp','ba','cat')
alist[1]='alibaba'
atruple[1]='alibaba'#元组是不可变的,列表是可变的
alist=[3,5,2,4]
alist.sort()#改变原来列表
alist
alist=[3,5,2,4]
sorted(alist)#sorted只是新生成一个列表的副本,原来的列
表并没有改变
alist
#但元组就只能使用sorted,不能用sort,因为不能改变元
组
atruple=(3,5,2,4)
atruple.sort#会报错
deffunc(args1,args2='world'):
#
printargs1,args2
func('hello',)
func(args2='apple',args1='hello')
deffunc(args1,*args):
printargs1
printargs
元组作为函数形式的参数
func('hello,','wangdachui','liuyuan','linlin')
#返回值类型为元组的函数enumerate()、coerce()
#字典
ainfo={'wangdachui':
3000,'niuyun':
2000,'linlin':
3000}#
直接
创建字典
info=[('wangdachui',3000),('niuyun',2000),('linlin',3000)]
binfo=dict(info)#
利用
dict
函数创建字典
cinfo=dict([['wangdachui',3000],['niuyun',2000],['linlin',3000
]])
dinfo=dict(wangdachui=3000,niuyun=2000,linlin=3000)
adict={}.fromkeys(('wangdachui','niuyun','linlin'),3000)
#fromkeys函数将所有的keys初始值设置为3000
adict
plist=[('axp','americanexpress','86.40'),('ba','theboeing
company','122.64'),('cat','caterpillarinc.','99.44')]
alist[]
blist[]
foriinrange
(2):
astr=plist[i][0]
bstr=plist[i][2]
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 入门 基础 代码