Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

Python Tutorial

Python HOMEPython IntroPython Get StartedPython SyntaxPython OutputPython CommentsPython VariablesPython Data TypesPython NumbersPython CastingPython StringsPython BooleansPython OperatorsPython ListsPython TuplesPython SetsPython DictionariesPython If...ElsePython MatchPython While LoopsPython For LoopsPython FunctionsPython RangePython ArraysPython IteratorsPython ModulesPython DatesPython MathPython JSONPython RegExPython PIPPython Try...ExceptPython String FormattingPython NonePython User InputPython VirtualEnv

Python Classes

Python OOPPython Classes/ObjectsPython __init__ MethodPython self ParameterPython Class PropertiesPython Class MethodsPython InheritancePython PolymorphismPython EncapsulationPython Inner Classes

File Handling

Python File HandlingPython Read FilesPython Write/Create FilesPython Delete Files

Python Modules

NumPy TutorialPandas TutorialSciPy TutorialDjango Tutorial

Python Matplotlib

Matplotlib IntroMatplotlib Get StartedMatplotlib PyplotMatplotlib PlottingMatplotlib MarkersMatplotlib LineMatplotlib LabelsMatplotlib GridMatplotlib SubplotMatplotlib ScatterMatplotlib BarsMatplotlib HistogramsMatplotlib Pie Charts

Machine Learning

Getting StartedMean Median ModeStandard DeviationPercentileData DistributionNormal Data DistributionScatter PlotLinear RegressionPolynomial RegressionMultiple RegressionScaleTrain/TestDecision TreeConfusion MatrixHierarchical ClusteringLogistic RegressionGrid SearchCategorical DataK-meansBootstrap AggregationCross ValidationAUC - ROC CurveK-nearest neighbors

Python DSA

Python DSALists and ArraysStacksQueuesLinked ListsHash TablesTreesBinary TreesBinary Search TreesAVL TreesGraphsLinear SearchBinary SearchBubble SortSelection SortInsertion SortQuick SortCounting SortRadix SortMerge Sort

Python MySQL

MySQL Get StartedMySQL Create DatabaseMySQL Create TableMySQL InsertMySQL SelectMySQL WhereMySQL Order ByMySQL DeleteMySQL Drop TableMySQL UpdateMySQL LimitMySQL Join

Python MongoDB

MongoDB Get StartedMongoDB Create DBMongoDB CollectionMongoDB InsertMongoDB FindMongoDB QueryMongoDB SortMongoDB DeleteMongoDB Drop CollectionMongoDB UpdateMongoDB Limit

Python Reference

Python OverviewPython Built-in FunctionsPython String MethodsPython List MethodsPython Dictionary MethodsPython Tuple MethodsPython Set MethodsPython File MethodsPython KeywordsPython ExceptionsPython Glossary

Module Reference

Built-in ModulesRandom ModuleRequests ModuleStatistics ModuleMath ModulecMath Module

Python How To

Remove List DuplicatesReverse a StringAdd Two Numbers

Python Examples

Python ExamplesPython CompilerPython ExercisesPython QuizPython ServerPython SyllabusPython Study PlanPython Interview Q&APython BootcampPython CertificatePython Training

Pythonrange


Python range

The built-inrange() function returns an immutable sequence of numbers, commonly used for looping a specific number of times.

This set of numbers has its own data type calledrange.

Note: Immutable means that it cannot be modified after it is created.

Creating ranges

Therange() function can be called with 1, 2, or 3 arguments, using this syntax:

range(start,stop,step)


Call range() With One Argument

If the range function is called with only one argument, the argument represents thestop value.

Thestart argument is optional, and if not provided, it defaults to 0.

range(10) returns a sequence of each number from 0 to 9. (The start argument, 0 is inclusive, and the stop argument, 10 is exclusive).

Example

Create a range of numbers from 0 to 9:

x = range(10)
Try it Yourself »

Call range() With Two Arguments

If the range function is called with two arguments, the first argument represents thestart value, and the second argument represents thestop value.

range(3, 10) returns a sequence of each number from 3 to 9:

Example

Create a range of numbers from 3 to 9:

x = range(3, 10)
Try it Yourself »

Call range() With Three Arguments

If the range function is called with three arguments, the third argument represents thestep value.

The step value means the difference between each number in the sequence. It is optional, and if not provided, it defaults to 1.

range(3, 10, 2) returns a sequence of each number from 3 to 9, with a step of 2:

Example

Create a range of numbers from 3 to 9:

x = range(3, 10, 2)
Try it Yourself »

Using ranges

Ranges are often used infor loops to iterate over a sequence of numbers.

Example

Iterate over each value in a range:

for i in range(10):
  print(i)
Try it Yourself »

Using List to Display Ranges

The range object is a data type that represents an immutable sequence of numbers, and it is not directly displayable.

Therefore, ranges are often converted to lists for display.

Example

Convert different ranges to lists:

print(list(range(5)))
print(list(range(1, 6)))
print(list(range(5, 20, 3)))
Try it Yourself »

Slicing Ranges

Like other sequences, ranges can be sliced to extract a subsequence.

Example

Extract a subsequence from a range:

r = range(10)
print(r[2])
print(r[:3])
Try it Yourself »
Note: The first print statement returns the value at index 2, and the second print statement returns a new range object, from index 0 to 3.

Membership Testing

Ranges support membership testing with thein operator.

Example

Test if the numbers 6 and 7 are present in a range:

r = range(0, 10, 2)
print(6 in r)
print(7 in r)
Try it Yourself »

The return value isTrue when the number is present in the range, andFalse when it is not.


Length

Ranges support thelen() function to get the number of elements in the range.

Example

Get the length of a range:

r = range(0, 10, 2)
print(len(r))
Try it Yourself »



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp