您的位置:首页 > 编程语言 > Python开发

Python学习笔记

2014-02-21 10:40 417 查看
输入 str1 = raw_input("Plz input a string:")

输出 print(str1)
print(5)
print(12.5)
print('H')
print('wwww' )
print x , y , z

help(raw_input)

变量:可变化数据的程序标识符,指向某个数据单元,通过赋值可以改变其指向(而不是上一个单元里的数据),指向内存单元的变,类似C中的指针。不分数据类型,指向什么样的就算是什么类型的变量。

id(x)打出x指向的单元的地址

字符函数库:help(str)
s = "Python"
s.islower()
help(str.islower())
str = 'http://www.jeapedu.com/index.php?m=content&c=index&a=show&catid=16&id=65'
mainBegin = str.find(r':')
mainEnd = str.find(r'?')
main = str[mainBegin+1:mainEnd]
#取得65
lastTwo = str[-2:]
s1 = 'www'
s2 = 'zhipu'
s3 = 'cn'
s = s1+'.'+s2+'.'+s3
i = 22
s = s1 + '.' + str(i) + s3 str(i)可以将整形变成字符串 +可以连接字符串

长度 n = len(str1)
age = raw_input('your age: ') 这样输入的都先默认为string类型的
print age[0] 可以使用下标访问
your age: 22
type(age)
<type 'str'> age是string类型的
age = int(age) 将string类型的age转换成int类型的
f1 = float('12.35') 将字符串12.35转换成float类型
s7 = r'aa \n bb' r说明后面的字符串是原串,不把 \n当成转义字符来处理
格式化字符串 "your age %d,sex %s,record %f"%(28,"Male",78.5)
"age %d recore %.2f"%(28,98.8907)
*字符串的重复
li1 = [1] * 5 重复五次
li2 = [1,'3',67.9]*3 []中内容重复三次
s1 = 'a'*5
切片str1 = 'abcdefghijk' sub = str1[3:8] sub = str1[:8] 起点从0开始,可以不写 sub = str1[3:]默认终点为最后 sub = str1[-2:] 从倒数第二位到最后
字符串逆序 str1 = "www.baidu.com" sub = str1[-1::-1] 最后面的-1表示逆序
int(s) 将字符串转换成整形
ord(s) 将字符串转换成asc码
chr(ASCII) 将ASCII转换成字符串
strip(s) 去除首尾空格 lstrip() rstrip()
s = 'hello my leetcode'
lis = s.split() 以空格作为分割来拆分字符串
s = "aa.bb.cc.dd.ee"
liDot = s.split('.') 以 . 作为分隔符 对字符串进行拆分 返回值为list
替换s = "www.baidu.com"
s2 = s.replace('b','B')

数学函数库
import math
val = math.sin(math.pi / 6)
3**7 就是3的7次方

操作系统库函数
import os
currentDir = os.getcwd() 获得当前的工作路径

网络函数库
import socket
baiduIP = socket.gethostbyname('www.baidu.com')

第三方函数库 httplib2
import urllib
import webbrowser as web
url = 'http://www.163.com'
content = urllib.urlopen(url).read()
print content
open('163.com.html','w').write(content) 打开本地的一个文件,如果没有此文件,则新建之
webbrowser.open_new_tab('163.com.html') 用默认浏览器打开本地文件,新建个tab
webbrowser.open_new_tab('www.renren.com') 打开一个网页,根据网址
模块名.函数名( 参数 )

自定义函数
def function_name(parameters):
statement1
statement2
没有返回值类型
eg:
def test_function(val1,val2):
print val1
print val2
调用:
test_function(12,"hello")
带返回值的
def test_c (n1,n2):
print n1,
print n2
n = n1 + n2
return n
调用
sum = test_c(101,102)
print 'sum = ', sum
返回多个值
def test_d (n1,n2):
n = n1+n2
m = n1* n2
r = n1**n2
p = n2 - n2
return n,m,r,p
调用:
sum1,multi,pow1,minus = test_d(2,10)
默认形参值
def test_e(n1,n2 = 3):
print n1,n2
n = n1 + n2
return n
调用
test_e(4)可以只传入一个值进去
test_e(4,5)

