python基本数据类型
首先需要你的电脑安装好了Python环境,并且安装好了Python开发工具。
python中的数据类型仅有int和float两种(没有如short,long,double之分)
这里查看各种情况的数据类型运用到了python中的type函数
print(type(1))
print(type(-1))
print(type(1.1111))
print(type(1+1))
print(type(1+1.0)) # 由于1.0为float类型,python将1+1.0自动转化为float类型
print(type(1*1))
print(type(1*1.0))
# python中的除法使用'/'结果为float类型,使用"//"为int类型
print(type(2/2))
print(type(2//2))
print(type(1//2)) # 与其他语言类似python中整除会忽略小数点后数字
运行结果:

小结:
# bool类型包括 True和 False两种
print(type(True))
print(type(False))
# 将bool类型转换为int类型
print(int(True))
print(int(False))
# python中0为假,非0为真(无论进制)
print(bool(1))
print(bool(0))
print(bool(2.2))
print(bool(0b10))
# 对字符串取布尔值
print(bool('abc'))
print(bool(''))
# 对列表取布尔值
print(bool([1,2,3]))
print(bool([]))
运行结果:

小结
True和False开头需大写
数字中:0为False,其他均为True;在其他类型中:空为False,非空为True
# 二进制标识符为 0b,打印输出其代表的十进制数
print(0b10)
print(0b11)
# 八进制标识符为 0o,打印输出其代表的十进制数
print(0o10)
print(0o11)
# 十六进制标识符为 0x,打印输出其代表的十进制数
print(0x10)
print(0x1F)
# 输入数字默认为十进制
print(10)
结果:

小结:需牢记各种进制的表示形式
# 转换为二进制
print(bin(10))
print(bin(0o7))
print(bin(0xE))
# 转换为八进制
print(oct(0b111))
print(oct(0x777))
# 转换为十进制
print(int(0b111))
print(int(0o777))
# 转换为十六进制
print(hex(888))
print(hex(0b111))
print(hex(0o7777))
运行结果:

print("Let't go")
print('Let't go') # 其中此语句会报错
运行结果:

两字符串间可相加拼接成一个字符串
字符串乘上一个数n,得到n个该字符串
# 字符串的运算
print("he"+"llo")
print("hello"*3)
结果

# 输出指定位置的字符
print("hello world"[0])
print("hello world"[1])
print("hello world"[2])
print("hello world"[-1])
print("hello world"[-2])
结果

其中i可为负数,代表获取倒数第i位的数字
print("hello world"[0:5])
print("hello world"[-5:11])
print("hello world"[-5:])

# 列表可存储的类型
print(type([1, 2, 3, 4, 5]))
print(type(["hello", 1, False]))
print(type([[1, 2], [3, 4], [True, False]])) # 嵌套列表

# 读取列表中的元素
print(["hello", "world"][0:]) # 和str类型的读取方式相同

# 列表的运算(和str的运算相似)
print(["hello", "world"] + ["hello", "world"])
print(["hello", "world"] * 3)

# 元组存储是数据类型
print(type((1, 2, 3, 4, 5)))
print(type((1, 2, "hello", [1, 2, 3], True)))
# 获取指定位置元素
print((1, 2, 3, 4)[2])
# 获取指定区域元素
print((1, 2, 3, 4)[1:])
print(type((1, 2, 3, 4)[1:])) # 返回类型为tuple
# 元组的运算
print((1, 2, 3, 4)+(5, 6))
print((1, 2, 3, 4)*2)

集合中的数据是无序的,故不能用下标进行访问
集合中的元素不重复
# 求两个集合的差集
print({1, 2, 3, 4, 5, 6} - {2, 3}) # '-'为求差集的符号
# 求两个集合的交集
print({1, 2, 3, 4, 5, 6} & {2, 3}) # '&'为求交集的符号
# 求两个集合的并集
print({1, 2, 3, 4, 5, 6} | {5, 6, 7}) # '-'为求差集的符号

>>> set()
# 字典类型的输入格式
print(type({1: 1, 2: 2, 3: 3}))
# 字典的使用
print({1:"Hello", 2:"world"}[2])

转义字符
