python小甲鱼教学:01

1.总结

  1. 使用原始字符r

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值