爬虫程序
#<a title="地震思考录" target="_blank" href="http://blog.asdfjsdalkgsdf.html" name="asdf">

#coding:utf-8

import urllib

import time

url = [' ']*50

con = urllib.urlopen('http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html').read()

i = 0

title = con.find(r'<a title=')

href = con.find(r'href=',title)

html = con.find(r'.html',href)

while title!=-1 and href!=-1 and html!=-1 and i<50

url[i] = con[href +6 :html +5]

print url[i]

title = con.find(r'<a title=',html)

href = con.find(r'href=',title)

html = con.find(r'.html',href)

i = i+1

else:

print 'find end'

j = 0

while j<50:

content = urllib.urlopen(url[j]).read()

open(r'hanhan/' +url[j][-26] , 'w+').write(content) 名字 且以w+方式打开

print 'downloading',url[j]

j = j+1

time.sleep(2)

else:

print 'download finished!'

函数形参赋值问题
def test_e(n1 , n2 ,n3 = 15) 有默认值的形参写在最后面
print n1,
print n2,
print n3,
n = n1 +n2 + n3
return n

s = test_e(4,1)
print s
s = test_e(4,1,12)
s = test_e(4,n2 = 1)
s = test_e(4, n2 = 1,n3 = 12)
s = test_e(n2 = 1,n1 =4)
s = test_e(n3 = 1, 2 ,4)这样写不行,因为n3是有预设值的形参,它放在前面并且后面的2 和4没有指定,这时不可以

if语句
count = int (raw_input("Plz input your score:") ) raw_input的返回类型为字符串
if count >80:
print 'large than eighty!'
else:
print 'less than eighty'

sex = raw_input("Plz input your sex:")
print 'sex',sex
if sex == "male":
print 'hello gentleman'
else:
print 'hello lady'

count = int (raw_input("Plz input your score:") )
if count >= 90:
print 'a'
elif count>= 80:
print 'b'
elif count>= 70:
print 'c'
elif count>= 60:
print 'd'
else
print 'no pass'

and or not 运算符 与或非

while语句
i = 1 初始化变量
sum = 0
while i<=100:
sum = sum+i
i = i+1 控制循环次数变量修正
else
print 'sum = ' , sum

刷微博点击次数
import webbrowser as web
import time
import os
import random
i = 0
j = 0
while j <= 5:
web.open_new_tab('网址')
i = i + 1
time.sleep(0.8)
if i == 10
os.sys( 'taskkill /F /IM chrome.exe' ) 关闭浏览器
i = 0
j = j+1
else:
print 'end'

TASKKILL /F /M QQ.exe 操作系统杀死某进程

count = random.randint(20,40) 产生20到40之间的一个随机数

for循环
for target in sequences:
statements
sequences可以是list tuple strings files

s1 = 'www.baidu.com'
i = 0
for c in s1:
print format(i,'2d') ,c 格式输出 i 占两位
print c
i = i +1
else:
print 'out for '

列表类型
list1 = [ 1, 3, 4, 9 ,'xjz' , 12.5] 一堆数据用 , 间隔,用[] 扩起来,可以是同一类型,也可以不同类型的
for bal in list1:
print val
list2 = list(s1)
for val in list2:
print val
else:
print 'out of list2'
也可以
for c in 'www.baidu.com':
print c

listNum = range(1,101,2) 1到100,2为步长的一个数列,使用range函数来表示的

元组 数据项只可读,不可写
tup = (1,2,3,4,5)
for t in tup:
print t

文件的读取
for var in files
for n in open('a.txt','r').readlines():
print n
文件的拷贝
for r in open('fore.txt','r').readlines:
open('temp.txt','a+').write(r)
else:
print 'copy end'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: