Python基本数据类型以及数据的运算

1.数据类型总括 ( c1.py )

# 代码:是现实世界在计算机世界中的映射
# 写代码:就是将现实世界中的事物用计算机语言来描述

# 序列:str、list、tuple      序列是有序的
# 集合  set                   集合是无序的

# Python基本数据类型
# Number,        eg:1,1.1,-1,-1.1,0
# bool           eg:False,True
# 字符串  str     eg:'hello',"hello"
# 列表    list    eg:[1,2],['he']
# 元组   tuple    eg:(1,2)
# 集合    set     eg:{1,2,3}
# 字典    dict    eg:{key1:value1,key2:value2}

# 数字类型:
    # int (整型) float (浮点型)  complex (复数)  bool (布尔类型,表示真和假)
    # 非零表示bool真,只有零表示bool假

# 数据类型 type
print(type(1))    #int
print(type(1.1))  #float

# 加减,为整型都是整型
print(type(1+1))  #int
print(type(1-1))  #int

# 整型 加(或减) 浮点型 为浮点型
print(type(1+1.0))  #float
print(type(1-1.0))  #float

print(type(1*1))  #int
# 除法为浮点型
print(type(1/1))  #float

# 整型 乘(或除) 浮点型 为浮点型
print(type(1*1.0))  #float
print(type(1/1.0))  #float

# 如果两个整型相除还想得到一个整型,用两个斜杠即可 //
    # 单斜杠 (除法,自动转为浮点型)
    # 双斜杠 (整除)
print(type(1/1))  #float  1.0
print(type(1//1))  #int   1
print(type(1//2))  #int   0

# complex (复数) 用 j 表示
print(36j)


# ~~~~~~~~~~~~~~~~数据类型小结~~~~~~~~~~~~~~~~~
# Python的基本数据类型

    # 1.数字(Number)
        # 1.1整型       int
        # 1.2浮点型     float
        # 1.3布尔型     bool
        # 1.4复数       complex
    # 2.组
        # 2.1序列   (有序,可以用下标索引来访问,切片操作(0:5))
            # 2.1.1字符串       str     (不可变)
            # 2.2.2列表         list
            # 2.2.3元组         tuple   (不可变) 
        # 2.2集合               set     无序,没有索引,不能切片   
        # 2.3字典               dic     key:value键值对是其基本概念
    

2.进制间的转换  (c2.py)

# 1.进制
    # 10进制:满10进1   0,1,2...8,9,10,11
    # 2进制:满2进1     0,1,10,11,100            (十进制的10和二进制的10不同)
    # 8进制:满8进1     0,1...6,7,10,11          (十进制的10和八进制的10不同)
    # 16进制:满16进1   0,1...9,A,B,C,D,E,F,10   (十进制的10和十六进制的10不同)

# 2.不同进制之间的转换
    # 2.1各个进制数的表示
        # 2.1.1二进制  (数字前边加0b)
print(0b10)
print(0b11)
        # 2.1.2八进制  (数字前边加0o)
print(0o10)
print(0o11)
        # 2.1.3十六进制  (数字前边加0x)
print(0x10)
print(0x11)
print(0x1F)

# 2.2进制间的转换
    # 2.2.1将别的进制的数转为二进制  bin()
print(bin(10))         #十进制转10为二进制
print(bin(0o7))        #八进制转0o7为二进制
print(bin(0xE))        #十六进制转0xE为二进制

    # 2.2.2将别的进制的数转为十进制  int()
print(int(0b11))       #二进制转0b11为十进制
print(int(0o77))       #十六进制转0o77为十进制

    # 2.2.3将别的进制的数转为十六进制  hex()
print(hex(888))        #十进制转0b11为十六进制
print(hex(0o777))      #八进制转0o777为十六进制

    # 2.2.3将别的进制的数转为八进制  oct()
print(hex(0b111))       #二进制转0b11为八进制
print(hex(0x777))       #十六进制转0b11为八进制

3.bool  (c3.py)

# bool (布尔类型) 

# 其它类型都有一种类型与之对应

# True False (首字母大写,否则会报错)

print(type(True))
print(type(False))
print(int(True))
print(int(False))
# 非零表示bool真,只有零表示bool假
print(bool(1))
print(bool(1.1))
print(bool(-1))
print(bool(0))

# 字符串
print(bool('abc'))
print(bool(''))

# 列表
print(bool([1,2,3]))
print(bool([]))

# 集合
print(bool({1,2,3}))
print(bool({}))

# None
print(bool(None))

4.字符串 str  (c4.py)

# 字符串 str

# 1.对于换行,可用成对的三个引号(单双都可以)
# ss1 = '''
#     hello
#     hello
# '''
# ss2 = 'hello\nhello'
# ss3 = '''hello\nhello'''
# ss4 = 'hello\nhello'
# print(ss1)
# print(ss2)
# print(ss3)
# print(ss4)

# 2.转义字符
# 2.1 \n : 换行 ; \r : 回车  ; \' :单引号  ;   \t : 横向制表符
# print('hello\\nhello')
# print('let\'s go')
# 2.2问磁盘路径,这里的\n需要转义
# print('C:\normal\normal')
# 2.2.1 转义字符
# print('C:\\normal\\normal')
# 2.2.2 如果路径比加长,每个都加转义字符斜杠,那么会使字符串特别长,这里还可使用 r (原样输出)
    # 当一个字符串前边加了 r 之后,他就不是一个普通字符串了,而是一个原始字符串
# print(r'C:\normal\normal')

# 3.字符串的运算
# 3.1 加:拼接 ; 乘:重复(乘几就表示重复几次)
# print('hellow '+'world')
# print('hellow'*3)

# 3.2 通过 [] 下标的方式,可以访问字符串中的某个字符 
    # 正向:从0开始;
    # 反向:从-1开始;   [-n]:从末尾数第n项
# print(len('hello world'))
# print(('hello world')[0])
# print(('hello world')[4])
# print(('hello world')[-1])
# print(('hello world')[6])
# print(('hello world')[-5])

# 3.3截取一段字符
    # (中括号包裹两个数字,两个数字之间通过冒号隔开)  通过 [起始位置:终了位置]
    # 注意:终了位置(应该再往后加一位,例如下例,到截取字符的下一位)
    # 负数表示  步长
    # 省略冒号后边:表示从冒号前边的那个开始一直到结束
    # 省略冒号前边:表示从冒号后边的那个数的前一位开始一直到最开始的位置
# print(('hello world')[0:5])   # hello
# print(('hello world')[0:-1])  # hello worl
# print(('hello world')[6:12])  # world
# print(('hello world')[6:])   # world
# print(('hello world')[-5:])  # world
# print(('hello pyton ruby #c java node')[6:])    # pyton ruby #c java node
# print(('hello pyton ruby #c java node')[-4:])   # node
# print(('hello pyton ruby #c java node')[:4])    # hell
# print(('hello pyton ruby #c java node')[:-4])   # hello pyton ruby #c java

# 3.4原始字符串 r/R (大小写都可以)

# print(r'C:\\Windows\node\nopad')
# print(R'C:\\Windows\node\nopad')

5.列表 list (c5.py)

# 组 (在Python中表示组的概念的方式有多种)

# 列表 list

# 1.定义
    # 组里的各个成员可以是 数字,字符串,bool,还可以是列表 (不限于这几种,还可加入其它类型)
# a1 = [1,2,3,4]
# print(type(a1))
# a2 = [[1,'hellow'],'hello',1,True]
# print(type(a2))

# 2.操作
# 2.1通过下标访问 (参考字符串的方式)
    # 2.1.1 如果下标只是一个数字,得到的是一个字符串
    # 2.1.2 如果下标是两个数字(用冒号隔开的),得到的是一个列表,即使改列表只有一个元素
# a3 = ['新月打击','苍白之瀑','月之降临','月神冲刺']
# print(a3[2])        # 月之降临
# print(a3[0:2])      # ['新月打击', '苍白之瀑']    
# print(a3[-1:])      # ['月神冲刺']

# 2.2运算
    # 2.2.1 加法(两个列表合并为一个列表)
    # 2.2.2 乘法(两个列表不能相乘;但列表和数字相乘,表示重复次数)
    # 2.2.3 减法(两个列表不能相减,会报错)

a4 = ['新月打击','苍白之瀑','月之降临','月神冲刺']
a5 = ['虚弱','点燃']
print(a4 + a5)          # ['新月打击', '苍白之瀑', '月之降临', '月神冲刺', '虚弱', '点燃']
print(a5*3)             # ['虚弱', '点燃', '虚弱', '点燃', '虚弱', '点燃']

# 组的
a6 = [['巴西','墨西哥','喀麦隆','罗克地亚'],[],[],[],[]]    # 列表的嵌套

6.元组 tuple  (c6.py)

# 元组 tuple

# 1.各个元素 也可以为   数字,字符串,bool
# a1 = (1,2,3,4)
# a2 = (1,'-1',False)
# print(type(a1))
# print(type(a2))

# 2.访问方式
    # 2.1通过[]下标的方式
a3 = (1,2,3)
a4 = (4,5,6)
# print(a3[0])

    # 2.2中括号中两个数字的方式
# print(a3[0:3])

    # 2.3两个元组相加
# print(a3+a4)

    # 2.4元组乘以一个整数 (重复整数次数)
# print(a3*3)

# 3.序列 :str、list、tuple   (一些共有的特性)       序列是有序的
    # 3.1访问  (可以铜鼓切片的方式  切片还可有第三个参数)
# print('hello world'[2])
# print([1,2,3,4,5][2])
# print([1,2,3,4,5][0:3])
# print([1,2,3,4,5][-1:])
# print('hello world'[0:8:2])

    # 3.2操作,有 +  和  *
    # 3.3判断一个数是否在某个list中,  使用  in(在)  not in(不在)  操作符 ,得到一个bool值结果
# print(1 in [1,2,3,4,5])
# print(1 not in [1,2,3,4,5])

    # 3.4获取列表的长度  使用  len()  函数
# print(len([1,2,3,4,5,6]))
# print(len('hello world'))

    # 3.5求一个最大值,使用    max()  函数  ;求 最小值,使用  min()  函数   (这时比较的是  ASCII 值的大小 ,ASCII值通过 ord() 函数获得 )
# print(max([1,2,3,4,5,6]))
# print(min([1,2,3,4,5,6]))
# print(max('helloworld'))
# print(min('helloworld'))
# print(ord('d'))
# print(ord('w'))

7.set  (c7.py)

# 集合是无序的

# set

# 不支持下标访问,不支持切片

# 1.定义:通过 {} 包裹
a1 = {1,2,3,4,5,6,}
# print(type(a1))

# 2.特性 
    # 2.1 无序     (不支持下标索引访问,不支持切片)
# print(a1[2])      #报错
# print(a1[0:2])    #报错
    # 2.2 不重复
# a2 = {1,1,1,2,2,2,3,3,3,}
# print(a2)        #{1, 2, 3}

    # 2.3支持长度判断 len()
# print(len(a1))

    # 2.4 支持 in 和 not in
# print(1 in a1)
# print(1 not in a1)

    # 2.5支持  求两个集合的  差集 (-)
# print({1,2,3,4,5,6}-{3,4})    #{1, 2, 5, 6}

    # 2.6支持  求两个集合的  交集 (&)
# print({1,2,3,4,5,6}&{3,4})    #{3, 4}

    # 2.7求两个集合的   并集 (|)
# print({1,2,3,4,5,6}|{3,4,7})    #{1, 2, 3, 4, 5, 6, 7}

# 2.8定义一个空的集合   并不能直接通过 一个空的花括号,而是应该通过set关键字
print(type({}))       #dict
print(type(set()))    #set 

8.字典 dict  (c8.py)


# 字典 dict  {key1:value1,key2:value2}

# 1.定义
    # 1.1很多个key和value,集合类型,字典有点像set,是无序的
# a1 = {1:1,2:2}
# print(type(a1))     #dict
    # 1.2对于字典的value:str,int,float,list,set,dict
    # 1.3对于字典的key可以:int,str  (必须为不可变类型)
    # 1.4键名为list报错,键名为tuple(元组)可以
# print({[1,2]:'hello'})
# print({(1,2):'hello'})
    # 1.5空的字典,用{}花括号表示,空的set(集合)须用set()来定义

a2 = {'Q':'新月打击','W':'苍白之瀑','E':'月之降临','R':'月神冲刺'}

# 2.操作
    # 2.1不能通过下标索引访问,不能通过切片,是无序的
    # 2.2通过key来访问value的形式,不能有相同的键key
# print(a2['Q'])
# a2 = {'Q':'新月打击','Q':'苍白之瀑','E':'月之降临','R':'月神冲刺'}
# print(a2)
# print(a2['Q'])
# a3 = {'1':'新月打击',1:'苍白之瀑'}
# print(type(a3))
    # 2.3这里的1是键值而非下标索引,数字和字符串是不同的
# print(a3[1])
# print(a3['1'])

(备注:以上内容来自七月老师的学习笔记,仅作为学习使用)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值