Hướng dẫn how do you assign a range in python? - làm thế nào để bạn chỉ định một phạm vi trong python?

Đó là bởi vì range trả về một đối tượng phạm vi trong Python 3. Đặt nó vào list để làm cho nó làm những gì bạn muốn:

>>> Var1 = range(10, 50)
>>> print(list(Var1))
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,  
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
>>>

x = phạm vi (3, 20, 2) cho n trong x: & nbsp; in (n)

Thí dụ

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3

Tạo một chuỗi các số từ 3 đến 5 và in từng mục trong chuỗi:

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 returns an immutable sequence of numbers that can be easily converted to lists, tuples, sets etc.


x = phạm vi (3, 6) cho n trong x: & nbsp; in (n)

Hãy tự mình thử »

range(start, stop, step)

Tạo một chuỗi các số từ 3 lên 19, nhưng tăng thêm 2 thay vì 1:

x = phạm vi (3, 20, 2) cho n trong x: & nbsp; in (n)


❮ Chức năng tích hợp sẵn

Hàm

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 trả về một chuỗi các số giữa phạm vi cho.

Lưu ý:

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 Trả về một chuỗi các số bất biến có thể dễ dàng chuyển đổi thành danh sách, bộ dữ liệu, bộ, v.v.0 up to the number (but not including the number).

# numbers from 0 to 3 (4 is not included)
numbers = range(4)
print(list(numbers))    # [0, 1, 2, 3]

# if 0 or negative number is passed, we get an empty sequence
numbers = range(-4)
print(list(numbers))    # []

Cú pháp của phạm vi ()

Hàm

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 có thể mất tối đa ba đối số:

Các tham số

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
3 và
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
4 trong
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 là tùy chọn.

# numbers from 2 to 4 (5 is not included)
numbers = range(2, 5)
print(list(numbers))    # [2, 3, 4]

# numbers from -2 to 3 (4 is not included)
numbers = range(-2, 4)    
print(list(numbers))    # [-2, -1, 0, 1, 2, 3]

# returns an empty sequence of numbers
numbers = range(4, 2) 
print(list(numbers))    # []

Bây giờ, hãy xem cách # create a sequence of numbers from 0 to 3 numbers = range(4) # iterating through the sequence of numbers for i in numbers: print(i) # Output: # 0 # 1 # 2 # 3 0 hoạt động với số lượng đối số khác nhau.

Ví dụ 1: phạm vi () với đối số dừng

  • Nếu chúng ta chuyển một đối số duy nhất cho
    # create a sequence of numbers from 0 to 3
    numbers = range(4)
    
    # iterating through the sequence of numbers
    for i in numbers:
        print(i)
    
    # Output:
    
    # 0
    # 1
    # 2
    # 3
    
    0, điều đó có nghĩa là chúng ta đang vượt qua đối số
    # create a sequence of numbers from 0 to 3
    numbers = range(4)
    
    # iterating through the sequence of numbers
    for i in numbers:
        print(i)
    
    # Output:
    
    # 0
    # 1
    # 2
    # 3
    
    8.
  • Trong trường hợp này,
    # create a sequence of numbers from 0 to 3
    numbers = range(4)
    
    # iterating through the sequence of numbers
    for i in numbers:
        print(i)
    
    # Output:
    
    # 0
    # 1
    # 2
    # 3
    
    0 trả về một chuỗi các số bắt đầu từ 0 đến số (nhưng không bao gồm số).
  • Ví dụ 2: phạm vi () với các đối số bắt đầu và dừng

Nếu chúng ta chuyển hai đối số cho

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0, điều đó có nghĩa là chúng ta đang thông qua các đối số
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
3 và
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
8.

# numbers from 2 to 10 with increment 3 between numbers
numbers = range(2, 10, 3)
print(list(numbers))    # [2, 5, 8]

# numbers from 4 to -1 with increment of -1
numbers = range(4, -1, -1)    
print(list(numbers))    # [4, 3, 2, 1, 0]

# numbers from 1 to 4 with increment of 1
# range(0, 5, 1) is equivalent to range(5)
numbers = range(0, 5, 1) 
print(list(numbers))    # [0, 1, 2, 3, 4]

Trong trường hợp này,

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 trả về một chuỗi các số bắt đầu từ
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
3 (bao gồm) lên đến
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
8 (độc quyền).
The default value of
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
3 is 0, and the default value of
# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
4 is 1. That's why
# numbers from 0 to 3 (4 is not included)
numbers = range(4)
print(list(numbers))    # [0, 1, 2, 3]

# if 0 or negative number is passed, we get an empty sequence
numbers = range(-4)
print(list(numbers))    # []
2 is equivalent to
# numbers from 0 to 3 (4 is not included)
numbers = range(4)
print(list(numbers))    # [0, 1, 2, 3]

# if 0 or negative number is passed, we get an empty sequence
numbers = range(-4)
print(list(numbers))    # []
3.


Ví dụ 3: phạm vi () với các đối số bắt đầu, dừng và bước

Nếu chúng ta vượt qua cả ba đối số,

# iterate the loop 5 times
for i in range(5):
    print(i, 'Hello')
0 Hello
1 Hello
2 Hello
3 Hello
4 Hello

❮ Chức năng tích hợp sẵn


Thí dụ

Tạo một chuỗi các số từ 0 đến 5 và in từng mục trong chuỗi:

x = phạm vi (6) cho n trong x: & nbsp;in (n)
for n in x:
  print(n)

Hãy tự mình thử »


Định nghĩa và cách sử dụng

Hàm

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 trả về một chuỗi các số, bắt đầu từ 0 theo mặc định và tăng thêm 1 (theo mặc định) và dừng trước một số được chỉ định.


Cú pháp

Giá trị tham số

Tham sốSự mô tả
bắt đầuKhông bắt buộc.Một số nguyên chỉ định tại vị trí bắt đầu.Mặc định là 0
dừng lạiYêu cầu.Một số nguyên chỉ định tại vị trí dừng (không bao gồm).
bươcKhông bắt buộc.Một số nguyên chỉ định sự gia tăng.Mặc định là 1

Nhiều ví dụ hơn

Thí dụ

Tạo một chuỗi các số từ 3 đến 5 và in từng mục trong chuỗi:

x = phạm vi (3, 6) cho n trong x: & nbsp;in (n)
for n in x:
  print(n)

Hãy tự mình thử »

Thí dụ

Định nghĩa và cách sử dụng

Hàm

# create a sequence of numbers from 0 to 3
numbers = range(4)

# iterating through the sequence of numbers
for i in numbers:
    print(i)

# Output:

# 0
# 1
# 2
# 3
0 trả về một chuỗi các số, bắt đầu từ 0 theo mặc định và tăng thêm 1 (theo mặc định) và dừng trước một số được chỉ định.
for n in x:
  print(n)

Hãy tự mình thử »

❮ Chức năng tích hợp sẵn