zoukankan      html  css  js  c++  java
  • 人工智能学习笔记002-Python数据类型

    注:本笔记对应B站江灏老师的教学视频https://www.bilibili.com/video/BV1zE411V79p

    一、数值型

    (一)类型

    1.类型:整型int,浮点型float,复数complex

    2.查看类型:

    obj = 12
    print(type(obj))

    3.类型转换

    obj = 12
    obj = int(obj)          #转成整型
    print(type(obj))
    obj = float(obj)        #浮点型
    print(type(obj))
    obj = complex(obj)    #复数
    print(type(obj))

    (二)数值运算

    print(9//2)       #取整
    print(9%2)         #取余
    print(9**2)       #乘方
    
    

    二、字符型

    (一)方法

    使用单引号'或者双引号''括起来

    使用反斜杠转义特殊字符

    (二)转义字符

    print("你是?
    ")          #换行
    print("	你是?")          #制表符,(缩进?)
    print(r"你是?
    	")      #表示""里的内容不转义

    (三)字符串的索引

    1.正向索引:从0开始

    2.反向索引:从-1开始

    3.切片:包首不包尾

    4.跳步取值:[头下标:尾下标:步长]

     

    string = 'hello world!'
    print(string[0])        #第一个
    print(string[-1])       #倒数第一个
    print(string[1:5])      #第2到第5个
    print(string[1:7:2])    #第2 4 6个
    print(string * 2)       #重复操作,打印两个
    print(string + 'hello') #连接

    (四)用户输入字符串input

    string=input("Please input a string: ") #引号里用来提示用户
    print(string)

    (五)字符串格式化输出

    与C语言类似的。

    name = input('your name: ')
    age = input('your age: ')
    print('your name is %s,you are %s years old.'%(name,age))

    三、列表

    (一)索引

    1.正向索引
    2.反向索引
    3.切片
    4.跳步

    list1 = [1,2,3,4,5,6,7,8,9]
    print(list1[0])     #第一个
    print(list1[-1])    #倒数第一个
    print(list1[1:5])   #第2到第5个
    print(list1[1:7:2]) #第2 4 6个

    (二)添加

    1.尾部添加 list1.append(obj)
    2.找位置插入list1.insert(index,obj)

    list1 = [1,2,3,4,5,6,7,8,9]
    list1.append(0)     #尾部添加
    print(list1)
    list1 = [1,2,3,4,5,6,7,8,9]
    list1.insert(2,0)   #指定位置添加
    print(list1)

    (三)删除和更新

    1.删除某个指定指list1.remove(obj)
    2.删除某个位置的值del list1[index]
    3.改变某个位置的值list1[index] =obj

    list1 = [1,1,2,2,3,3,4,4]
    list1.remove(3)     #去除一个3
    print(list1)
    ​
    list1 = [1,1,2,2,3,3,4,4]
    del list1[2]        #删除第三个数
    print(list1)
    ​
    list1 = [1,1,2,2,3,3,4,4]
    list1[-1] = 5       #最后一个数字改成5
    print(list1)

    (四)列表嵌套及索引

    单层嵌套:

    a = [1,2,3]
    b = ['A','B','C']
    c = [a,b]
    ​
    print(c)
    print(c[1])         #第二个列表
    print(c[1][2])      #第二个列表的第三个值,C

    多层嵌套:

    list1 = [['x','y','z'],[1,2,['A','B']]]
    print(list1[1][2][0])   #第2个列表中的第三个列表中的第1个元素,A

    四、字典

    (一)介绍

    1.键值对的集合,无序
    2.一种可变容器模型,可储存任意类型对象,包括字典本身
    3.每个键值对(key:value)用冒号(:)分开
    4.每个对之间用逗号(,)分割
    5.整个字典包含在大括号中

    (二)索引查询

    dic ={
        'apple':'red',
        'mango':'orange',
        'barry':[5,4]}
    ​
    print(dic)
    print(dic['apple'])     #dec[key]
    print(dic['barry'][0])  #dic[key][]
    a1=dic.keys()           #dic.keys()
    print(a1)
    a2=dic.values()         #dic.values()
    print(a2)

    (三)增加和更新

    增加:dic['新的键名'] = 新的值
    更新:dic['需要更新的键名'] = 更新的内容

    dic ={
        'apple':'red',
        'mango':'orange',
        'barry':[5,4]}
    ​
    dic['blala'] = 'yellow'     #增加
    print(dic)
    dic['apple'] = 'redred'     #更新
    print(dic)

    (四)删除

    1.删除键值对del dic[index]
    2.删除整个字典del dic
    3.清空字典dic.clear()

    dic ={
        'apple':'red',
        'mango':'orange',
        'barry':[5,4]}
    ​
    del dic['apple']
    print(dic)
    dic.clear()
    print(dic)
    del dic
    print(dict)     #dict类型为字典

    (五)字典操作注意事项

    1.不允许同一个键出现两次,否则仅记住后赋值的键,值可出现多次。
    2.不可用列表(列表是可变的)即不能dic={['color']:red}这样
    3.计算字典元素个数len(dic)
    4.字符串形式输出字典str(dic)
    5.返回输入的变量类型

    dic ={
        'apple':'red',
        'mango':'orange',
        'barry':[5,4]}
    ​
    print(len(dic))
    print(str(dic))
    print(type(dic))

    五、元组

    (一)介绍

    1.与列表类似,但元组不能修改,元组通常作为常量使用
    2.元组使用小括号,列表使用方括号,字典使用大括号
    3.创建元组只需要再括号中添加元素,并使用逗号隔开即可。tup1=(数字,'字符串',(元组))

    (二)操作

    由于不能修改,所以只有查找这个操作,tup[需要查找的位置]

    tup1 = (3,'a',[4,2])
    ​
    print(tup1[2])      #第三个元素
    六、集合

    (一)介绍

    1.无序的元素序列
    2.可以使用大括号{},或者set{}创建集合
    3.创建一个空集合必须用 set,而非{},否则就成了空字典
    4.由于是无序,所以没有索引功能
    5.具有去重的作用
    6.只能使用成员操作in或not in来判断某元素是否在集合中

    set = {2,3,4,4,'a','b'}
    ​
    print(set)      #结果会发现去掉了多余的4
    print('hello' in set)
    print(2 in set)

    (二)操作

    1.增加:set.add(obj)
    2.删除:
    set.remove(obj)#obj只能是集合中已有的元素,否则报错
    set.discard(odj)#obj即使不是集合中元素,也不会报错
    set.pop()#从集合的末端剔除一个数据
    set.clear()#清空集合

    set = {2,3,4,'a','b'}
    ​
    set.add('c')    #加c
    print(set)
    set.remove('a') #删a
    print(set)
    set.discard('b')#删b
    print(set)
    set.pop()       #删最后一个元素为什么删了4,而非c呢
    print(set)
    set.clear()     #清空
    print(set)
  • 相关阅读:
    Centos PHP+Apache执行exec()等Linux脚本权限设置的详细步骤
    新版本的bettercap不好用, 如何安装和编译旧版本的bettercap
    iqiyi__youku__cookie_设置
    window系统命令行设置proxy----Setting a proxy for Windows using the command-line
    修改Electron的libcc(libchromiumcontent)源码,重新编译electron, 设置event.isTrusted为true
    apache配置伪静态Rewrite
    sql注入工具:sqlmap命令
    Reject insecure SameSite=None cookies
    Spring Session Session Max Inactive Interval
    nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
  • 原文地址:https://www.cnblogs.com/ltwen/p/12756151.html
Copyright © 2011-2022 走看看