【python入门之基本数据类型】---基本数据类型(字典、布尔)【三】

发布时间 2023-11-28 19:40:01作者: Unfool

【五】字典类型(dict)

【1】作用

  • 如果我们需要用一个变量记录多个值,但多个值是不同属性的
    • 比如人的姓名、年龄、身高,用列表可以存,但列表是用索引对应值的,而索引不能明确地表示值的含义
  • 这就用到字典类型,字典类型是用key:value形式来存储数据
    • 其中key可以对value有描述性的功能,能够明确的描述详细信息

【2】定义

  • 大括号括起来,内部可以存放多个元素,元素与元素之间使用逗号隔开,是以K:V键值对的形式存储
    • K:
      • 是对V的描述性信息(一般情况是字符串)
    • V:
      • 真正的数据,其实相当于变量值,也是任意的数据类型
person_info = {'name': 'Jack', 'age': 18, 'height': 185.3, 'hobby': ["动漫", "小说"]}
print(person_info)  # {'name': 'Jack', 'age': 18, 'height': 185.3, 'hobby': ['动漫', '小说']}
print(type(person_info))  # <class 'dict'>

【3】使用

(1)字典取值(键取值)

  • 字典不能通过索引取值,只能通过字典的K取值
person_info = {'name': 'Jack', 'age': 18, 'height': 185.3, 'hobby': ["动漫", "小说"]}

# 字典取值(键取值)
name = person_info['name']
print(name)  # 输出结果:Jack

age = person_info['age']
print(age)  # 输出结果:18

(2)字典嵌套及取值

# 字典嵌套及取值
student1 = {'name': 'Tom', 'age': 18}
student2 = {'name': 'Jerry', 'age': 19}
classroom = {'student1': student1, 'student2': student2}

# 取值
student1_name = classroom['student1']['name']
print(student1_name)  # 输出结果:Tom

student2_age = classroom['student2']['age']
print(student2_age)  # 输出结果:19
小习题:
info = {
    'name': 'Jack',
    'addr': {
        '国家': '中国',
        'info': [666, 999, {'编号': 466722, 'hobby': ['read', 'study', 'music']}]
    }
}

# 1. music在大字典里的位置
d1 = info['addr']  
print(d1)
# {'国家': '中国', 'info': [666, 999, {'编号': 466722, 'hobby': ['read', 'study', 'music']}]}

# 2. music在小字典里的位置
d2 = d1['info']  
print(d2)
# [666, 999, {'编号': 466722, 'hobby': ['read', 'study', 'music']}]

# 3. music在列表里的位置
d3 = d2[2]  
print(d3)
# {'编号': 466722, 'hobby': ['read', 'study', 'music']}

# 4. music在小字典里的位置
d4 = d3['hobby']  
print(d4)
# ['read', 'study', 'music']

# 5. music在列表里的位置
d5 = d4[2]  
print(d5)
# music

# 整合
d6 = info['addr']['info'][2]['hobby'][2]
print(d6)
# music

##作业:    .get()情况可以多加注意,学习
取值:
print("my name is {} my contry is {} 我的编号 {} 我的爱好 {} 我的幸运数字 {}")

print(info['name'])
print(info['addr'].get('国家'))
print(info['addr'].get('info')[2].get('编号'))
print(info['addr'].get('info')[2].get('hobby')[1])
name = info['name']
country = info['addr'].get('国家')
id = info['addr'].get('info')[2].get('编号')
hobby = info['addr'].get('info')[2].get('hobby')[1]
lucky_id= info['addr'].get('info')[0]

print('my name is {}, my country is {}, my id is {}, my hobby is {}, my lucky number is {}'.format(name,country,id,hobby,lucky_id))


【六】布尔类型(bool)

【1】作用

  • 布尔类型用于表示逻辑值,只有两个取值:True 和 False。
  • 在编程中,布尔类型经常用于控制程序的流程,例如条件判断、循环等。

【2】定义

  • 布尔类型只有两个取值:True 和 False。在 Python 中,首字母必须大写
  • 布尔值的命名规范:结果可能是布尔值的情况,一般都采用 is 开头 命名
# 定义布尔类型
is_student = True
is_adult = False

【3】使用

(1)条件判断

  • 布尔类型常常用于条件判断,例如 if 语句中
# 使用布尔类型进行条件判断
is_raining = True    #定义一个值为true

if is_raining:       #判断定义的值
    print("Remember to bring an umbrella!")
else:
    print("Enjoy the weather!")

(2)比较运算

  • 布尔类型还可以用于表达式的判断,例如比较运算
# 使用布尔类型进行比较运算
x = 5
y = 10

is_greater = x > y
print(is_greater)  # 输出结果:False

【补充】Python中的真与假

  • 在 Python 中,布尔类型的 True 表示真,False 表示假。
  • 在条件判断和逻辑运算中,通常会使用布尔值来确定程序的执行流程。

(1)假的情况(False)

  • 布尔值为 False: 显而易见,False 本身就表示假。
is_false = False
if is_false:
    print("This won't be executed.")
else:
    print("This will be executed.")
  • 数字零: 数字类型中,整数或浮点数中的零被视为假。
zero_integer = 0
zero_float = 0.0

if zero_integer or zero_float:
    print("This won't be executed.")
else:
    print("This will be executed.")
  • 空字符串: 空字符串 '' 被视为假。
empty_string = ''

if empty_string:
    print("This won't be executed.")
else:
    print("This will be executed.")
  • 空列表、空字典、空集合等: 对于容器类型,如果它们为空,被视为假。
empty_list = []
empty_dict = {}
empty_set = set()

if empty_list or empty_dict or empty_set:
    print("This won't be executed.")
else:
    print("This will be executed.")

(2)真的情况(True)

  • 布尔值为 True: True 本身表示真。
is_true = True
if is_true:
    print("This will be executed.")
else:
    print("This won't be executed.")
  • 非零数字: 除了零之外的任何整数或浮点数都被视为真。
non_zero_integer = 42
non_zero_float = 3.14

if non_zero_integer and non_zero_float:
    print("This will be executed.")
else:
    print("This won't be executed.")
  • 非空字符串: 非空字符串被视为真。
non_empty_string = 'Hello, World!'

if non_empty_string:
    print("This will be executed.")
else:
    print("This won't be executed.")
  • 非空列表、非空字典、非空集合等: 如果容器类型中包含元素,被视为真。
non_empty_list = [1, 2, 3]
non_empty_dict = {'key': 'value'}
non_empty_set = {1, 2, 3}

if non_empty_list and non_empty_dict and non_empty_set:
    print("This will be executed.")
else:
    print("This won't be executed.")