table of Contents
First, review the lesson content
while 循环
while + break
跳出本层循环
while + continue
跳出本次循环
while + else
循环没有被break终止才会执行
for 循环
for + break
for + continue
for + else
进制转换
Second, the method of the built-in type digital
2.1 Integer built-in method
effect
Description age, number, id number
Defined way
x = 10
x = int('10')
x = int(10.1)
x = int('10.1') # 报错
Built-in method
No built-in method, only the arithmetic and compare operations
A value or presence of a plurality of values
Save a value
Orderly or disorderly orderly: the index; disorder: None Index
Are they not say that
Variable or non-variable (Key)
Digital type immutable
Variable (variable value terms)
lis = [1,2,3] print(id(lis)) lis[0] = 2 #lis-->[2,2,3] print(id(lis))
Immutable (in terms of the value of the variable), the value becomes variable id
x = 10 print(id(x)) x = 20 print(id(x))
2.2 float built-in method
With integer
Third, the character string type of built-in method
3.1 Role
Name / gender / address
3.2 define how
s = b‘sdkfljl’ # 打印出来的bytes类型,二进制类型,010011001011001011
print(s)
print(‘中文’,encode(‘utf8’))
3.3 String built-in method (only string type to use)
s = ‘forever handsome’
Priority master (you must have to master today)
Index values
print(s[1])
Index sliced
print(s[4:0:1]) # 1 表示从左到右 print(s[-4:0:-1]) # -1 表示从右到左
for loop
for i in s: print(i)
strip () specified character ends removed
s1 = ‘ nick handsome ’ print(s1.strip()) # 去除两端的空白 s2 = '****!!!nick handsome-----***' print(s2.strip('-*!')) # 指定多个字符一起去掉,只能strip里面有的字符就全部干掉
- split () Cutting
python print(s.split()) # 默认以空格为切割条件 print(s.split('/')) # 以/切割 print(s.split('!')) # 以!切割
in 或 not in
python print('forever' in s ) #True print('!' not in s) # True
Length len
```python s = '123' print(len(s)) # 3 # 求字符串的长度 ```
You need to know (mastered within a week, Monday to dictation)
# 1. lstrip() 和 rstrip()
s2 = '***!!!!!nick handsome----***'
print(s2.lstrip('*'))
print(s2.rstrip('*'))
# 2. rsplit()
print(s2.split('*', 1))
print(s2.rsplit('*', 1))
# lower&upper
s3 = 'aaabbJ'
print(s3.lower())
print(s3.upper())
# startswith&endswith
s3 = 'aaabbJ'
print(s3.startswith('b'))
print(s3.endswith('J'))
#
# join(用的比较多)一般和split联用
s3 = ' '
print(s3.join(['234', '234', '234'])) # 以s3为间隔符,拼接列表里的每一个元素
s = '辣条/薯片/汽水/泡面/火腿肠/枸杞/当归/鹿茸'
s1 = s.split('/')
print('*'.join(s1))
# replace
s2 = 'yongjiu handsome'
print(s2.replace('yongjiu', 'gebilaowang'))
# isdigit(纯数字)/isalpha(纯字母)
s2 = '12312'
print(s2.isdigit())
s3 = 'aaac1c'
print(s3.isalpha())
# username_db ='nick'
# pwd_db = '123'
#
#
# inp_username = input('username:')
# inp_pwd = input('pwd:')
#
# if inp_username.isalpha():
# pass
#
# if inp_pwd.isdigit():
# pass
Understand
# find|rfind|index|rindex|count
s2 = '**23423***ni234234ck $$ hand223423some******'
# 01234567891011
print(s2.find('$')) # 从左找,找到第一个停止,找不到返回-1
print(s2.rfind('$')) # 从右找,找到就停止,找不到返回-1
print(s2.index('$')) # 找不到报错
print(s2.rindex('$')) # 找不到报错
# center|ljust|rjust|zfill
s2 = 'nick handsome'
print(s2.center(50, '*')) # 居中
print(s2.ljust(50, '*')) # 居左
print(s2.rjust(50, '*')) # 居右
print(s2.zfill(50)) # 填充0居右
# expandtabs
s2 = 'a\ta'
print(s2)
print(s2.expandtabs(8)) # 针对\t而言
# captalize|swapcase|title 只针对英文
s2 = 'harry Potter'
print(s2.capitalize()) # 首字母(一句话的开头)大写,其他全小写,用在段落开始
print(s2.swapcase()) # 大小写互换
print(s2.title()) # 所有单词首字母大写
# is系列(有兴趣的自己了解)
3.4 save a value or multiple values
A value
3.5 orderly or disorderly
Ordered
3.6 Variable or immutable (focus)
Variable: The value of constant change id, not a hash
Immutable: variable id value becomes, hashable
Immutable
s2 = 'abc'
print(id(s2))
s2 += 'abc'
print(id(s2))
Fourth, after-school practice
https://www.cnblogs.com/foreversun92/p/11290579.html
Summarize today
今天首先对上节课的内容进行了回顾,上节课主要学习了while循环和for循环,讲了怎么定义和使用,以及break+continue+else的配合。这节课主要学习了数字类型和字符串的一些内置方法,这些内置方法可以帮助我们更好地处理数据,对数据进行分析和打印。