rangev2 – Python范围类的新版本
range()
是Python的内置函数。当用户需要执行特定次数的操作时使用它。 range()
函数用于生成数字序列。但是范围产生的数字序列通常要么线性减少要么线性增加,这意味着它要么按特定常数递增或递减。
rangev2
模块提供了一个函数new_range()
,该函数还允许使用*, //, %
运算符生成序列,以便序列可以以指数方式变化。
该模块没有内置Python,因此可以通过在终端中键入以下命令来安装它。
pip install rangev2
Syntax: new_range(start, stop, step)
Parameters:
start – integer starting from which the sequence of integers is to be returned.
stop – integer before which the sequence of integers is to be returned.
step – string containing operand and operator.
示例 #1:
# Python Program to
# show rangev2 basics
#Importing module
import rangev2 as r2
# program to produce Geometeric progression using rangev2
for i in r2.new_range(2, 100, '*2'):
print(i, end=" ")
print()
# printing powers
for i in r2.new_range(2, 1000, '**2'):
print(i, end=" ")
print()
# printing divisions
for i in r2.new_range(100,1,'//3'):
print(i, end=" ")
输出:
2 4 8 16 32 64
2 4 16 256
100 33 11 3
示例 #2:
# Python program to produce Geometric progression using rangev2
import rangev2 as r2
a = '2' # First number of the geometeric progression
c = '100' # Enter the upper bond
b = '2' # Enter the common ration
print(r2.new_range(int(a),int(c),'*' + b).list)
输出:
[2,4,8,16,32,64]