PYTHON PROGRAMMING TUTORIAL: Range Function with example

in #python8 years ago

Introduction

Hello everybody! With this Post I'm starting a series of python basics with examples. In each post I will be taking about one function of python with suitable example.

What you will learn?

You will learn about the range function
You will learn how and where to use range function

Requirements

Basic knowledge of python
Python shell

What is range Function

First let's define what python range function is? In simple words it is define as a function used to generate list of numbers which is generally used to iterate over with for loops.
For example lets say we want to generate numbers between 4 & 20 . we will give a command
range(4,20)
The above command will generate [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] list of numbers
Screenshot (185).png
The last number (in above case 20 ) is not included.

Parameters of range function

The range function has three parameters namely
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step: Difference between each number in the sequence.
Note: all parameters must be integers.
If you don't give start and stop parameter then they are 0 & 1 respectively.
Screenshot (186).png

Example

Print Numbers Between 0 and 100 which are divisible by 3

For this example things need to be done
*we will use for loop to iterate over the generated list
*we will use if statement to check if number is divisible by 3
*We will print the generated list

for i in range(100): (this line means that we will now iterate over every number one by one )
if i%3==0: ( we are checking if number is divisible by 3)
print(i) (if condition is true it will print i, i.e the number)
Screenshot (187).png

That's it for this post guys for the next post I will be talking about how you can do this by defining the function.
I will
Thank You! for reading it.
If You like this post do follow for more awesome post on python programing.

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.079
BTC 63129.80
ETH 1662.47
USDT 1.00
SBD 0.42