cpa单页网站怎么做如何推广网站链接
文章目录
- 3 函数
- 3.3 None 值
- 3.4 关键字和 print()
- 3.7 异常
- 4 列表
- 4.1 用 del 语句从列表中删除值
- 4.4.1 用 index 方法在列表中查找值
- 4.4.2 用 append() 和 insert() 方法在列表中添加值
- 4.4.3 用 remove() 方法从列表中删除值
- 4.4.4 用 sort() 方法将列表中的值排序
- 4.5 python 的缩进规则
- 4.6 可变和不可变数据类型
- 4.6.3 用 list() 和 tuple() 函数来转换类型
- 4.7 copy 模块的 copy() 和 deepcopy() 函数
- 5. 字典和结构化数据
- 5.1.1字典与列表
- 5.1.2 keys()、values()和items() 方法
- 5.1.4 get() 方法
- 5.1.5 setdefault() 方法
- 6. 字符串操作
- 6.2.1 upper()、lower()、isupper()、islower()
- 6.2.4 字符串的 join() 和 split()
- 6.2.5 rjust()、ljust() 和 center() 方法对齐文本
- 6.2.6 用 strip()、rstrip() 和 lstrip() 删除空白字符
3 函数
3.3 None 值
None 的数据类型就是 NoneType
3.4 关键字和 print()
①end 关键字
print('hello', end = '')
print('world')
输出
helloworld
②sep 关键字
print('cats', 'dogs', 'mice', sep=',')
输出
cats,dogs,mice
3.7 异常
一旦执行跳到 except 子句的代码,就不会回到 try 子句
4 列表
4.1 用 del 语句从列表中删除值
spam = ['cat', 'bat', 'rat', 'elephant']
del spam[2]
spam
输出
[‘cat’, ‘bat’, ‘elephant’]
4.4.1 用 index 方法在列表中查找值
spam = ['hello', 'hi', 'howdy', 'heyas']
spam.index('hello')
输出
0
4.4.2 用 append() 和 insert() 方法在列表中添加值
append() 方法会将参数添加到列表末尾
spam = ['cat', 'dog', 'bat']
spam.append('moose')
spam
输出
[‘cat’, ‘dog’, ‘bat’, ‘moose’]
insert() 方法可以在列表中任意位置插入一个值
spam = ['cat', 'dog', 'bat']
spam.insert(1, 'chicken')
spam
输出
[‘cat’, ‘chicken’, ‘dog’, ‘bat’]
4.4.3 用 remove() 方法从列表中删除值
spam = ['cat', 'bat', 'rat', 'elephant']
spam.remove('bat')
spam
输出
[‘cat’, ‘rat’, ‘elephant’]
4.4.4 用 sort() 方法将列表中的值排序
reverse 关键字参数为 True,让 sort() 按逆序排序
spam.sort(reverse=True)
spam
输出
[‘elephants’, ‘dogs’, ‘cats’, ‘badgers’, ‘ants’]
注意:不能即对有数字又有字符串值的列表排序
4.5 python 的缩进规则
Python 知道,没有看到结束方括号,列表就没有结束
spam = ['apples','oranges','bananas','cats']print(spam)
输出
[‘apples’, ‘oranges’, ‘bananas’, ‘cats’]
也可以在行末使用续行字符 \,将一条指令写成多行
print('Four score and seven ' + \
'years ago...')
4.6 可变和不可变数据类型
字符串是“不可变的”,改变一个字符串的正确方式,是使用切片和连接。构造一个新的字符串,从老的字符串哪里复制一些部分。
name = 'Zophie a cat'
newName = name[0:7] + 'the' + name[8:12]
print(name)
print(newName)
输出
Zophie a cat
Zophie the cat
4.6.3 用 list() 和 tuple() 函数来转换类型
用 list 构造 tuple
tuple(['cat', 'dog', 5])
输出
(‘cat’, ‘dog’, 5)
用 tuple 构造 list
list(('cat', 'dog', 5))
输出
[‘cat’, ‘dog’, 5]
用 str 构造 list
list('hello')
输出
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
4.7 copy 模块的 copy() 和 deepcopy() 函数
copy.copy(),可以用来复制列表或字典这样的可变值,而不只是复制引用
import copy
spam = ['A', 'B', 'C', 'D']
cheese = copy.copy(spam)
cheese[1] = 42
print(spam)
print(cheese)
输出
[‘A’, ‘B’, ‘C’, ‘D’]
[‘A’, ‘42’, ‘C’, ‘D’]
注意:如果复制的列表包含了列表 [[]],那就使用 copy.deepcopy() 函数来替代。deepcopy() 函数将同时复制它们内部的列表。
5. 字典和结构化数据
5.1.1字典与列表
确定两个列表是否相同时,表项的顺序和重要,但是在字典中,键-值对的顺序并不重要
spam = ['cats', 'dogs', 'moose']
bacon = ['dogs', 'moose', 'cats']
spam == bacon
输出
False
eggs = {'name': 'Zophie', 'species': 'cat', 'age': '8'}
ham = {'species': 'cat', 'age': '8', 'name': 'Zophie'}
eggs == ham
输出
True
5.1.2 keys()、values()和items() 方法
spam = {'color':'red', 'age':42}
for v in spam.keys():print(v)
输出
color
age
spam = {'color':'red', 'age':42}
for v in spam.values():print(v)
输出
red
42
spam = {'color':'red', 'age':42}
for v in spam.items():print(v)
输出
(‘color’, ‘red’)
(‘age’, 42)
可以利用多重复制的方法,讲items的键和值复制给不同的变量
spam = {'color':'red', 'age':42}
for k, v in spam.items():print('The key is ' + k + ' and the value is ' + str(v) + '.')
输出
The key is color and the value is red.
The key is age and the value is 42.
5.1.4 get() 方法
在访问一个键的值之前,检查该键是否在字典中,这很麻烦。字典有一个 get() 方法,他有两个参数:要取得其值的键,以及如果该键不存在时,返回备用值。
dic = {'color':'red', 'age':42}
print('my hair color is ' + dic.get('color', 'black'))
输出
my hair color is red
对键进行改动之后
dic = {'color':'red', 'age':42}
print('my hair color is ' + dic.get('abc', 'black'))
输出
my hair color is black
5.1.5 setdefault() 方法
查询字典中某个键的值,如果键不存在,则设置为第二个参数
spam = {'name':'Pooka', 'age':'5'}
spam.setdefault('name', 'Noah')
输出
‘Pooka’
spam = {'name':'Pooka', 'age':'5'}
spam.setdefault('color', 'white')
当键不存在时,输出第二个参数
‘white’
打印字典 spam,发现 ‘color’: ‘white’ 也已经存进去了
{‘name’: ‘Pooka’, ‘age’: ‘5’, ‘color’: ‘white’}
6. 字符串操作
6.2.1 upper()、lower()、isupper()、islower()
upper() 将字符串所有字符转换成大写
lower() 小写
spam = 'hello world!'
spam.upper()
输出
‘HELLO WORLD!’
spam = 'HELLO WORLD!'
spam = spam.lower()
spam
输出
‘hello world!’
- isupper() 判断字符串所有字符是大写吗
- islower() 都是小写吗
- isalpha() 返回 True,所有字符串只包含字母,而且非空
- isalnum() 返回 True,字符串只包含字母和数字,而且非空
- isdecimal() 返回 True,字符串只包含数字字符,而且非空
- isspace() 返回True,字符串只包含空格、制表符和换行,并且非空
- istitle() 返回True,字符串仅包含大写字母开头、后面都是小写字母的单词
- startwith() 返回True,如果调用的字符串以某个字符串开始
- endwith() 返回True,如果调用的字符串以某个字符串结束
6.2.4 字符串的 join() 和 split()
str.join([str1, str2, str3]) 将字符串加入到字符串列表的每一个元素中,返回一个字符串
str = ', '.join(['cat', 'rat', 'bat'])
str
输出
‘cat, rat, bat’
str.split(char_or_str) 将字符串用字符或者字符串分割开来,返回字符串列表
str = '1, 2, 3'
list = str.split(', ')
list
输出
[‘1’, ‘2’, ‘3’]
6.2.5 rjust()、ljust() 和 center() 方法对齐文本
先上实例
'Hello'.rjust(10)
输出
’ Hello’
解释:默认通过插入空格来对齐文本,rjust() 右对齐,lust() 左对齐,center() 中间对齐,字符串总共占10字节,剩下5字节用空格填充,也可以设置填充的字符
'Hello'.rjust(10, '*')
输出
‘*****Hello’
6.2.6 用 strip()、rstrip() 和 lstrip() 删除空白字符
strip() 删除两边的空白字符(空格、制表符和换行符)
lstrip() 删除左边的空白字符
rstrip() 删除右边的空白字符