Title here
Summary here
The range() function generates a sequence of numbers, commonly used for looping or generating lists with specific patterns.
range(i, j) Generates a sequence of numbers starting from i and ending before j.range(i, j)
# Produces the sequence: i, i+1, ..., j-1range(j) Starts from 0 and ends before j. This is equivalent to range(0, j).range(j)
# Like slice(:j), starts from 0, ends at j-1range():range(i, j, k)k for defining the step increment, useful for sequences with a specific pattern, like arithmetic progressions (AP).range(i, j, k)
# Produces: i, i+k, i+2k, ..., i+nkk. Ensure i > j when using a negative step.range(i, j, -1)
# i must be greater than j (i > j)range(0, len(l))
# Produces a valid range of indices for the list lrange() to a List:range() creates a range object, not a list. To convert it into a list, use the list() function:list(range(0, 5))
# Converts range to list: [0, 1, 2, 3, 4]even = list(range(2, 11, 2))
print(even)
# Output: [2, 4, 6, 8, 10]range() to Generate Sequencesrange() can be particularly useful in a variety of contexts, such as repeating actions or generating specific number sequences.
# Repeating the string "meow" 3 times:
print("meow" * 3) # Output: meowmeowmeowrange()# Using newline `\n`:
print("meow\n" * 3) # Output: meow (with new line after each "meow")print()# Removing the default newline after the print:
print("meow\n" * 3, end="") # Output: meowmeowmeow (without extra newline)nYou can use range() in a for loop to find all factors of a given number n:
def factors(n):
flist = []
for i in range(1, n + 1):
if n % i == 0:
flist.append(i) # Can also use flist = flist + [i]
return flist