没有合适的资源?快使用搜索试试~ 我知道了~
python 入门基础代码适合新手学习,里面包含了各种比较实用的函数介绍,以及应用例子。
资源推荐
资源详情
资源评论
python 入门基础代码
#查找 index 函数的帮助
help(str.index)
#for 循环和 break 语句
from math import sqrt
for i in range(2,101):
flag=1
k=int(sqrt(i))
for j in range(2,k 1):
if i%j==0:
flag=0
break
if(flag):
print i
#continue 语句,满足条件跳出 continue 后面的语句
sumA=0
i=1
while i<=5:
sumA =i
i =1
if i==3:
continue
print 'i=%d,sum=%d'%(i,sumA)
#循环中的 else 语句
k=5
for i in range(1,10):
if k==3:
break
else:
print i
#自定义函数/素材 1-100 间素数
from math import sqrt
def isprime(x):
if x==1:
return False
k=int(sqrt(x))
for j in range(2,k 1):
if x%j==0:
return False
return True
for i in range(2,101):
if isprime(i):
print idef f(x,y=True): #默认参数要放在参数列表的最后
'x and y both correct words or not'
if y:
print x,'and y both correct'
print x,'is Ok'
f(68)
f(68,False)
#传递函数
def addme2me(x):
return(x x)
def self(f,y):
print f(y)
self(addme2me,2.2)
#lambda 函数
my_add=lambda x,y:x y
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()
print p1,p2
f.close
f1=open(r'E:\360Downloads\companies.txt')
cname=f1.readlines()
for i in range(0,len(cname)):
cname[i]=str(i 1) '' cname[i]
f1.close
f2=open(r'E:\360Downloads\scompanies.txt','w')
f2.writelines(cname)
f2.close()
#网络数据获取
import urllib
r=urllib.urlopen('http://z.cn/')
html=r.read
#序列
#标准类型运算符
'apple'<'banana'
[1,2,3]!=[4,5,6]#值比较
atuple=('ba',126.4)
btuple=atuple
btuple is not atuple#对象身份比较
('86.40'<'122.64') and ('apple'<'banana')#布尔运算
剩余20页未读,继续阅读
资源评论
life—cn
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功