The for loop flow control
for + break
# for+break
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
if name == 'jason':
break
print(name)
# break会退出本层循环
for + continue
# for+continue
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
if name == 'jason':
continue
print(name)
# continue 会退出本次循环
nested for loop
# for循环嵌套
for i in range(3):
print(f'-----:{i}')
for j in range(2):
print(f'*****:{j}')
#外层循环一次,内层会循环所有
for + else
# for+else
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
print(name)
else:
print('for循环没有被break中断掉')
#for循环没有被break的时候会触发else
for realization of loading cycle
import time
print('Loading', end='')
for i in range(6):
print(".", end='')
time.sleep(0.2)
Variable and immutable
Variable : ID does not change the value of variable
Immutable : variable ID value becomes
Variable types of lists, dictionaries
Immutable type digital type, a string
Order and disorder
Order : the index is ordered
Disorder : no index is disorderly
Ordered data types include strings, lists,
Data types include disorderly dictionary
Built-in digital type method
A, int
1. Role
The age, ID number
2. Define the way
x=10
### 3.使用方法
+ - * / % // ** < <= > >= == !=
Second, the float
1. Role
Salary, height
2. Define the way
x=10.2
3. Use
+ - * / % // ** < <= > >= == !=
String type built-in method
First, the role
Full name
Second, the definition mode
x='name'
Third, the built-in method
Priority grasp
- Index values
s = 'nick handsome'
print(s[1])
- Index sliced
s = 'nick handsome'
print(s[0:4]) # 顾头不顾尾
print(s[0:4:2]) # 2表示步长,隔一个取一个
print(1, s[4:0:-1]) # +从左到右,-表示从右到左
print(1, s[2:]) # 左边的不写取到最左边,右边的不写取到最右边
- for loop
s = 'nick handsome' #循环出每一个字符
for i in s:
print(i)
- Member operator
s = 'nick handsome' #判断是否存在该序列,返回布尔值
print('nick' in s)
print('nick1' not in s) # True
- len length
s1 = 'nick handsome' #计算字符长度
print(len(s1))
- strip
# 默认去除两端空格,可以指定去除的字符,可以指定多个字符同时去掉
s1 = 'a nick ******'
print(s1.strip())
print(s1.strip(' kc*')) # 可以乱序,但是不能没有
- split
s2 = 'nick|123658|180|140'
print(s2.split('|')) # 按照|切割字符串,得到的是一个列表
Need to know
- rstrip/lstrip
# strip() 默认去除两端空格
s3 = '**nick**' #lstrip 从左开始去除,rstrip 从右边开始去除
print(s3.lstrip('*'))
print(s3.rstrip('*'))
- rsplit / split
#split默认从左边开始切割
s = 'nick|handsome|180|140|'
print(s.split('|',1))
print(s.rsplit('|',1)) # 从右切割
- lower/upper
s3 = 'Nick'
print(s3.lower()) # 小写
print(s3.upper()) # 大写
- startswith/endswith
s4 = 'nick handsome'
print(s4.startswith('nick')) # 以。。。开始 返回布尔值
print(s4.endswith('some')) # 以。。。结束
- isdigit/isalpha
s = 'a123123'
print(s.isdigit()) # 判断字符串内字符是否都为数字, 返回布尔值
s = 'askdlfj234'
print(s.isalpha()) # 判断字符串内字符是否都为字母
To understanding
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 / bright / rjust / zfill
s = 'nick' print(s.center(50,'*')) # 居中 print(s.ljust(50,'*')) print(s.rjust(50,'*')) s = '111' print(s.zfill(8)) #自动补足8位 常用于二进制
expandtabs
s = 'a\t\t\t\ta' # \n 换行 # \t 缩进 print(s) print(s.expandtabs(18)) #缩进
capital/swapcase/title
s = 'nickNick handsome' print(s.capitalize()) # 首字母大写 print(s.swapcase()) #全部大写 print(s.title()) # 每个单词的首字母大写
series is
- sdecimal (): check whether the string value contains decimal character, if it returns True, otherwise False.
- isdigit (): If the string contains only digit returns True, otherwise False.
- isnumeric (): If the string contains only numeric characters, it returns True, otherwise False.
List type built-in method
First, the role
Storing a plurality of types of data
Second, the definition mode
In [] with the separating each data store any data type
Third, the built-in method
Priority grasp
Cited values / value index modifier
lt = [1, 2, 3, 4] print(lt[1]) #索引取值 lt[1] = 3 #索引修改值 print(lt)
Index sliced
lt = [1, 2, 3, 4] print(lt[:]) print(lt[1:2]) #顾头不顾尾 print(lt[1:4:2]) # 三个参数为 开始的索引 结束的索引 步长
for loop
lt = [1, 2, 3, 4] for i in lt: print(i ** 2)
Member operator
lt = [1, 2, 3, 4] print(1 in lt) # 返回布尔值 print(5 in lt)
len length
lt = [1, 2, 3, 4] print(len(lt)) #计算长度
append
lt = [1, 2, 3, 4] lt.append(5) # 追加元素 print(lt)
of the
lt = [1, 2, 3, 4] print(lt) del lt[0] # 删除某一个元素 print(lt)
Need to know
sort
lt = [2, 3, 1, 0, 4] lt.sort(reverse=True) #reverse 为true时, 排序为降序 print(lt) #排序, 默认升序
reverse
lt = [1, 2, 3, 4] lt.reverse() # 反转 print(lt)
pop
lt = [11, 22, 33, 44, 55] lt.pop(0) # 按照索引删除某一个元素 print(lt)
remove
lt = [11, 22, 33, 44, 55] lt.remove(22) # 按照数值删除元素 print(lt)
index
lt = [11, 22, 33, 44, 55] print(lt.index(11)) # 找到了就返回 没找到就报错
insert
lt = [1, 2, 3, 4, 5] lt.insert(0, 0) # 往前插入 两个参数为 索引,插入的数值 print(lt)
extend
lt1 = [1, 2, 34] lt2 = [1, 1, 2, ] lt1.extend(lt2) #扩展列表 print(lt1)
copy
lt = [1, 2, 3, 4] lt1 = lt.copy() # 复制列表 print(lt1)
clear
lt = [1, 2, 2, 3] lt.clear() # 清空列表 print(lt)