for循环与内置方法详解

for循环与内置方法详解

'''

for循环与内置方法详解

'''

# 循环:重复(按照某种规律的)做一件事情

# lt = [1, 2, 3, 4]
#
# ind = 0
#
# while True:
# print(lt[ind])
#
# ind += 1 # dic = {'a': 1, 'b': 2, 'c': 3} # while循环:可以循环一切事物
# for循环:提供了一种手段,不依赖索引取值 #
# for i in dic:
# print(i,dic[i]) '''
for 变量名(会拿到容器类元素的每一个值,没有了就结束循环) in 容器类元素:
print(变量名)
'''
# lt = [1, 2, 3, 4]
#
# for i in lt:
# print(i) # dic = {'a': 1, 'b': 2, 'c': 3}
#
# count = 0
# for i in dic: # 对于字典,for循环只能拿到key
# # print(i, dic[i])
# print(count)
# count += 1 # print(list(range(10))) # for i in range(50,101): # 顾头不顾尾
# print(i) # for i in range(50,101,3): # 顾头不顾尾,2表示步长
# print(i) # for + break
# for i in range(50,101,3): # 顾头不顾尾,2表示步长
# if i == 53:
# break # 中断循环
# print(i) # for + continue
# for i in range(50,101,3): # 顾头不顾尾,2表示步长
# if i == 53:
# continue # 跳出本次循环,不执行下面的代码
# print(i) # for + else(仅作了解):for循环不被break终止就执行else下的代码,否则不执行
# for i in range(50,101,3):
# if i == 1000:
# break
# print(i)
# else:u
# print('如果没有被break终止我就打印') '''
Loading....
''' # print(1,end=' ') # end是打印1后额外做的操作
# print(2,end=' ')
# print(3,end=' ')
#
# import time
# print(1)
# time.sleep(5)
# print(2) print('Loading',end='')
import time
for i in range(4): print('.',end='')
time.sleep(0.2)
# 整型
# 浮点型
# 字符串
# 列表
# 元组
# 字典
# 集合
# 布尔值 # lt = [1, 2, 34, 54, 5, 45, 0, 1000]
# lt.sort(reverse=True) # 死记硬背
# print(lt) # 整型 int # 1. 作用:年龄/id # 2. 定义方式
x = 10 # 3. 使用方法: + - * / % // ** < <= > >= == != # 4. 有序or无序(有索引的就有序,无索引的就无序)
# 压根就没有有序无序一说 # 5. 可变or不可变(值变id不变叫可变,值变id变叫不可变):整型不可变
# x = 10
# print(id(x))
# x += 1
# print(id(x)) # lt = [1, 2, 3]
# print(id(lt))
# lt.append(4)
# print(lt)
# print(id(lt)) # 浮点型float # 1. 作用: 薪资 # 2. 定义方式
x = 10.1 # 3. 使用方法: + - * / % // ** > >= < <= == != # 4. 有序or无序: 压根没有有序无序一说 # 5. 可变or不可变:不可变
x = 10.1
print(id(x)
)
x +=1
print(id(x))
# 字符串str
# 1. 作用:姓名 # 2. 定义方式
# 单引号/双引号/三单引号/三双引号 # 3. 使用方法 ## 优先掌握(今天就得掌握)
# 1. 索引
# 左 右
s = 'nick handsome'
# 01234567
# -2-1
print(s[1]) # 2. 索引切片
print(s[0:4]) # 顾头不顾尾
print(s[0:4:2]) # 2表示步长,隔一个取一个
print(1, s[4:0:-1]) # +从左到右,-表示从右到左
print(1, s[2:]) # 左边的不写取到最左边,右边的不写取到最右边 # 3. for循环
for i in s:
print(4, i) # 4. 成员运算
print('nick' in s)
print('nick1' not in s) # True # 5. strip() # 默认去除两端空格,可以指定去除的字符,可以指定多个字符同时去掉
s1 = 'a nick ******'
# print(s1.strip())
print(s1.strip(' kc*')) # 可以乱序,但是不能没有 # 6. split() # 切割
s2 = 'nick|123658|180|140'
print(s2.split('|')) # 按照|切割字符串,得到的是一个列表 # 7. len长度
s1 = 'nick handsome'
print(len(s1)) ## 需要掌握(一周内掌握) # lstrip&rstrip
s3 = '**nick**'
print(s3.lstrip('*'))
print(s3.rstrip('*')) # lower&upper
s3 = 'Nick'
print(s3.lower()) # 小写
print(s3.upper()) # 大写 # startswith&endswith
s4 = 'nick handsome'
print(s4.startswith('nick')) # 以。。。开始
print(s4.endswith('some')) # 以。。。结束 # rsplit
s = 'nick|handsome|180|140|'
# 01234
# print('range(len(s))',range(len(s)))
# lt = []
# ind = -1
# for i in range(len(s)):
# if s[i] == '|':
# lt.append(s[ind+1:i])
# ind = i # ind = 4
#
# print('lt', lt) print(s.split('|',1))
print(s.rsplit('|',1)) # 从右切割 # join
s = 'nick|handsome|180|140'
lt = s.split('|')
print(lt)
# 用*拼接列表内的每一个元素
# new_s = ''
# for i in lt:
# i += '*'
# new_s += i
# print(new_s)
print('*'.join(lt)) # 使用*拼接列表内的每一个元素 # replace
s = 'nick handsome'
s = s.replace('nick','nick chen')
print(s) # isdigit/isalpha
s = 'a123123'
print(s.isdigit()) # 判断字符串内字符是否都为数字
s = 'askdlfj234'
print(s.isalpha()) # pwd = input('请输入你的密码:')
# if not pwd.isdigit():
# print('请输入纯数字') ## 了解(看到能够想起来就行) # find|rfind|index|rindex|count
s = 'nick handsome'
print(s.find('h')) # 找索引,-1表示没找到
print(s.rfind('h',6,10)) print(s.index('h'))
# print(s.index('sadf')) # 找不到报错
# print(s.find('sadf')) # 找不到返回-1 s = 'aaaaacccc'
print(s.count('a')) # center|ljust|rjust|zfill
s = 'nick'
print(s.center(50,'*')) # 居中
print(s.ljust(50,'*'))
print(s.rjust(50,'*'))
s = '111'
print(s.zfill(8)) # expandtabs
# \n 换行
# \t 缩进
s = 'a\t\t\t\ta'
print(s)
print(s.expandtabs(18)) # captalize|swapcase|title
s = 'nickNick handsome'
print(s.capitalize()) # 首字母大写
print(s.swapcase())
print(s.title()) # 每个单词的首字母大写 # is系列:有兴趣看一看,没兴趣算了 # 4. 有序or无序:有序 # 5. 可变or不可变:不可变
s = 'nick'
print(id(s))
s += 'nick'
print(id(s))
class SelfList(list):
def self_sort(self):
for i in range(len(self)): # [0,1,2,3,4,5]
for j in range(len(self)): # [0,1,2,3,4,5]
if self[i] < self[j]:
self[i], self[j] = self[j], self[i] lt = SelfList([2, 0, 4, 3, 5])
lt.sort()
print(lt)
05-11 11:04