li = ["alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]'''
li = ["alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]
li1 = []
for m in li:
b = m.strip().startswith('a')
if b == True :
li1.append(m.strip())
for n in li1:
print(n)2、语法格式
print("实例一:起始值为1,结束值为10")
for i in range(1,10):
print(i,end='')
print("\n实例二:结束值为10")
for i in range(10): print(i,end='')
print("\n实例三:结束值为10,步长为2")
for i in range(1,10,2):
print(i,end='')
TypeError: ‘float‘ object cannot be interpreted as an integer 呢?
原因是range只能生成整数,不能生成float类型,使用 numpy的 arange函数来解决:
import numpy as np
for i in np.arange(0.1,0.5,0.05):
print(i) # 0.1,0.15,0.2,...,0.4,0.45, 不包含0.5!
# 或者 l = list(np.arange(0.1,0.5,0.05))4、range()函数需要注意的
① 它表示的是左闭右开区间;
② 它接收的参数必须是整数,可以是负数,但不能是浮点数等其它类型;
'''判断指定的整数 在序列中是否存在 in ,not in'''
print(10 in r) #False ,10不在当前的r这个整数序列中
print(9 in r) #true ,9在当前的这个r序列里
print(9 not in r) #false ,9不在当前的这个r序列里
③ 它是不可变的序列类型,可以进行判断元素、查找元素、切片等操作,但不能修改元素;
④ 它是可迭代对象,却不是迭代器。
# (1)左闭右开
>>> for i in range(3, 6):>>>
print(i,end=" ")3 4 5
# (2)参数类型
>>> for i in range(-8, -2, 2):>>>
print(i,end=" ")-8 -6 -4>>> range(2.2)----------------------------TypeError Traceback (most recent call last)...TypeError:
'float' object cannot be interpreted as an integer
# (3)序列操作
>>> b = range(1,10)>>> b[0]1>>> b[:-3]range(1, 7)>>> b[0] = 2TypeError Traceback (most recent call last)...TypeError:
'range' object does not support item assignment
# (4)不是迭代器
>>> hasattr(range(3),'__iter__')True>>>
hasattr(range(3),'__next__')False>>> hasattr(iter(range(3)),'__next__')True5、range对象是不可变序列
>>> range(2) + range(3)-----------------------------------------TypeError Traceback (most recent call last)...TypeError: unsupported operand type(s)
for +: 'range' and 'range' >>> range(2)*2-----------------------------------------TypeError Traceback (most recent call last)...TypeError: unsupported operand type(s)
for *: 'range' and 'int'
那么问题来了:同样是不可变序列,为什么字符串和元组就支持上述两种操作,而偏偏 range 序列不支持呢?
虽然不能直接修改不可变序列,但我们可以将它们拷贝到新的序列上进行操作啊,为何 range 对象连这都不支持呢?
官方文档的解释:
...due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern.
原因是 range 对象仅仅表示一个遵循着严格模式的序列,而重复与拼接通常会破坏这种模式...
问题的关键就在于 range 序列的 pattern!仔细想想,其实它表示的就是一个等差数列,拼接两个等差数列,或者重复拼接一个等差数列,这就是为啥 range 类型不支持这两个操作的原因了。因此可以得出结论,任何修改行为都会破坏等差数列的结构,因此最好不要进行任何修改。
li = ["alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]'''
li = ["alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]
li1 = []
for m in li:
b = m.strip().startswith('a')
if b == True :
li1.append(m.strip())
for n in li1:
print(n)2、语法格式
print("实例一:起始值为1,结束值为10")
for i in range(1,10):
print(i,end='')
print("\n实例二:结束值为10")
for i in range(10): print(i,end='')
print("\n实例三:结束值为10,步长为2")
for i in range(1,10,2):
print(i,end='')
TypeError: ‘float‘ object cannot be interpreted as an integer 呢?
原因是range只能生成整数,不能生成float类型,使用 numpy的 arange函数来解决:
import numpy as np
for i in np.arange(0.1,0.5,0.05):
print(i) # 0.1,0.15,0.2,...,0.4,0.45, 不包含0.5!
# 或者 l = list(np.arange(0.1,0.5,0.05))4、range()函数需要注意的
① 它表示的是左闭右开区间;
② 它接收的参数必须是整数,可以是负数,但不能是浮点数等其它类型;
'''判断指定的整数 在序列中是否存在 in ,not in'''
print(10 in r) #False ,10不在当前的r这个整数序列中
print(9 in r) #true ,9在当前的这个r序列里
print(9 not in r) #false ,9不在当前的这个r序列里
③ 它是不可变的序列类型,可以进行判断元素、查找元素、切片等操作,但不能修改元素;
④ 它是可迭代对象,却不是迭代器。
# (1)左闭右开
>>> for i in range(3, 6):>>>
print(i,end=" ")3 4 5
# (2)参数类型
>>> for i in range(-8, -2, 2):>>>
print(i,end=" ")-8 -6 -4>>> range(2.2)----------------------------TypeError Traceback (most recent call last)...TypeError:
'float' object cannot be interpreted as an integer
# (3)序列操作
>>> b = range(1,10)>>> b[0]1>>> b[:-3]range(1, 7)>>> b[0] = 2TypeError Traceback (most recent call last)...TypeError:
'range' object does not support item assignment
# (4)不是迭代器
>>> hasattr(range(3),'__iter__')True>>>
hasattr(range(3),'__next__')False>>> hasattr(iter(range(3)),'__next__')True5、range对象是不可变序列
>>> range(2) + range(3)-----------------------------------------TypeError Traceback (most recent call last)...TypeError: unsupported operand type(s)
for +: 'range' and 'range' >>> range(2)*2-----------------------------------------TypeError Traceback (most recent call last)...TypeError: unsupported operand type(s)
for *: 'range' and 'int'
那么问题来了:同样是不可变序列,为什么字符串和元组就支持上述两种操作,而偏偏 range 序列不支持呢?
虽然不能直接修改不可变序列,但我们可以将它们拷贝到新的序列上进行操作啊,为何 range 对象连这都不支持呢?
官方文档的解释:
...due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern.
原因是 range 对象仅仅表示一个遵循着严格模式的序列,而重复与拼接通常会破坏这种模式...
问题的关键就在于 range 序列的 pattern!仔细想想,其实它表示的就是一个等差数列,拼接两个等差数列,或者重复拼接一个等差数列,这就是为啥 range 类型不支持这两个操作的原因了。因此可以得出结论,任何修改行为都会破坏等差数列的结构,因此最好不要进行任何修改。