1. Python的基本数据类型
1.1 数字类型
整型(关键字int) 由0~9组成的所有数字,例如:年龄、存款、尺寸、手机号等。 浮点型(关键字float) 包含有小数点,例如:价格,重量 布尔型(关键字bool) 生活通过某种判断得到的真(True)、假(False)。例如: 1大于2是假,1大于0是真 复数(关键字complex) type(变量名):查看变量数据类型
案例:
# 根据业务场景来决定 酒店管理系统(整数),超市管理系统--价格(小数)
phone_price = 899
print(type(phone_price)) #<class 'int'>
f_price = 3.5895159690318690315
print(type(f_price)) #<class 'float'>
# 结合判断、循环使用
# 登录,判断用户名==admin并且密码==123456,登录成功,否则登录失败
bol = True
boll = False
print(type(bol)) #<class 'bool'>
# 复数:了解即可
age = 10+0j
print(type(age)) #<class 'complex'>
age1 = 0j
print(type(age1))
2. 字符串类型
2.1 字符串
字符串定义:凡是由引号引起来的内容就是字符串,引号可以是单引号,双引号,三引号。 单引号,双引号,三引号区别? 统计字符串的长度:len(变量名)
# 'python'是最容易入门的语言
str1 = "'python'是最容易入门的语言"
print(str1)
str2 = 'py"th"on是最容易入门的语言'
print(str2)
str3 = ''''python'是最容易入门的语言'''
print(str3)
str4 = ' 床前明月光,' \
' 疑是地上霜,' \
' 举头望明月,' \
' 低头思故乡 '
print(str4)
str5 = ''' 床前明月光,
疑是地上霜,
举头望明月,
低头思故乡'''
print(str5)
特殊字符的处理:不要记忆,只要对内容的颜色进行查看 \n : 换行 \t: tab 在特殊字符前面加上\,转义 在整个字符串前面加上r、R
str1 = '\thello \n python'
print(str1)
str2 = 'hello \\n python' # hello \n python
print(str2)
str3 = R'\thello \n python' # \thello \n python
print(str3)
url = r'E:\A测试\04 文件\01 Python编程技术\02 Python的基本数据类型'
print(url)
# 不同的数据类型是不能进行运算
num1 = '100'
str1 = 3
# print(num1 + str1)
print(num1 * str1)
总结: 字符串相加,字符串的拼接 字符串和整数相乘,字符串的多次输出