1、函数定义:
2、可变长度参数:*args
写法1)def sumdata(a,*agrs): return (a,*agrs) print(sumdata(1,2,5,6,7)) 写法2)def sumdata(a,*agrs): return a,agrs print(sumdata(1,2,5,6,7))
3、字符串str1.index('c',3) 查找下标值,find('c',3)
4、字符串去掉空格 str1.strip()
5、srt1.replace('dd','a'),字符串替换
6、srt=‘13223322222’ 字符串以什么开头srt1.startswith('1'),以什么结尾srt2.endswith('55')
7、切片,listnew=srt2.split('2'),结果时一个list列表,当切割值不存在时,会把原字符串变成一个列表,长度是1
8、字符串格式化 a=2,b=4
写法1)print('%d+%d=%d'%(a,b,a+b)) %s 字符串,%d 整数,%f 浮点数。前面空位和后面值的数量要一样,某则报错
浮点数莫保留6未小数 print('请输入%f'%(10/3)),保留3位小数,print('请输入%.3f'%(10/3))。
'请输入%10.3f'%(10/3)左对齐,保留3位小数,'请输入%-10.3f'%(10/3)右对齐,保留3位小数
import request
from random import randint
#新增课程
def add_corse(self): #参数化
host=self.host
cookies=self.cookies
i=randint(0,99999)
nametime="数学"+str(i)
payload={
"action":"add_course",
"data" :
'''
{ "name":"%s",
"desc":"初中化学课程",
"display_idx":"4" }
'''%(nametime)
}
ret=requests.post(f'http://{host}/api/mgr/sq_mgr/',data=payload,cookies=cookies)
data=ret.json()
return data['retcode'],data['id']
写法2)
- 顺序取值:print('My name is {},my year is {} old .'.format('LUOBAOBAO','8','23')),
前面空格比后面值多,报错,后面值比前面空格多,正常运行,按顺序取值
- 下标取值法:'My name is {1},he name is {0} ,my year is {2} old .'.format('luo','zhou','8','23')
顺序取值和下标取值法不能昏混用,否则报错
- 对齐方式,左对齐10个空格 {:>10},居中{:^10},右对齐{:<10}
'My name is {:>10},he name is {:^10} ,my age is {:<10} old '.format('luo','zhou',23)
- 对齐方式和下标取值法并用
'My name is {1:>10},he name is {0:^10} ,my age is {2:<10} old '.format('luo','zhou',23)
- 用0补齐
My name is {:>010},he name is {:^10} ,my age is {:0<10} old '.format('luo','zhou',23)
写法3) f ’‘的写法,时写法2)的优化版,用法一样。
st1='ww' st2='ll' print(f'MY name is {st2},his name is {st2}') 补齐:print(f'MY name is {st2:>10},his name is {st2:<10}')
9、循环 while\for i in range(10)\
i=0 while i<11: i +=1 print(i,end='\t')
list2=['lww','22','33','ew','434'] for i in range(len(list2)): if i == 1: #break 此时循环结束 #continue 此时的值去掉,跳出此次循环 print(list2[i]) for one in list2: print(one)
九九乘法表:
def fordata():
for i in range(1,10):
for j in range(1,i+1):
data='%s * %s = %s'%(j,i,i*j)
print(data,end='\t')
print()
倒计时:
import time for i in range(10,0,-1): print(f'\r倒计时{i}',end='') #\r光标回到行首 time.sleep(1) else: print('\r倒计时结束')
循环语句中也可以带else语句,当循环中没有出现break,则循环结束时运行一次else语句,当循环中有break,则不执行else语句