一、打印99乘法表:
# for i in range(1,10): #i=3 # for j in range(1,i+1): # print('%s*%s=%s ' %(i,j,i*j),end='') #i=2 j=2 # print()
二、打印金字塔:
''' max_level=5 * current_level=1 空格数=4 星号=1 *** current_level=2 空格数=3 星号=3 ***** current_level=3 空格数=2 星号=5 ******* current_level=4 空格数=1 星号=7 ********* current_level=5 空格数=0 星号=9 “”“ max_level=5 for current_level in range(1,max_level+1): # 先不换行打印打印空格 for x in range(max_level-current_level): print(' ',end='') # 再不换行打印* for y in range(2*current_level - 1): print('*',end='') print()
三、字符串类型的内置方法:
1 用途:记录描述性的状态,比如人的名字、地址、性别
2 定义方式:在"",'',""""""内包含一系列的字符
# msg='hello' #msg=str('hello') # res1=str(1) # res2=str([1,2,3]) # print(type(res1),type(res2)) # info="'xxx'"
3、 常用操作+内置的方法
优先掌握的操作:
1、按索引取值(正向取+反向取) :只能取
msg='hello world' print(msg[4]) print(msg[-1]) msg[3]='A' name='egon'
2、切片(顾头不顾尾,步长)就是从一个大的字符串中切分出一个全新的子字符串
# msg='hello world' # print(msg[0:5]) # print(msg) # 没有改变原值 # print(msg[0:5:1]) # print(msg[0:5]) # print(msg[0:5:2])
了解:
# print(msg[0:5:1]) # msg='hello world' # print(msg[5:0:-1]) # print(msg[5::-1]) # print(msg[-1::-1]) # print(msg[::-1])
3、长度len
msg='hello world' print(len(msg))
4、成员运算in和not in: 判断一个子字符串是否存在于一个大的字符串中
# print('alex' in 'alex is dsb') # print('dsb' in 'alex is dsb') # print('sb' in 'alex is dsb') # print('xxx' not in 'alex is dsb') # 推荐 # print(not 'xxx' in 'alex is dsb')
5、去掉字符串左右两边的字符strip,不管中间的
# user=' egon ' # user=' x egon ' # user="*******egon********" # user=" **+* */***egon* **-*****" # print(user.strip("* +/-")) # user=input('>>>: ').strip() # if user == "egon": # print('用户名正确')
6、切分split:针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值
# msg="root:123456:0:0::/root:/bin/bash" # res=msg.split(':') # print(res[1]) # cmd='dowload|a.txt|3333333' # cmd_name,filename,filesize=cmd.split('|')
7、循环
# msg='hello' # for item in msg: # print(item)
需要你掌握的:
1、strip,lstrip,rstrip
# print('*****egon*****'.lstrip('*')) # print('*****egon*****'.rstrip('*')) # print('*****egon*****'.strip('*'))
2、lower,upper
# msg='aABBBBb' # res=msg.lower() # print(res) # print(msg)
3、startswith,endswith
# msg='alex is dsb' # print(msg.startswith('alex')) # print(msg.endswith('sb')) # print(msg.endswith('b'))
4、format的三种玩法
# print('my name is %s my age is %s' %('egon',18)) # print('my name is {name} my age is {age}'.format(age=18,name='egon')) # 了解 # print('my name is {} my age is {}'.format(18,'egon')) # print('my name is {0} my age is {1}{1}'.format(18,'egon'))
5、split,rsplit
# msg='get|a.txt|333331231' # # print(msg.split('|',1)) # print(msg.split('|',1)) # print(msg.rsplit('|',1))
6、join
# msg='get|a.txt|333331231' # l=msg.split('|') # print(l) # # src_msg='|'.join(l) # print(src_msg)
7、replace
# msg='alex say i have one tesla,alex is alex hahaha' # print(msg.replace('alex','sb',1)) # print(msg)
8.isdigit # 判断字符串中包含的是否为纯数字
# print('10.1'.isdigit()) # age=input('>>: ').strip() # if age.isdigit(): # age=int(age) #int('asfdsadfsd') # if age > 30: # print('too big') # elif age < 30: # print('too small') # else: # print('you got it') # else: # print('必须输入数字')
三、列表的内置方法:
1、 用途:存放多个值,可以根据索引存取值
2 定义方式:在[ ]内用逗号分割开多个任意类型的值
3 常用操作+内置的方法
1、按索引存取值(正向存取+反向存取):即可存也可以取,只能根据已经存在的索引去改值,如果索引不存在直接报错
l=['egon','lxx','yxx'] # print(l[0]) # l[0]='EGON' # print(l) # print(l[-1]) # print(l[3]) # l[0]='EGON' # 只能根据已经存在的索引去改值 # l[3]='xxxxxxxx'
2、切片(顾头不顾尾,步长)
l=['egon','lxx','yxx',444,555,66666] # print(l[0:5]) # print(l[0:5:2]) # print(l[::-1])
3、长度
# l=['egon','lxx','yxx',444,555,66666,[1,2,3]] # print(len(l))
4、成员运算in和not in
# l=['egon','lxx','yxx',444,555,66666,[1,2,3]] # print('lxx' in l) # print(444 in l)
5、追加
l=['egon','lxx','yxx'] # l.append(44444) # l.append(55555) # print(l)
6、往指定索引前插入值
# l=['egon','lxx','yxx'] # l.insert(0,11111) # print(l) # l.insert(2,2222222) # print(l)
7、删除
# l=['egon','lxx','yxx'] # 单纯的删除值: # 方式1: # del l[1] # 通用的 # print(l) # 方式2: # res=l.remove('lxx') # 指定要删除的值,返回是None # print(l,res)
# 从列表中拿走一个值
# res=l.pop(-1) # 按照索引删除值(默认是从末尾删除),返回删除的那个值
# print(l,res)
8、循环
# l=['egon','lxx','yxx'] # for item in l: # print(item)
9、排序
# nums=[3,-1,9,8,11] # nums.sort(reverse=True) # print(nums) #从小到大 # nums.sort() #从大到小 # print(nums)
10、堆栈
# 队列:先进先出 # l=[] # # 入队 # l.append('first') # l.append('second') # l.append('third') # print(l) # # 出队 # print(l.pop(0)) # print(l.pop(0)) # print(l.pop(0)) # 堆栈:先进后出 l=[] # 入栈 l.append('first') l.append('second') l.append('third') # print(l) # 出栈 print(l.pop()) print(l.pop()) print(l.pop())
二:列表总结:
1 存多个值
2 有序
3 可变