day04 作业

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

数字类型

整型

作用:描述年龄

定义方式:

x = 10
y = int('10')

使用方法:

+ - * / % // **

如果需要使用sin/cos 等函数,可以导入cmath模块, import cmath

浮点型

作用:描述薪资

定义方式:

x = 10.5

y = float(10)  # 10.0

使用方法:

+ - * / % // **

如果需要使用sin/cos 等函数,可以导入cmath模块, import cmath

字符串类型

作用:描述姓名

定义方式:

name = 'cwz'
name = "cwz"
name = '''name'''

使用方法:

s1 = 'rese'
s2 = 'zio'
print(s1 + s2)  # resezio
print(s2 * 5)   # zioziozioziozio

列表

作用:[]存储多个元素,用逗号隔开

定义方式:

lt = [12,3,4,5,[123,344,],'wewe',10]

使用方法:索引取值

lt = [12,3,4,5,[123,344,],'wewe',10]
print(lt[4][0])  # 123

字典

作用:{} 内存储多个值,以key(描述信息): value(值,可以是任意数据类型)形式存储

定义方式:

dic = {'name': 'cwz', 'age': 19}

使用方法:

dic = {'name': 'cwz', 'age': 19}
print(dic['name'])  # cwz

布尔型

作用:判断条件结果

定义方式:布尔类型只有两个值,True / False

使用方法:

print(1 > 2)  # False
print(1 < 2)  # True

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

'''
x = 10
y = 10
z = 10
'''

x = y = z =10

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

'''
x = 10
y = 20
'''

# 法1
x = 10
y = 20
z = x
x = y
y = z

# 法2
x = 10
y = 20
x, y = y, x

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

nick_info_dict = {
'name':'nick',
'age':'18',
'height':180,
'weight':140,
'hobby_list':['read','run','music','code'],
}

_, h2, h3, _ = nick_info_dict['hobby_list']

五、使用格式化输出的三种方式实现以下输出(name换成自己的名字,既得修改身高体重,不要厚颜无耻)

name = 'cwz'
height = 180
weight = 140

# "My name is 'Nick', my height is 180, my weight is 140"

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

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

print('My name is {}, my height is {}, my weight is {}'.format(name, height, weight))
原文地址:https://www.cnblogs.com/setcreed/p/11498144.html