For Loops w/ Numeric Sequences
With studying Computer Science, we can use a lot of integers. By using range()
function along with a for loop
, we can start to analyze integer based sequences.
range()
function
range()
functionThe
range()
function can take 3 different arguments and it must have atleast 1 argument.The
range()
function is a function that generates an iterable sequence of integers in an arithmetic progression.
IMPORTANT NOTE: the result of a range()
function is not printable by itself.
It must be converted to string or a list if you want to see the entire sequence.
Different Behaviours of: range()
NOTE: [
means inclusive, (
means exclusive mathematically.
Read more at this link.
range(n)
returns a sequence of[0,n)
range(a,b)
returns a sequence of[a,b)
; if a == b, it is an empty sequencerange(start, end, interval_value)
returns a sequence of[start,end)
, but the sequence will approach the end value by adding each step with the interval value. if theinterval_value
is not specified, it is assumed to be 1.
NOTE: range()
never includes the last/ending value.
Examples of range()
For Loops with range()
range()
Since range()
returns an iterable sequence, we can look through each individual values in it with a for loop
.
Last updated