python 学习

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
b" # 关闭转义
a2="a
b" 
a3=u"a
b" #unicode格式 
a4="you age %d, name %s"%(27, 'sjk')

open('c: mpa.txt', 'r') #wrong
open(r'c: mpa.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
b'

# 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() #读一行  后面有 
   strip()/ rstrip('
')
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), '
'])
f.writelines({'
', 'hello', ' world ', str(44)})
f.writelines(('hello', ' world ', str(44), '
'))
f.writelines('xxxxxxxxx')

f.close()
#写文件
f = open('e:song.txt', 'w')

head = "%10s%10s%10s 
"%('id', 'name', 'record')
item1 = "%10d%10s%10d 
"%(5556, 'name', 45545)
item2 = "%10d%10s%10d 
"%(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()
原文地址:https://www.cnblogs.com/siqi/p/4249224.html