首页 > 技术文章 > python学习笔记:

ct1104 2014-10-27 09:55 原文

(1)浅拷贝:

当 a = b时,实际上对a只是复制了一个b的引用,如果a的值改变了,b的值也会改变,这就是浅拷贝。

同样,对list切片也会引起浅拷贝

(2)排序:

python内置了两种排序方法,sort和sorted。

a.sort() 或者 b =  a.sort() 改变了a中的顺序

b = sorted(a) 不会改变a的顺序

 

(3)函数调用赋值顺序问题

1、一般情况下是一一对应赋值的

2、可以指定形参一一赋值

3、有预定义值得参数不能先于无预定义值参数被赋值

 

(4)time.sleep

在终端输入>taskkill /F /IM dwm.exe (dwn为对应的应用程序)

 

(5)循环语句:

for target in sequences:

  statements

其中sequences包含:list列表、tuple元组、strings字符串、files文件

 

 

(6)字符串操作

字符串前面加r:输出原始数据,关闭转义机制

字符串前面加u:unicode字符串编码

格式化字符串:"your age %d,sex %s, record %f"%(28,"Male",78.5):注意圆括号前面的%号

转义字符串:不能单独输出\n'

print s1,s2:在输出s1与s2之间会有空格

字符串可以切片:str1=‘asdkflsdfjkdslkf’  sub=str1[3:6],3、6分别为切割字符串的起点、终点、可以单独只写起点或终点,或起点、终点都不写,也不一定非得是整数,负数是从后往前。要求起点比终点小,不然会输出空行。s[i:j:step]:i起点、j终点、step方向

 

 

提取字符串中的字符:

i=0
s1=s1.strip()
print s1+'++++'
while 1:
j=s1.find(' ')
if j==-1:
print s1
break
str=s1[:j]
print str
s1=s1[j+1:]
s1=s1.strip()

 

if的条件表达式:True、False可用

 

 

文件操作:尽量完了以后就关闭文件。否则再次打开而之前有没有关的话,它会以只读方式打开

readline:只读一行; readlines:读多行,不包含空格;read:包含空格。如果没有关闭文件,使用不同的函数来读,则都是从当前已经读到的情况下再接着读;对于关闭文件在调用这些函数的话,会从头开始读。

file_obj=open("PythonApplication.py",'r')
print file_obj


s=file_obj.readline()
print 'l1',s
s=file_obj.readline()
print 'l2',s
#file_obj.close()

#file_obj=open("PythonApplication.py",'r')
#print file_obj
s=file_obj.readlines()
print 'all line',s
#file_obj.close()

#file_obj=open("PythonApplication.py",'r')
#print file_obj
s=file_obj.read()
print s
#file_obj.close()

file_obj.close()

 

 

测试过程中写的一点点东西:

 

file_obj=open("1.txt",'a+')
print file_obj


a='hello'+'\n'
b='world'+'\n'
file_obj.write(a+b)
file_obj.writelines(a)

head="%10s%10s%10s\n"%("ID","NAME","RECORD")
file_obj.write(head)
item="%10s%10s%10s\n"%(102041,"CT","60")
file_obj.write(item)
file_obj.close()

fd=open("1.txt",'r')
c=fd.readline()
while c!='':
print c.rstrip('\n')
c=fd.readline()

fd.close()

fd=open("1.txt",'r')
for c in fd:
print c.rstrip('\n')

fd.close

s='hello world panda you'
si=iter(s)
print si.next()
while si!=StopIteration:
print si.next()
print 'end'

///////////for与迭代器//////////////////

s=open("1.txt",'r')
si=iter(s)
print si.next()
while si!=StopIteration:
print si.next()
print 'file end'

 

////////////用于文件的读取直到抛出异常///////////////

 

st=[12,'abv','c',[1,2,3,4]]
print st

st1=st[1:3]
print st1

st2=st[3:1:-1]
print st2

st3=st[4:1:-1]
print st3

st4=st[1:0:-1]
print st4

st5=st[1:3:-1]
print st5

///////////测试列表的操作///////////////////

 

import webbrowser as web
import time
import os
i=0
while i<5:
web.open_new_tab('http://weibo.com/u/2960484191/home?wvr=5&c=spr_qdhz_bd_360jsllqcj_weibo_001')
i=i+1
time.sleep(1.0)

os.system('taskkill /F /IM dwm.exe')

 

 

count=0
i=0
while i<100:
count=count+i
i=i+1
print i,count

print count

 

print('Hello World')

a='sdfjkdslfjskdlxcvydusfdsfnxsdjsk'

#a.replace('s','S')

print(a.replace('s','S'))

import math
a=math.pi/3
print a
a=math.sin(a)
print pow(2,3)

import socket
baduip =socket.gethostbyname("www.baidu.com")
print baduip

def add(a,b):
print 'the result is :'
print a+b

print 'entry of fun'
add(1.0,2)
add('hello','world')
print 'leave the fun'

def add_res(a,b):
return a+b

print '\n'
print 'entry of fun'
print add_res(1.0,2)
print add_res('hello','world')
print 'leave the fun'

#?¨¤??¡À???¡À????¨¤??????¡¤?????
def add_mul(a,b):
x=a+b
y=a*b
z=a/b
return x,y,z

print '\n'
print 'entry of fun'
a,b,c=add_mul(1.0,2)

print a,b,c
print 'leave the fun'

def test(a,b,c=15):
print a
print b
print c
cc=a+b+c
return cc


print 'a,b,c-------\n'
tes=test(1,2)
print tes
print 'a,b,c-------\n'
tes=test(1,2,12)
print tes
print 'a,b,c-------\n'
tes=test(b=2,a=1)
print tes
print 'a,b,c-------\n'
tes=test(c=12,b=2,a=1)
print tes

print 'a,b,c-------\n'
tes=test(b=12,a=2)
print tes

count=int(raw_input("pelase input the value of count:"))

if count>10:
print 'larger'
else:
print 'less'


grade=int(raw_input("pelase input the value of count:"))

if grade>910:
print 'A'
else:
if grade>80:
print 'B'
else:
if grade>70:
print 'C'
else:
if grade>60:
print 'D'
else:
print 'no pass'
print 'end'


grade=int(raw_input("pelase input the value of count:"))

if grade>910:
print 'lev_A'
elif grade>80:
print 'lev_B'
elif grade>70:
print 'lev_C'
elif grade>60:
print 'lev_D'
else:
print 'no pass'
print 'end'


if 0:
print 'false'
else:
print 'true'

if False:
print 'true'
else:
print 'false'

推荐阅读