0% found this document useful (0 votes)
23 views6 pages

New 14 Range

Uploaded by

jayrathore130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

New 14 Range

Uploaded by

jayrathore130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
SSi | Degaae se tunction; The Python range() function returns the sequence of the given number between the given range. The most common use of it is to iterate sequence type (Python range() List, string, etc. ) with for and while loop. Syntax of range() Syntax: range(start, stop, step) Parameter: 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 ends at stop - 1. Step: integer value which determines the increment between each integer in the sequence Example: # printing a number for i in range(0,10,2): print(i, end="") print( What is the use of the range function in Python frrsimpteterms-rangethattows the user to generate aseries of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where that series of numbers will begin and end as well as how big the difference will be between one number and the next.range() takes mainly three arguments. range(stop) takes one argument. range(start, stop) takes two arguments. range(start, stop, step) takes three arguments. SSi | Degaae Python range(stop) When the user call range() with one argument, the user will get a series of numbers that starts at 0 and includes every whole number up to but not including, the number that the user has provided as the stop. Python range( 6 ) EERE Example: Demonstration of Python range(sto, # printing first 6 # whole number print(i, end= print Python range(start, stap) When the user call range() with two arguments, the user gets to decide not only where the series of numbers stops but also where it starts, so the user don’t have to start at 0 all the time. Users can use range() to generate a series of numbers from X to Y using range(X, Y). Example: Demonstration of Python range(start, stop) # printing a natural # number from 5 to 20 fori in range(5, 20): print(i, end="") Python range(start, stop, step) When the user call range() with three arguments, the user can choose not only where the series of numbers will start and stop but also how big the difference will be between one number and the next. If the user doesn’t provide a step, then range() will automatically behave as if the step is 1. In this example, we are printing an even number between 0 to 10 so we choose our starting point from O(start = 0) and stop the series at 10(stop = 10). For printing an even number the difference between one number and the next must be 2 (step = 2) after providing a step we get the following output (0, 2, 4, 8). SSi | Degerat avsalelane-]a\-4=1 00H LOH) Example: Demonstration of Python range(start, stop, step) for i in range(0, 10, 2): print(i, end="") print() Python range() with Examples Example 1: Incrementing the range using a positive step If a user wants to increment, then the user needs steps to be a positive number. # incremented by 4 print() Example 2: Python range() using negative step If a user wants to decrement, then the user needs steps to be a negative number. #-incremented-by-2 for iin range(25, 2, -2): print(i, end="") print() . SSi | Degeta Example 3: Python range() with float Python range() function doesn’t support the float numbers. i.e. user cannot use floating-point or non-integer numbers in any of its argument. Users can use only integer numbers. # using a float number for i in range(3.3): print(i) #Type Error: ‘float’ object cannot be interpreted as an integer Example 4: Concatenation of two range() functions using itertools The result 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. from itertools import chain # Using chain method print("Concatenating the result") res = chain(range(5), range(10, 20, 2)) for iin res: print(i, enc Example 5: Accessing range() with an index value A sequence of numbers is returned by the range() function as its object that can be accessed by its index value. Both positive and negative indexing is supported by its object. -ele-=range(4.9)f9}— $$$ print("First element:", ele) ele = range(10)[-1] print(“\nLast element:", ele) ele = range(10)[4] SSi| Degetat print("\nFifth element:", ele) | Some Important points to remember about the Python range() function: range() function only works with the integers i.e. whole numbers. Allarguments must be integers. Users can not pass a string or float number or any other type ina start, stop and step argument of a range(). All three arguments can be positive or negative. The step value must not be zero. If a step is zero python raises a Value Error exception. range() is a type in Python Users can access items in a range() by index, just as users do with a list:

You might also like