Computer Science

Python Range Function

The Python range function is used to generate a sequence of numbers within a specified range. It is commonly used in for loops to iterate over a sequence of numbers. The range function takes parameters for start, stop, and step, and returns a range object that represents the sequence of numbers within the specified range.

Written by Perlego with AI-assistance

3 Key excerpts on "Python Range Function"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Bioinformatics Algorithms
    eBook - ePub

    Bioinformatics Algorithms

    Design and Implementation in Python

    • Miguel Rocha, Pedro G. Ferreira(Authors)
    • 2018(Publication Date)
    • Academic Press
      (Publisher)

    ...Python offers a function range to generate an immutable sequence of integers between a start and a stop value, with an increment step value. The general syntax range ([ start, ], stop, [, step ]) allows a more compact notation where only the stop value needs to be provided. In that case, the start value is assumed to be zero and step to be one. By considering a step with a negative value, sequences of decreasing values can be generated. Note that in the generated sequence of values, the stop value is not included: The following example iterates through a string and prints pairs of values with the index and the respective character found in that position: The enumerate function returns an iterable object that simultaneously provides access to the index and the respective element. Thus, the previous code can be re-written as follows: Although all previous examples here focus on iterating over strings, similar examples can be put forward considering lists. As an example, we will develop a function, similar to the one presented in the previous section, where in this case we take a list and an element, and return all positions where the element occurs in the list (as another list). If the element does not occur, the result will be the empty list. These cycles can also be nested to work with multi-dimensional data structures. A simple example are matrices, that are normally processed by nesting two for cycles, one iterating over row indexes and the other over columns. The following example provides a script that creates a matrix and uses this strategy to calculate the sum of all its elements. In some situations, it may be necessary to alter the expected flow within the loop (including both for and while loops). Python provides two statements for loop control. The break statement forces an immediate exit of the loop...

  • Hands on Data Science for Biologists Using Python
    • Yasha Hasija, Rajkumar Chakraborty(Authors)
    • 2021(Publication Date)
    • CRC Press
      (Publisher)

    ...With a “break” statement, the program departs from the loop immediately, and the rest of the statements outside the loop block or body are executed. Code: # Braking a loop before its end drug_name = ['Metformin', 'Acarbose', 'Canagliflozin','Dapagliflozin'] for temp in drug_name: print(temp) if temp = = 'Canagliflozin': break Output: Metformin Acarbose Canagliflozin As the “if” condition is satisfied when “temp” variable is equal to “Canagliflozin”, the execution of the “break” statement blocks the printing of “Dapagliflozin”. Range in Python We can use a Python built-in function range() to execute a series of statements for a given number of times. Let us consider the application of the range() function for loops before we talk of “functions” in detail in the subsequent section. The “range” function produces a series from 0 to the number before the number in the “range”. Code: # range function for i in range(5): print(i) Output: 0 1 2 3 4 The range() function offers a convenient way to repeat or iterate some tasks for a certain number of times. Code: for i in range(3): print('I love biology') Output: I love biology I love biology I love biology By default, the range begins from 0. The range function can also start from any number, and this is done by defining a starting parameter at a specific number. Code: for i in range(6,10): print(i) Output: 6 7 8 9 Functions In programming, a sequence of statements that do particular work can be clubbed into a function. Functions are useful because these do not repeat the same statements in doing a particular task. Functions are a block of statements that perform a task and have a name so that we can identify them later. A function can also take arguments and returns some values. Let us define a simple function below: Code: def BioLove(): print('I love Biology..!') BioLove() Output: I love Biology..! This is a simple function that just prints a few things...

  • Deep Learning in Practice

    ...It is a determined loop in which the programmer defines the loop iteration at the beginning of the code. Its syntax is as follows: for a variable in sequence: # lines codes execute in every loop … # lines code after the loop … In total, there are two types of for : for each (goes through every item in the sequence("iterable")) range-based for loop (goes through every item in a range) for range (), these are the parameters: range () generates a sequence of numbers based on some parameters. range (int start, int stop, int step) start : begins at, step : change each time, stop : up to but not including. There are three types for range () : single parameterrange ➔ ending value, two parametersrange ➔ starting value, ending value, three parametersrange ➔ starting value, ending value, and increment. And there are two types of changes: positive increment moves forward, negative increment moves backward. 2.1.7 While Loop It is an undetermined loop that its iteration checks in the block of the loop. This is the syntax: (The block of the loop will continue until the conditions are true) while condition: statements … 2.1.8 String The string is one of the data types that consist of some pieces, which are characters. It is a sequence of characters (there is an empty string if its length is zero). Mathematical operations do not work on strings even if their forms are like numbers (Except for + and *, which concatenates two strings and repetition). The characters can be accessed by their index. From left to right, it starts at 0, and for accessing from right to left, it starts with –1. The string is an object in python that, same as other objects, has some methods. For example, upper and lower are the methods that convert a string to the uppercase and lowercase characters...