Home Backend Development Python Tutorial How to use range in python

How to use range in python

Jun 20, 2019 pm 01:39 PM
range

How to use range in python

Python3 range() function returns an iterable object (type is object), not a list type, so the list will not be printed when printing.

Python3 list() function is an object iterator, which can convert the iterable object returned by range() into a list, and the returned variable type is a list.

Python2 range() function returns a list.

Function syntax

range(stop)
range(start, stop[, step])
Copy after login

Parameter description:

start: Counting starts from start. The default is to start from 0. For example, range(5) is equivalent to range(0, 5);

stop: counts to the end of stop, but does not include stop. For example: range (0, 5) is [0, 1, 2, 3, 4] without 5

step: step size, default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

Instance

>>>range(5)
range(0, 5)
>>> for i in range(5):
...     print(i)
... 
0
1
2
3
4
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(0))
[]
>>>
Copy after login

has two parameters or three parameters (the second construction method) ::

>>>list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 2))
[0, 2, 4, 6, 8]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(1, 0))
[]
>>>
>>>
Copy after login

The above is the detailed content of How to use range in python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP returns all the values ​​in the array to form an array PHP returns all the values ​​in the array to form an array Mar 21, 2024 am 09:06 AM

This article will explain in detail how PHP returns all the values ​​of an array to form an array. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Using the array_values() function The array_values() function returns an array of all the values ​​in an array. It does not preserve the keys of the original array. $array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values ​​will be ["bar","qux"]Using a loop can Use a loop to manually get all the values ​​of the array and add them to a new

How to use Range function in Java How to use Range function in Java Apr 19, 2023 pm 11:49 PM

Preface In Java, the Range method is available in both IntStream and LongStream classes. In IntStream class, it helps to return the sequential value of IntStream within function parameter scope. In the method, startInclusive(inclusive) and endExclusive(exclusive) are the two parameters used along with the increment step size, as mentioned before, the start value will be included and the end value will be excluded. In the case of LongStream, the only difference is the addition of the LongStream value. Range Syntax Let us see the syntax of range method in Java. IntStream range

Explain in simple terms: Thoroughly understand the working principle of Go language range Explain in simple terms: Thoroughly understand the working principle of Go language range Mar 12, 2024 pm 02:18 PM

Go language is a concise and powerful programming language with unique design and features in many aspects. One of the most impressive features is the range keyword, which is used to iterate over data structures such as arrays, slices, maps, and channels. The flexibility and convenience of range make it easy to traverse complex data structures, but many people are confused about how it works. This article will explain how range works in a simple and in-depth way, and use specific code examples to help readers better understand. First, let's look at a simple example

Enormous Cadillac Escalade IQ offers 460 miles of range at up to $170,000 price Enormous Cadillac Escalade IQ offers 460 miles of range at up to $170,000 price Sep 30, 2024 am 06:15 AM

GM's electric take on the legendary Escalade just got its configurator up, and the price ranges from$130,000 to about $170,000 after incentives. This is more than nearly any other luxury electric vehicle, pickup, or SUV out there, including Tesla's C

How to use Python's range function How to use Python's range function May 16, 2023 pm 08:26 PM

1. What is the range() function? The range() function is Python's built-in function. It returns a series of consecutively added integers and can generate a list object. Most of them often appear in for loops and can be used as indexes in for loops. Small question practice: for..range Exercise 1: Use for loop and range to find all even numbers between 0 and 100, and append them to a list. list1=[]foriinrange(0,100,2):list1.append(i)print(list1)2: Use for loop and range to find the numbers between 0 and 50 that are divisible by 3, and append them to a list. list2

How to implement non-repeating random numbers in php range How to implement non-repeating random numbers in php range Oct 26, 2022 am 09:22 AM

Methods to implement non-repeating random numbers in php range: 1. "$result = array_slice($numbers,0,$num);" method; 2. "while (list(, $number) = each ($numbers)) {. ..}" method; 3. "array_slice($rand_array,0,$limit);" method; 4. "range(1,30);" method.

What does mysql range partition refer to? What does mysql range partition refer to? May 31, 2023 pm 08:40 PM

1. Based on the given interval boundary, obtain several continuous interval ranges, and allocate the data to different partitions according to the placement point of the partition key. Range partitioning is mainly used for partitioning date columns. 2. Range partitioning is implemented by using PARTITIONBYRANGE(expr). Where expr can be a certain column value, or an expression based on a certain column value and returns an integer value, such as YEAR (date). Example CREATETABLE`Order`(`id`INTNOTNULLAUTO_INCREMENT,`partition_key`INTNOTNULL,`amt`DECIMAL(5)NULL)PARTIT

Quickly master the efficient use skills of Go language range Quickly master the efficient use skills of Go language range Mar 12, 2024 pm 09:18 PM

Go language is an open source programming language developed and open sourced by Google. It has become more and more popular among programmers in recent years. In the Go language, range is a powerful and convenient iterator that can be used to traverse data structures such as arrays, slices, maps, and channels. This article will introduce how to quickly master the efficient use of range in Go language, and provide specific code examples to help readers better understand related concepts. 1. Traverse arrays and slices First, let’s look at how to use range to traverse arrays and slices.

See all articles