设为首页 收藏本站
查看: 1001|回复: 0

[经验分享] python 学习

[复制链接]

尚未签到

发表于 2015-12-1 07:11:29 | 显示全部楼层 |阅读模式
  
  zend 安装python环境 http://pydev.org/updates/
  



import urllib
import webbrowser
url ="http://www.baidu.com"
content = urllib.urlopen(url).read()
#print content
open('e:/song.htm', 'w').write(content)
webbrowser.open_new_tab('http://www.qq.com')
  
  help(str)
  id()
  len()
  import time
  



def test():
print 'a'
print 'a'
  



#返回多个值,多个变量来接收
def test(a,b):
print a
return a, b
print test(1,2)
a1,b1 = test(2,3);
print a1
print b1
  



def test(a,b):
return a,b

aa = test(2,3);
print aa[0],aa[1] #元组
  



def test(a = 23,b): #无预定义值的参数必须最前面,否则会报错
return a,b
aa = test(2,3)
  



#coding: utf-8
def test(a1, a2, a3=3):
print a1, a2, a3
test(1,2) # 1 2 3
test(1,2,4) #1 2 4
test(1,a2=2,a3=2) # 1 2 2 有名的必须写在无名的后面, 以名称为准
#test(1,a2=2, 4) #non-keyword arg after keyword arg
test(1,a3=4, a2=2)
  



if 0:
print 'true'
else:
print 'false'
a=5
if a>'ac':
print 'yes1'
else     :
print 'no'
print 'always'
  



#coding: utf-8
# elif 用法
count = 70
if count>80:
print 'good'
elif count>60:
print 'middle'
else:
print 'bad'
  



if not 0: #不支持符号 &&
print 'yes'
else:
print 'false'
  



# while condition:
#     statment
# else:
#     statment #else部分可省略

i=0
while True:
print i
i+=1
if i>10:
break
else:
print 'out-else'
print 'out'  
  



#coding: utf-8
import webbrowser
import os
import time
while True:
i=0
while i<3:
i += 1
webbrowser.open_new_tab('http://www.baidu.com')
time.sleep(1)
else:   
os.sys('TASKKILL /F /IM explorer.exe')

  



#coding: utf-8
#
# for target in sequences(序列)<list, tuple, strings, files>:
#     staements

str="abcdefg"
i=0
for s in str:
#     print format(i, '2d'),s
i+=1
else:
print 'out for'
# list 类型中的元素可以不同 eg:[1,2,'a', 3]   
for s in range(1,15):
print s;   

for s in list("abc"):
print s
for line in open('e:/song.txt', 'r').readlines():
print line
  



a1=r"a\nb" # 关闭转义
a2="a\nb"
a3=u"a\nb" #unicode格式
a4="you age %d, name %s"%(27, 'sjk')

  open('c:\tmp\a.txt', 'r') #wrong
open(r'c:\tmp\a.txt', 'r') #right
open('c:\\tmp\\a.txt', 'r') #right
  



s2='ab'*5 #重复
print s2 #ababababab

s3='abcdefg' #切片  s[i:j:step]
print s3[1:3] #bc
print s3[1:-1] #
print s3[-1]
print s3[-1:-4:-1] #gfe 倒着走
print s3[-1::-1] #逆序
  



str1 = 'ab '
str2 = 'a\r\nb'
# print str1.isalnum() #数字+字母
# print "4455".isalpha() #字母
# print "4455".isdigit() #数字  islower isupper
# print ' '.isspace()
# print "abc".upper()
# print "abB".lower() #strip lstrip rstrip
# print "abB".startswith('ac') #以字串开头的 endwhith
# print "abB".find('B') #从左往右查 rfind
# print "abB".replace('a', 'A')
  



#读文件

f = open('e:\song.txt', 'r')
# content = f.read() #读取全部
line = f.readline() #读一行  后面有 \n   strip()/ rstrip('\n')
lines = f.readlines() #读多行
# print content
print line
print 'x'
# print lines[0].decode('utf-8')

