0% found this document useful (0 votes)
84 views

Python Program To Show Range Basics

The document discusses using the range() function in Python to iterate over sequences of numbers in for loops. It provides 4 examples: 1) Printing numbers from 0-9, 2) Printing a list of numbers using its length as the range, 3) Printing natural numbers up to 20, and 4) Printing numbers divisible by 3 and 5 up to 30 and 50 respectively using the step argument to increment by 3 and 5. The range() function can take 1, 2, or 3 arguments to define the start, stop, and step values for the sequence of numbers it generates.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Python Program To Show Range Basics

The document discusses using the range() function in Python to iterate over sequences of numbers in for loops. It provides 4 examples: 1) Printing numbers from 0-9, 2) Printing a list of numbers using its length as the range, 3) Printing natural numbers up to 20, and 4) Printing numbers divisible by 3 and 5 up to 30 and 50 respectively using the step argument to increment by 3 and 5. The range() function can take 1, 2, or 3 arguments to define the start, stop, and step values for the sequence of numbers it generates.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Python Program to  show range() basics


  
# printing a number
for i in range(10):
    print(i, end =" ")
print()
  
# using range for iteration
l = [10, 20, 30, 40]
for i in range(len(l)):
    print(l[i], end =" ")
print()
  
# performing sum of natural
# number
sum = 0
for i in range(1, 11):
    sum = sum + i
print("Sum of first 10 natural number :", sum)

Output :

0 1 2 3 4 5 6 7 8 9 10 20 30 40 Sum of first 10 natural number : 55

There are three ways you can call range() :

 range(stop) takes one argument.


 range(start, stop) takes two arguments.
 range(start, stop, step) takes three arguments.

When user call range() with one argument, user will get a series of numbers that starts
at 0 and includes every whole number up to, but not including, the number that user
have provided as the stop. For Example –

2. Python program to print whole number using


range()
  
# printing first 10 
# whole number
for i in range(10):
    print(i, end =" ")
print()
  
# printing first 20
# whole number
for i in range(20):
    print(i, end = " ")

Output:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

3. Python program to print natural number using


range
  
# printing a natural
# number upto 20
for i in range(1, 20):
    print(i, end =" ")
print()
  
# printing a natural
# number from 5 t0 20
for i in range(5, 20):
    print(i, end =" ")

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19

4. When user call range() with three arguments, 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 user don’t provide a step, then range() will
automatically behave as if the step is 1.

brightness_4

# Python program to  print all number divisible by 3 and 5


   
# using range to print number
# divisible by 3
for i in range(0, 30, 3):
    print(i, end = " ")
print()
  
# using range to print number
# divisible by 5
for  i in range(0, 50, 5):
     print(i, end = " ")
      

Output :

0 3 6 9 12 15 18 21 24 27 0 5 10 15 20 25 30 35 40 45

You might also like