x=3
y=4
x,y=y,x
print(x)
4
print('let us learn python!')
let us learn python!
print("let us learn python!")
let us learn python!
print('"let us learn python!"')
"let us learn python!"
print("\"let us learn python!\""\n)
"let us learn python!"
print("\"let us \n learn python!\"")
"let us
learn python!"
print("D:\three\two\one\now")
D: hree wo\one
ow
print("D:\\three\\two\\one\\now")
D:\three\two\one\now
print(r"D:\three\two\one\now")
D:\three\two\one\now
print(" \n\
*****\n\
*** \n\
*\n")
*****
***
*
print("""
致张嘉:
我身高只有155cm,但是我却喜欢180的你
我们以后的还在肯定像你""")
致张嘉:
我身高只有155cm,但是我却喜欢180的你
我们以后的还在肯定像你
print("520"+"1314")
5201314
print("我喜欢张嘉,我要嫁给他!\n"*30)
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
我喜欢张嘉,我要嫁给他!
temp=input("who are you:")
who are you:i am your father
print(temp)
i am your father
temp=input("请输入数字:")
请输入数字:8
print(temp)
8
counts=3
while counts>0:
temp=input("请猜测小鱼心中想的数字:")
guess=int(temp)
if guess==8:
print("right!")
else:
if guess<8:
print("小了!")
else:
print("大了!")
print("game over!")
li=[1,2,3,4,5,"上山打老虎"]
li[0]
1
li[5]
'上山打老虎'
li[1:3]
[2, 3]
li[0:3]
[1, 2, 3]
li[:]
[1, 2, 3, 4, 5, '上山打老虎']
li[0:6:2]
[1, 3, 5]
li[::2]
[1, 3, 5]
li[::-2]
['上山打老虎', 4, 2]
heroes=["绿巨人","钢铁侠"]
heroes.append("黑寡妇")
heroes
['绿巨人', '钢铁侠', '黑寡妇']
heroes.extend(["蜘蛛侠","奇异博士","鹰眼","冬兵"])
heroes
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵']
%%timeit
lenght=len(heroes)
heroes[lenght:]=["雷神"]
354 ns ± 48.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
heroes
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵', '雷神']
heroes[len(heroes):]=["rabbit"]
heroes
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵', '雷神', 'rabbit']
s=[1,3,4,5]
s.insert(1,2)
s
[1, 2, 3, 4, 5]
s.insert(0,0)
s
[0, 0, 1, 2, 3, 4, 5, 6]
s.remove(0)
s
[0, 1, 2, 3, 4, 5, 6]
heroes.pop(len(heroes)-1)
'rabbit'
heroes
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵', '雷神']
heroes.clear()
heroes
[]
import random
l=[random.random() for i in range(1000)]
%timeit l.sort()
4.15 µs ± 79.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
l=[random.random() for i in range(1000)]
%time l.sort()
Wall time: 0 ns
%time l.sort()
Wall time: 0 ns
%lsmagic
Available line magics:
%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
heroes=["钢铁侠","绿巨人","小蜘蛛"]
heroes
['钢铁侠', '绿巨人', '小蜘蛛']
heroes[1]="奇异博士"
heroes
['钢铁侠', '奇异博士', '小蜘蛛']
heroes[2:]=["猪猪侠","蜘蛛侠"]
heroes
['钢铁侠', '奇异博士', '猪猪侠', '蜘蛛侠']
num=[1,2,4,4,3,5,7,7,9]
num.sort()
num
[1, 2, 3, 4, 4, 5, 7, 7, 9]
num.reverse()
num
[9, 7, 7, 5, 4, 4, 3, 2, 1]
heroes.reverse()
heroes
['蜘蛛侠', '猪猪侠', '奇异博士', '钢铁侠']
num=[1,2,4,4,3,5,7,7,9]
num.sort(reverse=True)
num
[9, 7, 7, 5, 4, 4, 3, 2, 1]
num.count(4)
2
heroes.index("钢铁侠")
3
heroes[heroes.index("钢铁侠")]="神奇女侠"
heroes
['蜘蛛侠', '猪猪侠', '奇异博士', '神奇女侠']
num.index(4,3,8)
4
copy_num1=num.copy()
copy_num1
[9, 7, 7, 5, 4, 4, 3, 2, 1]
copy_num2=num[:]
copy_num2
[9, 7, 7, 5, 4, 4, 3, 2, 1]
s=[1,2,3]
t=[4,5,6]
s+t
[1, 2, 3, 4, 5, 6]
s*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
matrix=[[1,2,3],
[4,5,6],
[7,8,9]]
matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i in matrix:
for each in i:
print(each)
1
2
3
4
5
6
7
8
9
for i in matrix:
for each in i:
print(each,end=' ')
print()
1 2 3
4 5 6
7 8 9
print(matrix[0])
print(matrix[0][0])
[1, 2, 3]
1
A=[0]*3
A
[0, 0, 0]
for i in range(3):
A[i]=[0]*3
A
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
x="fish"
y="fish"
x is y
True
x=[1,2,3]
y=[1,2,3]
x is y
False
x=[1,2,3]
y=x#引用
x[1]=1
x
[1, 1, 3]
y#x发生改变,y也要发生改变
[1, 1, 3]
x=[1,2,3]
y=x.copy()
x[1]=1
x
[1, 1, 3]
y#y的值不会随着x发生改变
[1, 2, 3]
x=[1,2,3]
y=x[:]
x[1]=1
x
[1, 1, 3]
y
[1, 2, 3]
x=[[1,2,3],
[4,5,6],
[7,8,9]]
y=x.copy()
x[1][1]=0
x
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
y
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
注意:
y也被修改了。因为浅拷贝copy的只是最外层对象,其余的部分任然是引用。
import copy
x=[[1,2,3],
[4,5,6],
[7,8,9]]
y=copy.copy(x)
x[1][1]=0
x
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
y
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
import copy
x=[[1,2,3],
[4,5,6],
[7,8,9]]
y=copy.deepcopy(x)
x[1][1]=0
x
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
y
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
oho=[1,2,3,4,5]
for i in range(len(oho)):
oho[i]=oho[i]*2
oho
[2, 4, 6, 8, 10]
oho=[1,2,3,4,5]
oho=[i*2 for i in oho]
oho
[2, 4, 6, 8, 10]
x=[i for i in range(10)]
x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x=[i+1 for i in range(10)]
x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x=[]
for i in range(10):
x.append(i+1)
x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y=[c*2 for c in "yuhongxia"]
y
['yy', 'uu', 'hh', 'oo', 'nn', 'gg', 'xx', 'ii', 'aa']
code=[ord(c)for c in "yuhongxia"]
code#ord()将字符转化成对应编码
[121, 117, 104, 111, 110, 103, 120, 105, 97]
matrix=[[1,2,3],
[4,5,6],
[7,8,9]]
col2=[row[1] for row in matrix]#取第二列元素
col2
[2, 5, 8]
diag=[matrix[i][i] for i in range(len(matrix))]
diag#主对角线元素
[1, 5, 9]
h=[matrix[i][len(matrix[0])-1-i] for i in range(len(matrix))]
h#副对角线
[3, 5, 7]
使用原始字符r
使用原始字符r,转义字符将不再有效
字符串最后加反斜杠表示字符串没完,换行
可以使用长字符串输出
重现随机数
import random
x=random.getstate()
random.randomint(1,10)
Random.getstate(x)
Python中0.1+0.2>0.3
引入demical(十进制)
短路逻辑:
从左往右,只有当第一个操作数的值无法确定逻辑运算结果时,才对第二个操作数进行求值。
range()函数
range(stop)
range(start,stop)
range(start,stop,step)
列表:
增
append()只能只能增加一个元素
extend()只能是可迭代元素
删除:
remove()只能移走与之匹配的第一个元素
若列表中无该元素则报错
清除clear()
修改
排序
sort()
reverse()
sort(reverse=true)
查找:
count()统计个数
index()查看索引下标
index(元素,start,end)
copy()=num[:]