f.close()
  



#写文件
f = open('e:\song.txt', 'w')
f.write('hello ')
f.write('world !')
f.writelines(['hello', ' world ', str(44), '\n'])
f.writelines({'\n', 'hello', ' world ', str(44)})
f.writelines(('hello', ' world ', str(44), '\n'))
f.writelines('xxxxxxxxx')
f.close()
  



#写文件
f = open('e:\song.txt', 'w')
head = "%10s%10s%10s \n"%('id', 'name', 'record')
item1 = "%10d%10s%10d \n"%(5556, 'name', 45545)
item2 = "%10d%10s%10d \n"%(5556, 'name', 45545)
f.write(head)
f.write(item1)
f.write(item2)
f.close()
  



mylist = 'a|b'.split('|')
print "".join(mylist)
  



#文件遍历
f = open('e:/song.txt', 'r')
line = f.readline()
while line != '':
print line.strip()
line = f.readline()
f = open('e:/song.txt', 'r')
for line in f:
print line.strip()
f.close()  
  



#迭代器 list string tuple set dict file都是可迭代的,都可以for来循环
str = 'abcdefg'
iterator =  iter(str);
print iterator.next()
print iterator.next()

f = open("e:/song.txt", 'r')
for line in f:
print line.strip()
  



#coding: utf-8
"""
list:
可修改
可动态增减长度不固定
里面的类型可以不相同
两个list可以连接(+),来形成新的list
数组(PyNum)
长度固定
{} 之间
数据类型必须相同
不可以连接(+)

"""
mylist = [1,2,3,4,4]
# print mylist[2]
# mylist[0] = 5 #修改某个元素
# print mylist[0]
#
# print mylist[0:2] #切片
#
# for s in mylist:
#     print s

# mylist1 = [1,2,3]
# mylist2 = [4,5,6]
#  
# mylist3 = mylist1+mylist2 #mylist.extend(mylist2)
# print mylist3
#  
# mylist1.extend(mylist2)
# print mylist1
# mylist3 = mylist1*2
# print mylist3
# print mylist
# mylist.append(5)
# print mylist
# mylist.insert(0,-1)
# print mylist
# print list('abcd') #字符串转换list
# print mylist.index(2)

mylist.count(2) #统计值出现的次数

mylist.remove(2) #移除第一次遇到的某一个值
del mylist[2]
mylist.__add__([5,6,4])
print mylist.pop(5)
mylist.reverse() #逆序 [::-1]






#输出没有换行的
sys.stdout.write(str2);
  



#from 模块名 import 函数名
#函数名
from sys import stdout
stdout.write('xx')
#import 模块名
import sys
sys.stdout.write(str)

  
import sys.path
from os import *
import string, re, time, random, socket, thread


  



dict = {'song':'s', 'kang':'k'}
print dict.get('song')
dict['song'] = 44
print dict.get('song')
dict.keys()
dict.values()
dict.copy()
dict.popitem()
  



for a, b in((1,2),(3,4)):
print a,b


#变长参数
def test(*args):
print args; #(1, 2, 3) 元组
print test(1,2,3)

def test2(**args):
print args; #{'a': 2, 'c': 4, 'b': 3} 字典

test2(a=2,b=3,c=4)
  



#对象 一切皆对象
class song:
def __init__(self, name, age):
self.name=name;
self.age=age
def say(self):
print 'say'

s = song(1,2)
print s.__class__ #__main__.song
print type(s) #<type 'instance'>

s.say()
  



#coding: utf-8
import httplib
http = httplib.HTTPConnection('www.baidu.com', 80)
http.request('GET', '/')
print http.getresponse().read()
http.close()

#更简单的库
import urllib2
opener = urllib2.build_opener();
f = opener.open('http://www.baidu.com')
print f.read()
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-145565-1-1.html 上篇帖子: 小 200 行 Python 代码做了一个换脸程序 下篇帖子: Python学习一入门
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表