python基础--数据类型简介

一、简述python 的五大数据类型的作用、定义方式、使用方法

1、数字类型

整型

​ 作用:用于描述事物的整数数量,如身高、年龄

​ 定义方式:

​ age = 18

​ height = 180

​ 使用方法:+ - * / % // **

浮点型

​ 作用: 用于描述事物带小数的数量,如薪资、长度

​ 定义方式:

​ salary = 5.5

​ length = 18.5

​ 使用方法:+ - * / % // **

2、字符串

​ 作用:描述事物的特征,如名字、爱好等

​ 定义方式:

​ name = ‘allen’

​ 使用方法:+ 、*、逻辑比较

3、列表

​ 作用:描述事物的多个特征

​ 定义方式:任意类型的数据,用逗号分隔,放入中括号

​ list1 = ['allen','read','run']

​ 使用方法:索引取值

​ print(list1[0])

4、字典

​ 作用:存放事物的多个特征及其描述信息

​ 定义方式:在{}内用逗号分隔开多个元素,每一个元素都是key: value的格式,其中value是任意格式的数据类型,key由于具有描述性的作用,所以key通常是字符串类型。

​ 使用方法:按key取值

5、布尔型 bool

​ 作用:条件判断时用于表示结果,条件成立则为True,条件不成立则为False

​ 定义:True、False一般不会直接引用,需要时一般经过逻辑判断得到

​ 使用方法:

​ print(type(True)) # 打印结果显示为:<class 'bool'>

​ print(bool(0)) # 打印结果显示为:False

​ print(bool('allen')) # 打印结果显示为:True

​ print(1>2) # 打印结果显示为:False

​ print(1<2) # 打印结果显示为:True

二、一行代码实现下述代码实现的功能

x = 10
y = 10
z = 10

链式赋值:

x = y = z = 10

三、写出两种交换 x、y值的方法

1、中转法

x = 10
y = 20
z = x
x = y
y = z
print(x,y)

2、交叉赋值法

x = 10
y = 20
x,y = y,x
print(x,y)

四、一行代码取出allen的第2、3个爱好

allen_info_dict = {
'name':'allen',
'age':'18',
'height':180,
'weight':140,
'hobby_list':['read','run','music','code'],
}
print(allen_info_dict['hobby_list'][1:3])

五、使用格式化输出的三种方式实现以下输出

name = 'allen'
height = 160
weight = 140
# "My name is 'allen', my height is 180, my weight is 180"

1、占位符

print("My name is %s, my height is %d, my weight is %d" %(name,height,weight))

2、format 格式化

print('My name is {}, my height is {}, my weight is {}'.format(name,height,weight))

3、f-String 格式化

print(f'My name is {name}, my height is {height}, my weight is {weight})'

posted @ 2019-09-10 19:44  AllenCH  阅读(194)  评论(0编辑  收藏  举报