【python小白到精通 | 课程笔记】第一章 初识Python(这几个小错误你会犯吗?)

文章目录

  • 【python小白到精通】第一章 初识Python
  • 数据类型
  • 解密和加密
  • 课后作业,错题记录
  • 第一组,关于赋值、变量和print
  • 第二组,关于字符串
  • 第四组,关于index(索引)操作
  • 第五组,关于list(列表)的操作
  • 第六组,关于dictionary(字典)的操作
  • 【python小白到精通】第一章 初识Python

    数据类型

    1. Number(数字)

    2. String(字符串)

    3. List(列表)

      列表里面甚至可以再装列表

    4. Tuple(元组)

      元组是不能修改的

    5. Set(集合)

      无序,无重复

    6. Dictionary(字典)

      感觉和C里面的散列表(哈希表)好像

    解密和加密

    加密文本使用三个引号的字符串:可以放好几行。

    例:

    # 首先准备好我们要转化的文本
    text = '''
    humans also deconstruct our bodies in other ways than science. for example, philosophies originating in buddhism regard the human physical body as an epiphenomenon. many, many beings, such as microorganisms, live in our bodies and influence what we are like and who we are.
    
    human, as a being, in turn, contains other beings (such as some microorganisms). are they other kinds of creatures? or are they part of us? when a man dies, they are still alive, or while they die or are replaced, a man would face some change. with such thinking, we will find that the boundary between human beings and external objects is blurred, and the boundary between life and death is blurred. thinking of that i have an abstract experience that i am merged with all things, and that all life in the world is one.
    '''
    print(text)
    

    疑惑:三个引号不是用来多行注释吗?是因为在NoteBook的环境中不一样?

    课后作业,错题记录

    第一组,关于赋值、变量和print

    1.

       a = 1
       b = 2
       c = a + b
    
       print(c = 3)
       # 3
    

    TypeError: 'c' is an invalid keyword argument for print()

    原因: python中的赋值运算符没有返回值。(x = y = 5不会报错是因为运算顺序是从左往右,先将 5 赋给 x,再将 5 赋给 y)。


    第二组,关于字符串

    2.

       a = '1'
       b = 2
       c = a + b
       # 3
    

    TypeError: can only concatenate str (not "int") to str


    第四组,关于index(索引)操作

    3.

       text = 'I love you | 我爱你 | 💗💗'
    
       print(text.find('我'))
       print(text.find('o'))
       print(text.find('you'))
       print(text.find('o', 5))
       # 13
       # 3
       # [7, 8, 9]
       # 不知道
    

    输出:

    13
    3
    7
    8
    

    解释:

  • text.find()对一个单词返回的是首字母的索引

  • 第四个,数字5表示从索引为5(不是指第5个)的位置开始找


  • 第五组,关于list(列表)的操作

    4.

    L = [1, 2, 3]
    L2 = ['a', 'b']
    L - L2
    # 1, 2, 3
    

    报错:TypeError: unsupported operand type(s) for -: 'list' and 'list'

    原因:list列表没有减法运算


    5.

    L = [1, 2, 3]
    L2 = ['a', 'b']
    L2.append(L)
    
    print(L2)
    # a, b, 1, 2, 3
    

    输出:

    ['a', 'b', [1, 2, 3]]
    

    解释:整个L被当作一个元素,加到了L2中


    6.

    L = [1, 2, 3]
    L2 = ['a', 'b']
    print(L2.append(L))
    print(L2)
    # a, b, 1, 2, 3
    

    输出:

    None
    ['a', 'b', [1, 2, 3]]
    

    解释:list.append()函数没有返回值。


    第六组,关于dictionary(字典)的操作

    7.

    score_dic = {'Sue': 98, 'Joe': 65, 'Danish': 77, 'Lucy': 78}
    score_dic_2  = {'Anny': 86, 'Tom': 95}
    dic = {**score_dic, **score_dic_2}
    
    print(dic)
    # 看不懂
    

    输出:{'Sue': 98, 'Joe': 65, 'Danish': 77, 'Lucy': 78, 'Anny': 86, 'Tom': 95}

    疑惑:**score_dic 是个什么奇怪的操作呢?

    那就输出来看看叭!

    例子:

    clf = {*score_dic}
    print(clf)
    clf = {**score_dic}
    print(clf)
    

    输出:

    {'Danish', 'Joe', 'Lucy', 'Sue'}
    {'Sue': 98, 'Joe': 65, 'Danish': 77, 'Lucy': 78}
    

    解释:python中* 可以用于解包操作,想详细了解可以查看这篇博客:Python星号表达式(*)用法详解


    Al Studio课程地址:Python从小白到精通
    今天的分享到这里就结束啦,感谢阅读!

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【python小白到精通 | 课程笔记】第一章 初识Python(这几个小错误你会犯吗?)

    发表评论