day 04

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

1.1 数字类型

作用:描述年龄/id号

定义方式:

tjx_age = 22
print(tjx_age)

使用方法:

x = 1
y = 2
print(x + y)
print(x - y)
print(x * y)
print(x / y)

1.2 字符串类型

作用:描述姓名/单个爱好/性别

定义方式:

name = 'tjx'
name = "tjx's"
name = "tjx"
name = '''
tjx
'''
name = """
tjx"""

使用方法

str1 ='t'
str2 = 'z'
print(str1+str2)
print(str1*10)

1.3 列表

作用:存储多个(任意数据类型)元素

定义方式:

num = 0
s = ''
it = []
lis = []

使用方法:

tjx_hobby_list = ['run','music','read',172,110]
print(tjx_hobby_list[1])
print(tjx_hobby_list[4])
print(tjx_hobby_list[-1])

1.5 布尔型

作用:用于判断条件结果

定义:True、False通常情况不会直接引用,需要使用逻辑运算得到结果。

使用方法:

print(type(True)
print(True)
<class 'bool'
True
print (bool(0))
print(bool('tjx'))
print(bool(1 > 2))
print(bool(1 == 1))
False
True
False
True

2. 一行代码实现下述代码实现的功能:

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

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

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

4.一行代码取出nick的第2、3个爱好

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

print(hobby_list[1][2])

5. 使用格式化输出的三种方式实现一下输出

name = 'tjx'
height = 172
weight = 110

# "My name is'tjx',my height is 172, my weight is 110"
print('my name is %s my height is %s my weight is%s' %(name, height,weight))

my name is tjx my height is 172 my weight is 110
print("hello,{}. {}.{}. ".format(name, height, weight))

hello, tjx. 172.110
print(f"hello,{name}.You are {height}.You are {weight}")

hello,tjx.You are 172.You are 110
posted @ 2019-09-10 16:50  T祥  阅读(86)  评论(0编辑  收藏  举报