小余同学的小甲鱼教学笔记呀

1. 输出

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

2. 循环与分支

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!")

3. 列表

li=[1,2,3,4,5,"上山打老虎"]
li[0]
1

3.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]

3.2 增:

  • append():一次只能添加一个元素
  • extend():必须是一个可迭代对象
  • insert():插入
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]

3.3 删除:

  • remove():内容为元素。如果删除元素有多个,则删除索引最小的那个;如果删除元素不在列表中,则报错。
  • pop():内容为下标索引。
  • clear():清空。
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.

3.4 改:

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]

3.5 查找:

  • count():计数
  • index(x,star,end):返回索引
  • copy():拷贝
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]

3.6 列表的加法和乘法:

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]

3.7 嵌套列表

访问

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]]

is :是否指向同一对象

  • python不同对象元素存储方式不一样
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]

在这里插入图片描述

3.8 浅拷贝和深拷贝

浅拷贝:只拷贝最外层,内层还是引用

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]]

3.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]

总结

  1. 使用原始字符r

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

  2. 重现随机数
    import random
    x=random.getstate()
    random.randomint(1,10)
    Random.getstate(x)

  3. Python中0.1+0.2>0.3
    引入demical(十进制)

  4. 短路逻辑:
    从左往右,只有当第一个操作数的值无法确定逻辑运算结果时,才对第二个操作数进行求值。

  5. range()函数
    range(stop)
    range(start,stop)
    range(start,stop,step)

  6. 列表:


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

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

    3. 清除clear()

    4. 修改

  7. 排序
    sort()
    reverse()
    sort(reverse=true)

  8. 查找:
    count()统计个数
    index()查看索引下标
    index(元素,start,end)
    copy()=num[:]

猜你喜欢

转载自blog.csdn.net/chairon/article/details/107896464