**

学习Python过程中的一些Tips

**
方法:

  • .split()
对字符进行分块处理。比如在输入的时候想要以空格进行分隔而不是以换行进行分隔
 	可以使用,默认分隔换行符
#spilt的基本语法:str.split(str="", num=string.count(str)).
a,b,c=map(int,input().spilt('&'))   """这里如果输入123&456&789,那么input()就是123&456&789
									input().spilt('&')就是一个被&分隔开的字符串数组为
									['123','456','789'];map的作用就是将这个数组进行一个
									映射,映射为三个int型数据;如果map函数第一个参数为
									str的话则映射为三个字符串,并且分别存储到a,b,c中"""
a=input().spilt('&')[1]				"""既然input().split('&')是一个列表那就可以对其进行直接
									赋值,输入123&456&789,input().spilt('&')[1]就是一个列
									表中下标为1的元素,a的值为456"""
a,b=map(int,input().spilt('&')[1:3])"""同样我们也可以映射这个列表的切片,如果输入还是上面那些,那
									得到的a,b分别为456,789"""
a,b=map(int,input().spilt('&',1)) """这个方法的第二个参数就是分隔几次,不填的话全部分隔。这里写的
									分隔两次,那么输入123&456&789得到的列表就是"""['123','456&789']

Tips:切片中a[1:3]中包含的元素是a[1],a[2]
像极了for(int i=1;i<3;i++)

  • .strip()
    语法:str.strip([chars]);
    去除str字符串两边的chars字符串
    它有兄弟方法,分别为rstrip()&lstrip()分别清除右边和左边
sd='12312300000000dfdsf1200000123123'
print(sd.strip('123'))     	#00000000dfdsf1200000
print(sd.lstrip('123'))		#00000000dfdsf1200000123123
print(sd.rstrip('123'))		#12312300000000dfdsf1200000
sd='00000000000000dfdsf1200000000000'
print(sd.strip('0'))		#dfdsf12
print(sd.lstrip('0'))		#dfdsf1200000000000
print(sd.rstrip('0'))		#00000000000000dfdsf12

函数:

语句:
一些问题的处理办法:

  • 向函数中传列表副本(禁止修改列表)
liebiao=[1,2,3,4,5]
fun(liebiao[:])  		#传递的只是列表的切片副本

- 向函数中传递任意数量的值对(作为字典)

def fun(frist,scend,**top):
 		file={}
 		file['frist']=frist
	  	file['scend']=scend
 		for key,value in top.items():
  		   file[key]=value
 	 	return file

		pro=fun('tang','wenxin',sex='nan',school='jlu')
		print(pro)
这个**top说明的是在函数里作为一个字典,而用户向调用函数中输入的值对就是作为这个字典的值对。
通常这个任意参数放在参数列表中的最后面。

- 向函数里传递任意数量的数据(作为元组)

def fun(frist,scend,*tops):
    file=[]
    file.append(frist)
    file.append(scend)
    for top in tops:
        file.append(top)
    return file
pro=fun('tang','wenxin','ddd','xxx',666)
print(pro)
这个**top说明的是在函数里作为一个元组,而用户向调用函数中输入的值对就是作为这个元组的值。
通常这个任意参数放在参数列表中的最后面。

容易犯的错误:

  • 关于print()输出函数的参数一致性
    print()函数中的参数必须全部一致
char='1'
num=1
print('number one is '+char)		//对的
print('number one is '+num)			//错的,因为字符串型和int型混了
print('number one is '+str(num))	//对了,对num进行类型转换
print(num)							//对了

自己的笔记,如有错误,欢迎指正,谢谢你
(未完)