python入门基础代码Word文档下载推荐.docx

上传人:b****3 文档编号:14271693 上传时间:2022-10-21 格式:DOCX 页数:31 大小:20.82KB
下载 相关 举报
python入门基础代码Word文档下载推荐.docx_第1页
第1页 / 共31页
python入门基础代码Word文档下载推荐.docx_第2页
第2页 / 共31页
python入门基础代码Word文档下载推荐.docx_第3页
第3页 / 共31页
python入门基础代码Word文档下载推荐.docx_第4页
第4页 / 共31页
python入门基础代码Word文档下载推荐.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

python入门基础代码Word文档下载推荐.docx

《python入门基础代码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《python入门基础代码Word文档下载推荐.docx(31页珍藏版)》请在冰豆网上搜索。

python入门基础代码Word文档下载推荐.docx

k=5

foriinrange(1,10):

ifk==3:

else:

#自定义函数/素材1-100间素数

defisprime(x):

ifx==1:

returnFalse

k=int(sqrt(x))

ifx%j==0:

returnTrue

ifisprime(i):

printideff(x,y=True):

#默认参数要放在参数列表的最后

'

xandybothcorrectwordsornot'

ify:

printx,'

andybothcorrect'

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()

r'

)#r代表读出

p1=f.read(5)#5代表读出的字节数

p2=f.read()

printp1,p2

f.close

f1=open(r'

\360Downloads\companies.txt'

cname=f1.readlines()

foriinrange(0,len(cname)):

cname[i]=str(i1)'

cname[i]

f1.close

f2=open(r'

\360Downloads\scompanies.txt'

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('

)#布尔运算

#序列类型运算符

week=['

mondy'

tuesday'

wednesday'

thursday'

firday'

sat

uday'

sunday'

]

print

week[1],week[-2],'

\n'

week[1:

4],'

week[:

6],'

:

-1]#

序列值的序号是从0开始到N-1或者从-1到N

*3

in('

theboeingcompany'

#序列类型转换工厂函数

list('

)#将字符串转成列表

tuple('

)#将字符串转成元组#字符串

s1='

hello

a'

b'

world!

s1#三引号可以保持里面字符串的原貌

s2=r'

d:

\python\a.py'

s2

ccode=['

axp'

cat'

csco'

cvx'

cprice=['

99.44'

23.78'

115.91'

foriinrange(5):

%d%8s:

%8s'

%(i,ccode[i],cprice[i])#%8s指的占8个

字符

iget%d%%!

%32#两个%%会留下一个

astr='

whatdoyouthinkofthissaying'

nopain,nogain'

?

tempstr=astr.split('

\'

)[1]#\'

为转义字符

iftempstr.istitle():

itistitleformat'

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'

weekend=['

saturday'

week.extend(weekend)#extend合并函数

fori,jinenumerate(week):

#enumerate函数产生从0开始

的序列

printi1,jnumlist=[3,11,5,8,16,1]

numlist.sort(reverse=True)#按逆序排列

numlist

fruitlist=['

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=(['

1],2,3)

btruple[0][1]

2014,#创建元组2014

alist=['

atruple=('

alist[1]='

alibaba'

atruple[1]='

#元组是不可变的,列表是可变的

alist=[3,5,2,4]

alist.sort()#改变原来列表

alist

sorted(alist)#sorted只是新生成一个列表的副本,原来的列

表并没有改变

#但元组就只能使用sorted,不能用sort,因为不能改变元

atruple=(3,5,2,4)

atruple.sort#会报错

deffunc(args1,args2='

world'

):

#

printargs1,args2

func('

hello'

func(args2='

args1='

deffunc(args1,*args):

printargs1

printargs

元组作为函数形式的参数

hello,'

wangdachui'

liuyuan'

linlin'

#返回值类型为元组的函数enumerate()、coerce()

#字典

ainfo={'

3000,'

niuyun'

2000,'

3000}#

直接

创建字典

info=[('

3000),('

2000),('

3000)]

binfo=dict(info)#

利用

dict

函数创建字典

cinfo=dict([['

3000],['

2000],['

3000

]])

dinfo=dict(wangdachui=3000,niuyun=2000,linlin=3000)

adict={}.fromkeys(('

),3000)

#fromkeys函数将所有的keys初始值设置为3000

adict

plist=[('

americanexpress'

),('

theboeing

company'

caterpillarinc.'

)]

alist[]

blist[]

foriinrange

(2):

astr=plist[i][0]

bstr=plist[i][2]

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1