考研准备第二天:Python基础(2)
2、数据类型与转换
2-1、数据类型
2-1-1、字符串
2-1-2、整数
2-1-3、浮点数
2-2、数据拼接
2-2-1、type()函数
作用:查询数据类型
例:print(type(“查询的内容”))
2-3、数据转换
2-3-1、str()函数
将其他类型转化为字符型,也可以用引号’’,进行转化为字符串。
例:num=123,转化成字符串:str(num)或num=’123’
2-3-2、int()函数
将其他类型转化为整型。
注意:int函数不能将文字型字符串和浮点数字串转化成整数型。
(例:’6h’和’3.6’不能被int转化,’666’则可以)
2-3-3、float()函数
将其他类型转化为浮点型。float()函数也可以将整数和字符串转化为浮点类型,
(例:float(3),float(’3’))
3、判断
3-1、条件判断
3-1-1、单向判断:if
a=6
#为a赋值
if a>=6:
#条件:如果a大于等于6
print(‘你拥有了毁灭宇宙的力量’)
3-1-2、双向判断:if…else…
a=6
#为a赋值
if a>=6:
#条件:如果a大于等于6
print(‘1’)
else:
#条件:如果a小于6
print(‘2’)
3-1-3、多向判断:if…elif…else…
a=6
#为a赋值
if a>=6:
#条件:如果a大于等于6
print(‘1’)
elif 4<a<5
#条件:如果a小于5大于4
print(‘2’)
else:
#其它
print(‘3’)
3-2、if嵌套
historyscore=26
if historyscore>=60:
print(‘你已经及格’)
if historyscore>=80:
print(‘你很优秀’)
else:
print(‘你只是一般般’)
else:
print(‘不及格’)
if historyscore<30:
print(‘学渣’)
else:
print(‘还能抢救一下’)
print(‘程序结束’)
注意:当碰到if条件嵌套时,先要理清谁与谁同级,谁是大分支,谁是小分支。
4
4-1、input()函数
例:a=input(‘请输入名字:’)
注意:运行后必须要在终端给input函数进行赋值
4-3、input()函数的数据结构
例:a=input(‘请输入名字:’)
‘a’的数据类型是字符串
4-4、input()函数结果的强制转换
例:a=int(input(‘请输入名字:’))
‘a’就变成了整数类型了