赞
踩
使用原始字符r
重现随机数
import random
x=random.getstate()
random.randomint(1,10)
Random.getstate(x)
range(stop)
range(start,stop)
range(start,stop,step)
增
append()只能只能增加一个元素
extend()只能是可迭代元素
删除:
remove()只能移走与之匹配的第一个元素
若列表中无该元素则报错
清除clear()
修改
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!\""\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")
*****
***
*
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.
%run?
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]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。