1.help()和dir()函数
help()函数与dir()函数都是帮助函数:
help()函数能够提供详细的帮助信息,dir()函数仅是简单的罗列可用的方法。
2.基础数据结构
基础数据类型:数值型、布尔型和字符串型。
2.1 数值型数据有整型(int)和浮点型(float)两种。
数值型数据的计算方法:
加 x+y
减 x-y
乘 x*y
除 x/y
幂次方 x**3
加等 x += 2 在x原有值的基础上再加2
减等 x -= 2 在x原有值的基础上减2
2.2 布尔型数据只有两种类型:True 和 False
2.3 字符串类型
字符串类型用单引号或双引号引起,引号内的内容即为字符串类型(str)。
s1 = 'hello world'
s2= "hello world"
len(s1) = 11 # 字符串计数包含空格
long_str = ' I am a teacher. '
# 去掉字符串左右两边的空格
long_str.strip() -> 'I am a teacher.'
# 去掉字符串左边的空格
long_str.lstrip() -> 'I am a teacher. '
# 去掉字符串右边的空格
long_str.rstrip() -> ' I am a teacher.'
# 将字符串中的teacher替换为student
long_str.strip().replace('teacher', 'student')
->I am a student.
num_str = '123456'
num_str.isdigit() # 判断变量是否为数值型变量
字符串切片
str = 'hello'
# str 01234
# str -5-4-3-2-1
str[1:4] -> 'ell' # 序数从0开始,右区间为开区间
str[1:] -> 'ello'
str[:3] -> 'hel'
# 逆序数
s[-5:] -> 'hello'
s[-4:-1] -> 'ell'
格式化输出
num_s = 100
num_t = 3
test_str = &#