x: 10, 12, 6, 8, 9, 11, 13, 13, 5, 0, 1 refer to exhibit 4-3. what is the mean?
Python range() function returns the sequence of the given number between the given range.
range() is a built-in role of Python. Information technology is used when a user needs to perform an activeness a specific number of times. range() in Python(3.x) is simply a renamed version of a function called xrange in Python(2.x). The range() part is used to generate a sequence of numbers.
Python range() function for loop is unremarkably used hence, knowledge of same is the fundamental aspect when dealing with whatever kind of Python code. The most mutual use of range() part in Python is to iterate sequence blazon (Python range() List, cord, etc. ) with for and while loop.
Python range syntax
range(finish)
range(outset, stop[, stride])
Python range() Nuts
In uncomplicated terms, range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the part, user tin can decide where that series of numbers will begin and end too every bit how big the difference will be betwixt i number and the next.range() takes mainly three arguments.
- 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. The range of integers terminate at stop – 1.
- step: integer value which determines the increment between each integer in the sequence
Example of Python range() methods
Example ane: Sit-in of Python range()
Python3
for
i
in
range
(
10
):
print
(i, end
=
" "
)
impress
()
fifty
=
[
x
,
20
,
thirty
,
forty
]
for
i
in
range
(
len
(l)):
impress
(50[i], cease
=
" "
)
impress
()
sum
=
0
for
i
in
range
(
ane
,
11
):
sum
=
sum
+
i
impress
(
"Sum of first ten natural number :"
,
sum
)
Output :
0 i 2 3 4 5 6 vii 8 9 10 xx thirty 40 Sum of first 10 natural number : 55
There are iii ways you can call range() :
- range(stop) takes one argument.
- range(start, stop) takes two arguments.
- range(get-go, stop, step) takes 3 arguments.
range(stop)
When user telephone call range() with ane argument, user will get a serial of numbers that starts at 0 and includes every whole number up to, but non including, the number that user have provided every bit the stop. For Example –
Example two: Demonstration of Python range(stop)
Python3
for
i
in
range
(
x
):
print
(i, stop
=
" "
)
print
()
for
i
in
range
(
20
):
print
(i, end
=
" "
)
Output:
0 ane 2 3 four 5 6 vii 8 ix 0 i 2 3 four v vi 7 8 9 ten 11 12 13 14 xv sixteen 17 xviii 19
range(start, terminate)
When user call range() with two arguments, user go to decide not only where the series of numbers stops merely also where it starts, so user don't accept to start at 0 all the time. User can utilize range() to generate a serial of numbers from X to Y using a range(X, Y). For Example -arguments
Example 3: Demonstration of Python range(start, terminate)
Python3
for
i
in
range
(
1
,
20
):
impress
(i, finish
=
" "
)
print
()
for
i
in
range
(
five
,
xx
):
print
(i, end
=
" "
)
Output:
1 two three 4 5 6 7 8 nine 10 11 12 13 fourteen 15 sixteen 17 18 19 five 6 vii 8 9 10 11 12 13 14 15 16 17 18 xix
range(start, stop, pace)
When the user phone call range() with three arguments, the user tin can choose not only where the series of numbers will commencement and finish simply likewise how big the difference will exist between one number and the next. If the user doesn't provide a step, then range() will automatically deport equally if the step is 1.
Case four: Demonstration of Python range(start, finish, step)
Python3
for
i
in
range
(
0
,
30
,
iii
):
print
(i, cease
=
" "
)
impress
()
for
i
in
range
(
0
,
50
,
5
):
print
(i, end
=
" "
)
Output :
0 iii half-dozen 9 12 15 18 21 24 27 0 v 10 15 twenty 25 xxx 35 40 45
In this example, we are printing an fifty-fifty number between 0 to 10 so we choose our starting point from 0(start = 0) and stop the serial at ten(stop = ten). For printing even number the difference between one number and the next must be 2 (step = 2) after providing a step nosotros get a following output ( 0, two, 4, viii).
Example v: Incrementing with the range using positive pace
If a user wants to increment, then the user needs steps to be a positive number. For example:
Python3
for
i
in
range
(
2
,
25
,
2
):
print
(i, stop
=
" "
)
impress
()
for
i
in
range
(
0
,
xxx
,
four
):
print
(i, end
=
" "
)
print
()
for
i
in
range
(
xv
,
25
,
3
):
print
(i, end
=
" "
)
Output :
2 4 six 8 10 12 14 16 18 xx 22 24 0 4 8 12 16 xx 24 28 xv 18 21 24
Example 6: Python range() backwards
If a user wants to decrement, then the user needs steps to exist a negative number. For instance:
Python3
for
i
in
range
(
25
,
2
,
-
2
):
print
(i, finish
=
" "
)
impress
()
for
i
in
range
(
xxx
,
one
,
-
4
):
print
(i, terminate
=
" "
)
print
()
for
i
in
range
(
25
,
-
6
,
-
three
):
print
(i, end
=
" "
)
Output :
25 23 21 19 17 15 13 11 ix 7 5 3 thirty 26 22 18 14 10 6 2 25 22 19 sixteen 13 ten 7 iv 1 -2 -five
Example 7: Python range() float
Python range() role doesn't support the float numbers. i.e. user cannot utilise floating-bespeak or non-integer number in any of its statement. Users tin use merely integer numbers. For instance
Python3
for
i
in
range
(
iii.3
):
print
(i)
for
i
in
range
(
5.five
):
print
(i)
Output :
for i in range(iii.three): TypeError: 'float' object cannot be interpreted every bit an integer
Example eight: Concatenation of ii range() functions
The effect from two range() functions can be concatenated by using the chain() method of itertools module. The chain() method is used to print all the values in iterable targets one after another mentioned in its arguments.
Python3
from
itertools
import
chain
print
(
"Concatenating the result"
)
res
=
chain(
range
(
five
),
range
(
10
,
20
,
ii
))
for
i
in
res:
print
(i, stop
=
" "
)
Output:
Concatenating the result 0 1 2 3 iv 10 12 14 16 18
Instance 9: Accessing range() with alphabetize value
A sequence of numbers is returned past the range() function as its object that can be accessed past its index value. Both positive and negative indexing is supported by its object.
Python3
ele
=
range
(
x
)[
0
]
print
(
"First element:"
, ele)
ele
=
range
(
ten
)[
-
ane
]
print
(
"\nLast element:"
, ele)
ele
=
range
(
x
)[
4
]
print
(
"\nFifth chemical element:"
, ele)
Output:
Start chemical element: 0 Last element: 9 Fifth element: 4
Points to remember most Python range() function :
- range() function only works with the integers i.e. whole numbers.
- All arguments must be integers. Users tin not pass a string or float number or whatsoever other type in a outset, terminate and step argument of a range().
- All three arguments tin be positive or negative.
- The pace value must non exist zilch. If a step is zero python raises a ValueError exception.
- range() is a type in Python
- Users tin access items in a range() past index, just as users do with a list:
Source: https://www.geeksforgeeks.org/python-range-function/
0 Response to "x: 10, 12, 6, 8, 9, 11, 13, 13, 5, 0, 1 refer to exhibit 4-3. what is the mean?"
Post a Comment