range function in python
range() function in python
range() function in python
In this exercise, you will practice using Python's built-in function range().
syntax for range() function is as below :
range(start,stop,step)Remember that you can use range() in a few different ways:
1) Create a sequence of numbers from 0 to a stop value (which is exclusive). This is useful when you want to create a simple sequence of numbers starting at zero:
range(stop)
# Example
list(range(11))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
when you want to create a sequence of numbers that increments by some value other than one. For example, a list of even numbers:
range(start, stop, step)
# Example
list(range(2,11,2))
[2, 4, 6, 8, 10]Lets learn by doing hands on
# Create a range object that goes from 0 to 5
nums = range(6)
print(type(nums))
# Convert nums to a list
nums_list = list(nums)
print(nums_list)
# Create a new list of odd numbers from 1 to 11 by unpacking a range object
nums_list2 = [*range(1,12,2)]
print(nums_list2)Explanation to the above code
The range() function generates a sequence of numbers starting from 0 by default and stops before the specified number, hence range(6) generates numbers from 0 to 5. The type of the nums variable is printed to confirm that it is a range object.
Next, the nums range object is converted into a list. This is done by passing nums to the built-in Python function list(), which converts the input into a list. The nums_list variable now holds a list of numbers from 0 to 5.
Finally, a new list of odd numbers from 1 to 11 is created. This is done by calling range() with three arguments: 1 (start), 12 (stop), and 2 (step). The range() function generates a sequence of numbers starting from the start value, stopping before the stop value, and incrementing by the step value. Therefore, range(1,12,2) generates odd numbers from 1 to 11. The star character (*) is used to unpack the range object into a list directly, without needing to call the list() function. The nums_list2 variable now holds a list of odd numbers from 1 to 11.
Another example : calculates the square root of numbers in the range from 0 to 999 (inclusive)
from math import sqrt
nums = {int(sqrt(x)) for x in range(1000)}
print(nums) # Prints "{0, 1, 2, 3, 4, 5}Here's a step-by-step explanation of the code:
from math import sqrt: This line imports thesqrtfunction from themathmodule. Thesqrtfunction is used to calculate the square root of a number.nums = {int(sqrt(x)) for x in range(1000)}: This line is a set comprehension that iterates over the range of numbers from 0 to 999 (inclusive). For each numberxin this range, it calculates the square root usingsqrt(x), and then converts the result to an integer using theintfunction. The integer part of the square root is added to the setnums. Note that since the set stores unique values, any duplicate integer parts of square roots will be automatically discarded.print(nums): This line prints the contents of thenumsset. The set will contain integer values that correspond to the integer parts of square roots calculated for each number in the range. The output will look like:{0, 1, 2, 3, 4, 5}.
WE CANT HAVE NEGATIVE NUMBERS IN START AND STOP in range function


I'm a beginner sir
Can you please help with this...
my_list = [1,2,3,4,5,6,]
reduced_list = my_list [::2],
reduced_list[0] = 10
print(reduced_list)
Output=??
I'm a beginner sir
Can you please help with this...
my_list = [1,2,3,4,5,6,]
reduced_list = my_list [::2],
reduced_list[0] = 10
print(reduced_list)
Output=??