当前位置:   article > 正文

python小甲鱼教学:01_小甲鱼python

小甲鱼python

1.总结

  1. 使用原始字符r

    • 使用原始字符r,转义字符将不再有效
    • 字符串最后加反斜杠表示字符串没完,换行
    • 可以使用长字符串输出
  2. 重现随机数

import random
x=random.getstate()
random.randomint(1,10)
Random.getstate(x)
  • 1
  • 2
  • 3
  • 4
  1. Python中0.1+0.2>0.3
    float 是非精确的存储
    想精确的处理小数时,可以使用decimal
    引入demical(十进制)
  2. 短路逻辑:
    从左往右,只有当第一个操作数的值无法确定逻辑运算结果时,才对第二个操作数进行求值。
  3. range()函数
range(stop)
range(start,stop)
range(start,stop,step)
  • 1
  • 2
  • 3
  1. 列表:

    1. append()只能只能增加一个元素
      extend()只能是可迭代元素

    2. 删除:
      remove()只能移走与之匹配的第一个元素
      若列表中无该元素则报错

    3. 清除clear()

    4. 修改

  2. 排序
    sort()
    reverse()
    sort(reverse=true)
  3. 查找:
    count()统计个数
    index()查看索引下标
    index(元素,start,end)
    copy()=num[:]

2. 代码

x=3
y=4
x,y=y,x
print(x)
  • 1
  • 2
  • 3
  • 4
4
  • 1
print("let us learn python!")
  • 1
let us learn python!
  • 1
print('"let us learn python!"')
  • 1
"let us learn python!"
  • 1
print("\"let us learn python!\""\n)
  • 1
"let us learn python!"
  • 1
print("\"let us \n learn python!\"")
  • 1
"let us 
 learn python!"
  • 1
  • 2
print("D:\three\two\one\now")
  • 1
D:	hree	wo\one
ow
  • 1
  • 2
print("D:\\three\\two\\one\\now")
  • 1
D:\three\two\one\now
  • 1
print(r"D:\three\two\one\now")
  • 1
D:\three\two\one\now
  • 1
print("     \n\
       *****\n\
        *** \n\
         *\n")
  • 1
  • 2
  • 3
  • 4
       *****
        *** 
         *
  • 1
  • 2
  • 3
temp=input("who are you:")
  • 1
who are you:i am your father
  • 1
print(temp)
  • 1
i am your father
  • 1
temp=input("请输入数字:")
  • 1
请输入数字:8
  • 1
print(temp)
  • 1
8
  • 1
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!")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
li=[1,2,3,4,5,"上山打老虎"]
li[0]
  • 1
  • 2
1
  • 1
li[5]
  • 1
'上山打老虎'
  • 1
li[1:3]
  • 1
[2, 3]
  • 1
li[0:3]
  • 1
[1, 2, 3]
  • 1
li[:]
  • 1
[1, 2, 3, 4, 5, '上山打老虎']
  • 1
li[0:6:2]
  • 1
[1, 3, 5]
  • 1
li[::2]
  • 1
[1, 3, 5]
  • 1
li[::-2]
  • 1
['上山打老虎', 4, 2]
  • 1
heroes=["绿巨人","钢铁侠"]
  • 1
heroes.append("黑寡妇")
  • 1
heroes
  • 1
['绿巨人', '钢铁侠', '黑寡妇']
  • 1
heroes.extend(["蜘蛛侠","奇异博士","鹰眼","冬兵"])
  • 1
heroes
  • 1
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵']
  • 1
%%timeit
lenght=len(heroes)
heroes[lenght:]=["雷神"]
  • 1
  • 2
  • 3
354 ns ± 48.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
  • 1
heroes
  • 1
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵', '雷神']
  • 1
heroes[len(heroes):]=["rabbit"]
heroes
  • 1
  • 2
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵', '雷神', 'rabbit']
  • 1
s=[1,3,4,5]
s.insert(1,2)
s
  • 1
  • 2
  • 3
[1, 2, 3, 4, 5]
  • 1
s.insert(0,0)
s

  • 1
  • 2
  • 3
[0, 0, 1, 2, 3, 4, 5, 6]
  • 1
s.remove(0)
s
  • 1
  • 2
[0, 1, 2, 3, 4, 5, 6]
  • 1
heroes.pop(len(heroes)-1)
  • 1
'rabbit'
  • 1
heroes
  • 1
['绿巨人', '钢铁侠', '黑寡妇', '蜘蛛侠', '奇异博士', '鹰眼', '冬兵', '雷神']
  • 1
heroes.clear()
  • 1
heroes
  • 1
[]
  • 1
import random
l=[random.random() for i in range(1000)]
%timeit l.sort()
  • 1
  • 2
  • 3
4.15 µs ± 79.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
  • 1
l=[random.random() for i in range(1000)]
%time l.sort()
  • 1
  • 2
Wall time: 0 ns
  • 1
%time l.sort()
  • 1
Wall time: 0 ns
  • 1
%lsmagic
  • 1
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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
%run?
  • 1
heroes=["钢铁侠","绿巨人","小蜘蛛"]
heroes
  • 1
  • 2
['钢铁侠', '绿巨人', '小蜘蛛']
  • 1
heroes[1]="奇异博士"
heroes
  • 1
  • 2
['钢铁侠', '奇异博士', '小蜘蛛']
  • 1
heroes[2:]=["猪猪侠","蜘蛛侠"]
heroes
  • 1
  • 2
['钢铁侠', '奇异博士', '猪猪侠', '蜘蛛侠']
  • 1
num=[1,2,4,4,3,5,7,7,9]
num.sort()
  • 1
  • 2
num
  • 1
[1, 2, 3, 4, 4, 5, 7, 7, 9]
  • 1
num.reverse()
  • 1
num
  • 1
[9, 7, 7, 5, 4, 4, 3, 2, 1]
  • 1
heroes.reverse()
heroes
  • 1
  • 2
['蜘蛛侠', '猪猪侠', '奇异博士', '钢铁侠']
  • 1
num=[1,2,4,4,3,5,7,7,9]
num.sort(reverse=True)
num
  • 1
  • 2
  • 3
[9, 7, 7, 5, 4, 4, 3, 2, 1]
  • 1
num.count(4)
  • 1
2
  • 1
heroes.index("钢铁侠")
  • 1
3
  • 1
heroes[heroes.index("钢铁侠")]="神奇女侠"
  • 1
heroes
  • 1
['蜘蛛侠', '猪猪侠', '奇异博士', '神奇女侠']
  • 1
num.index(4,3,8)
  • 1
4
  • 1
copy_num1=num.copy()
copy_num1

  • 1
  • 2
  • 3
[9, 7, 7, 5, 4, 4, 3, 2, 1]
  • 1
copy_num2=num[:]
copy_num2
  • 1
  • 2
[9, 7, 7, 5, 4, 4, 3, 2, 1]
  • 1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/412554
推荐阅读
相关标签
  

闽ICP备14008679号