Introduction to Python Programming (011) - Usage of range() function

Introduction to Python Programming (011) - Usage of range() function

The range() function is one of Python's built-in functions, used to generate a sequence of integers.

The usage syntax of the range() function is as follows:

range(stop)                 # 生成一个从 0 到 stop-1 的整数序列,步长为 1。
range(start, stop[, step])  # 生成一个从 start 到 stop-1 的整数序列,步长为 step。

说明:
(1)start 和 step 参数是可选的。如果不给定 start,默认为 0。如果不给定 step,默认为 1。
(2range() 函数生成的整数序列是一个生成器对象,可以通过遍历或者转化为列表等方式使用。

Example:

1. Generate an integer sequence from 0 to 9

for i in range(10):
    print(i)
    
list1=list(range(10))
print(list1)
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
0
1
2
3
4
5
6
7
8
9
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2. Generate an integer sequence from 2 to 6

for i in range(2,7):
    print(i)
    
tuple1=tuple(range(2,7))
print(tuple1)
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
2
3
4
5
6
(2, 3, 4, 5, 6)

3. Generate an even number sequence from 1 to 10

for i in range(2,11,2):
    print(i)
    
set1=set(range(2,11,2))
print(set1)
        
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
2
4
6
8
10
{
    
    2, 4, 6, 8, 10}

4. Generate a sequence of integers in reverse order from 10 to 1

for i in range(10,0,-1):
    print(i)
    
list1=list(range(10,0,-1))
print(list1)
            
运行结果如下:
10
9
8
7
6
5
4
3
2
1
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

5. Write a countdown program

The program calls the sleep() function of the time module to implement:

import time
for i in range(10,0,-1):
    print("倒计时:",i)
    time.sleep(1)  # 每次暂停1秒
                
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
倒计时: 10
倒计时: 9
倒计时: 8
倒计时: 7
倒计时: 6
倒计时: 5
倒计时: 4
倒计时: 3
倒计时: 2
倒计时: 1

6. Generate 10 random numbers

The program calls the random module. The functions of the random module are as follows:

(1) Rearrange the data randomly and disrupt the order of the data

The syntax format is as follows:

random.shuffle(seq)

For example:

import random
list1 = list(range(0,10))
print(list1)
random.shuffle(list1)
print(list1)
                
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 4, 3, 7, 8, 9, 2, 6, 1, 0]

(2) Select n random and non-repeating elements from the sequence

The syntax format is as follows:

random.sample(seq, n)

For example:

import random
list1 = list(range(0,10))
print(list1)
print(random.sample(list1,4))
print(type(random.sample(list1,4)))
                
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 2, 1]
<class 'list'>

(3) Randomly return any number of elements from the list

The syntax format is as follows:

random.choice(seq)

For example:

import random
list1 = list(range(0,10))
print(list1)
print(random.choice(list1))
print(type(random.choice(list1)))
                
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9
<class 'int'>

(4) Obtain a random number from the specified range by increasing the base.

The syntax format is as follows:

random.randrange([start,] stop [,step])
说明:
(1)范围包含 start,不包含 stop
(2)step 为步长,默认为1

For example:

import random
print(random.randrange(1,10)) # 相当于从[1,2,3,...,9]序列中返回一个随机数
print(random.randrange(20,40,2)) # 相当于从[20,22,24,...,38]序列中返回一个随机数
print(random.randrange(20)) # 相当于从[0,1,2,3,...,19]序列中返回一个随机数
                
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
4
26
4

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132135349