b
=
"a"
b
+
=
"c"
b
=
1
b
+
=
1
x, y, z
=
0
,
0
,
0
print
(b)
x
=
int
(x)
x
=
10
def
my_function():
global
x
print
(x)
x
=
20
print
(x)
my_function()
c
=
f
"{a} {b}"
print
(
"a:\b\nc\nd"
)
b
=
b.rstrip()
b
=
是我的.removeprefix(
'我的'
)
b
+
=
a.line.lstrip(),在b后面接着写a,并且去除a的左边空格分割字符串为列表
text
=
"one-two-three-four"
parts
=
text.split(
'-'
, maxsplit
=
2
)
print
(parts)
zimu
=
[
'a'
,
'b'
,
'c'
,
'd'
]
a
=
zimu[
-
3
:]
zimu2
=
zimu[:]
zimu2
=
copy.deepcopy(zimu)
print
(zimu[
0
].title())
zimu[
0
]
=
'e'
zimu.append(
'f'
)
zimu.insert(
0
,
'g'
),
del
zimu[
0
]
x
=
zimu.pop(
0
)
zimu.remove(
'a'
)
while
'a'
in
zimu
zimu.remove(
'a'
)
zimu.sort()
zimu.sort(reverse
=
True
)
zimu.reverse()
print
(
sorted
(zimu))
len
(zimu)
5.
操作列表
for
循环
zimu
=
[
'a'
,
'b'
,
'c'
,
'd'
]
for
xx
in
zimu:
print
(xx)
range
(
1
,
6
)
range
(start, stop, step)
numbers
=
list
(
range
(
1
,
6
))
for
x
in
range
(
30
):
for
循环
30
次
f
=
[]
for
value
in
range
(
1
,
11
):
p
=
value
*
*
2
f.append(p)
squares
=
[value
*
*
2
for
value
in
range
(
1
,
11
)]
min
(p)
max
(p)
sum
(p)
6.
元组
7.if
如果
xx
=
[
'a'
,
'b'
,
'c'
,
'd'
]
for
xy
in
xx:
if
xy
=
=
'b'
:
if
xy
=
=
'b'
and
xy <c
print
(xy.upper())
else
:
print
(xy)
if
-
elif
-
else
if
-
if
-
else
zimu
=
[]
if
zimu:
早饭
=
{
'品种'
:
'包子'
,
'个数'
:
'5'
}
早饭
=
{
'品种'
:
'包子'
,
'个数'
:
'5'
}
print
(早饭[
'品种'
])
早饭[
'大小'
]
=
'小笼包'
早饭[
'品种'
]
=
' 粥
del
早饭[
'品种'
]
早饭.get(
'颜色'
,
'none(可改或者不写)'
)
for
k, v
in
早饭.items():
print
(k,v)
for
k
in
sorted
(早饭.keys()):
sorted
改
set
,变成去除重复值,输出的是集合
字典
=
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
}
value
=
字典.pop(
'd'
,
'不存在(可以自己改)'
)
print
(value)
每天的早饭
=
[早饭_0, 早饭_1, 早饭_2]
早饭
=
{
'品种'
:
'包子'
,
'内容'
: [
'a'
,
'b'
,
'c'
,
'd'
],}
早饭
=
{
'品种'
:
'包子'
,
'用料'
:{
'材料'
:
'牛肉'
,
'重量'
:
'5斤'
}}
x
=
input
(
"请输入"
)
x
=
int
(x)
print
(x)
a
=
1
while
a <
=
5
: 循环到
5
为止
print
(a)
a
+
=
1
while
a
=
1
:
x
=
input
(请输入)
if
x
=
=
'退出'
:
break
未验证
=
[
'a'
,
'b'
,
'c'
]
已验证
=
[]
while
未验证:
待添加
=
未验证.pop()
已验证.append(待添加)
while
'a'
in
zimu
zimu.remove(
'a'
)
def
xx(y,z):
def
xx(y,z
=
"好好"
): 默认z等于好好,可以后面不指定用默认值,也可以指定""表示空白
print
(f
"{y} {z}"
)
aa
=
f
"{y} {z}"
return
aa
xx(
"你好"
,
"不好"
) 实参
xx(y
=
"你好"
,z
=
"不好"
)
变量
=
xx(y
=
"你好"
,z
=
"不好"
)
def
xx(y,
*
z):
print
(z)
xx(
12
,
'a'
,
'b'
,
'c'
)
from
1
import
函数_0 函数_1 函数_2
rom
1
import
函数_0 as xx
class
Car:
def
__init__(
self
, brand, model):
self
.d
=
brand
m
=
model
print
(m)
self
.model
=
model
self
.xxx
=
1
def
describe(
self
):
print
(f
"The car is a {self.d} {self.model}."
)
my_car
=
Car(
"Tesla"
,
"Model S"
)
my_car.describe()
print
(my_car.model)
class
电动Car(Car):
def
__init__(
self
, brand, model, 电量):
super
().__init__(brand, model)
self
.b
=
电量
self
.其他
=
其他()
def
describe_battery(
self
):
print
(f
"This electric car has a {self.b}-kWh battery."
)
my_电动_car
=
电动Car(
"Tesla"
,
"Model 3"
,
"75"
)
my_电动_car.describe()
my_电动_car.describe_battery()
class
Engine:
def
__init__(
self
, horsepower):
self
.horsepower
=
horsepower
def
display_horsepower(
self
):
print
(f
"This engine has {self.horsepower} horsepower."
)
class
Car:
def
__init__(
self
, brand, model, horsepower):
self
.brand
=
brand
self
.model
=
model
self
.engine
=
Engine(horsepower)
def
describe_car(
self
):
print
(f
"The car is a {self.brand} {self.model}."
)
self
.engine.display_horsepower()
my_car
=
Car(
"Tesla"
,
"Model S"
,
450
)
my_car.describe_car()
my_car.engine.display_horsepower() 这样也可以调用,()里可以填参数,这个函数里没有
from
car
import
Car ,电动car
import
car 导入整个模块,后面使用就是car.Car,但是上面那句导入可用Car
from
random
import
randint
randint(
1
,
6
)
from
random
import
choice
players
=
[
'1'
,
'2'
,
'3'
]
first_up
=
choice(players)
from
pathlib
import
Path
path
=
Path(
'pi_digits.txt'
)
contents
=
path.read_text(encoding
=
'utf-8'
)
path.write_text(
"123"
)
xx
=
contents.splitlines()
print
(xx)
try
:
a
=
5
/
q
except
: xxxError
print
(
"你不能除以0"
)
else
:
except
如果跳过就执行这个,不跳过不执行
print
(a)
path
=
Path(
'username.json'
)
if
not
path.exists():
else
:
from
pathlib
import
Path
import
json
numbers
=
[
2
,
3
,
5
,
7
,
11
,
13
]
path
=
Path(
'numbers.json'
)
contents
=
json.dumps(numbers)
path.write_text(contents)
contents
=
path.read_text()
numbers
=
json.loads(contents)
def
add(a, b):
return
a
+
b
def
test_add():
assert
add(
2
,
3
)
=
=
5
assert
add(
5
,
0
)
=
=
5
pytest test_example.py
import
pytest
@pytest
.fixture(scope
=
"module"
)
def
my_fixture():
print
(
"Setup: my_fixture"
)
yield
print
(
"Teardown: my_fixture"
)
def
test_one(my_fixture):
print
(
"Running test_one"
)
assert
True
def
test_two(my_fixture):
print
(
"Running test_two"
)
assert
True
def
pytest_sessionstart(session):
print
(
"xx"
)
def
pytest_sessionfinish(session,exitstatus):