1.2数据类型

#计算机中的存储单位换算
#字节:byte 写作‘B’ 1B= 8b(bit)
#1024B = KB     1024KB = 1MB
#1024MB = 1GB   1024GB = 1TB


#进制转换
#计算机是基于2进制 基数是0 和 1逢2进一
#0b110100 = 2^5 + 2^4 + 2 ^2  = 52
#由于二进制稳定性高(只有0,1两种状态),技术上容易实现,与现实逻辑相吻合。这是
#使用二进制的主要原因

#数据类型
# Python中常见的数据类型有 整型(int)、浮点数(float)
#字符串(string)、列表(list)、元组(tuple)、集合(set)、字典(dict)
# float
b = 1.23456
print(b, type(b)) # 1.23456 <class 'float'>

#科学计数法
c = 1.23456e5
print(c) #123456.0

#复数(complex):
d = 9 + 6j
print(type(d)) #<class 'complex'>

#布尔(bool) 只有True或者False
print(3>2) #True
print(3<2) #False

#空类型(None)
print(None) #None

#原始字符
#在字符串格式前面添加一个'r'即可
f = r'hello\n world'
print(f) #hello\n world

#列表(list)
list1 = ['Python',1, 1.23456,'列表']
print(list1, type(list1)) #['Python', 1, 1.23456, '列表'] <class 'list'>
#list的下标是从0开始的,可以通过下标获取元素
print(list1[0]) #Python
#获取的下标不存在会报错
#print(list1[4]) #IndexError: list index out of range


#元组(tuple) 元组是不可以修改的
tuple1 = ( 1,2,3,4,5,6)
print(tuple1, type(tuple1)) #(1, 2, 3, 4, 5, 6) <class 'tuple'>
#同list一样, tuple 也是通过下标访问元素
print(tuple1[0]) #1
#单个元素的元组
tuple2 = (1,)
print(tuple2, type(tuple2)) # (1,) <class 'tuple'>

#集合 (set) 是无序的
set1 = {1,2,3,4,5,6}
set2 = {4,5,6,7,8,9}
print(set1, type(set1)) #{1, 2, 3, 4, 5, 6} <class 'set'>
#交集
print(set1 & set2) #{4, 5, 6}
#并集
print(set1 | set2) #1, 2, 3, 4, 5, 6, 7, 8, 9}
#差集
print(set1 - set2) # {1, 2, 3}
print(set2 - set1) # {8, 9, 7}
#定义空集合的正确方法
s = set()
print(s) #set()
#而不是
s = {}
print(s, type(s)) #{} <class 'dict'>

#字典(dict)
d = {'姓名:':'梦江','年龄':'18','职业:':'工程师'}
print(d,type(d)) #{'姓名:': '梦江', '年龄': '18', '职业:': '工程师'} <class 'dict'>
print(d['姓名:']) #梦江
#当访问不存在的键会报错
#print(d['age'])
#get方法可以获取value,有就返回,没有None
print(d.get('住址:')) #None
# 通过get添加默认值
print(d.get('住址:','北京市')) # 北京市
#统计元素出现的个数
print(len(d)) # 3 这里是键值对的个数
print(len(set1)) # 6




猜你喜欢

转载自blog.csdn.net/xc_lmh/article/details/81145632
今日推荐