Range() Function | Development | Online Course With Appy Pie Academy

# range() function

##### Lesson Details:
June 29, 2020

I: Introduction

Python is an easy-to-learn, versatile and powerful programming language that is available on all platforms, including Windows, Mac OS X, Linux, Unix, Android and IOS. It can be used to develop desktop apps, Web apps, games and other projects.

II: Body

A: Range() function

Python offers its users a number of built-in functions. One of the most important functions for beginners is the range() function. The range(n) function returns a list of integers starting from 0 to n-1. Let’s see how it works in Python programming.

# Defining variables N = 5 print ( range ( N )) # Printing the list [0, 1, 2, 3, 4]

Range(N) is one of the many built-in functions available in Python. It takes one argument called n which can be any integer value. The range(n) function returns a list containing n elements starting from 0 to n-1. So, in the above example, we have defined variable N to be 5. Next, we printed the list containing elements starting from 0 to 4 using the print statement.

B: For Loop

One of the most commonly used control structure in programming is the for loop. It is used to iterate through a set of values in a definite order. Let’s see how it works in Python programming.

# Defining variables for i in range ( N ): print ( i ) # Printing '0', '1', '2', '3', '4'

In the above example, we have defined variable N to be 5. Next, we have used a for loop with a range(N) function to print elements from 0 to 4. In this way, we have been able to print elements from a given range.

C: Using If Statement

We can use if statements to make decisions based on whether a condition is true or false. Let’s see how it works in Python programming.

# Defining variables N = 5 if N % 2 == 0 : print ( "Sum is Even" ) elif N % 2 == 1 : print ( "Sum is Odd" ) else : print ( "Sum is Neither" ) # Printing 'Sum is Neither'

In the above example, we have defined variable N to be 5. We then used an if statement along with a logical operator % . In this case, we have used % to check if the remainder of the division of N by 2 would equal 0 or 1 or neither. Next we checked whether the number was even or odd. If the condition was true , it would print “Sum is Even” and if not it would print “Sum is Odd”. If the condition was neither it would print “Sum is Neither”. You can learn more about logical operators here.

D: While Loop

Another commonly used control structure in programming is the while loop. It is used to iterate through a set of values in a definite order until a condition becomes false . Let’s see how it works in Python programming.

# Defining variables for i in range ( 5 ): print ( i ) # Printing '0', '1', '2', '3', '4' while True : i += 1 # Printing '5', '6', '7', '8', '9'

In this example, we have defined variable N to be 5. Inside the while loop , we printed numbers from 0 to 4 and incremented i by 1 each time it executed. As long as the condition is True , it would keep printing numbers and incrementing i by 1 each time before exiting the loop . This will continue to do so until the condition becomes False .

III: Conclusion