Range() In for Loop
M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 7 min read
Syntax-2: Using range() function : The for loop combined with the range() function is commonly used for
iterating over a sequence of numbers. function uses three types of parameters, which are
Syntax:
for <variable Name> in range (<Start >, <stop>,<step>)
Parameters:
- Start or lower limit (optional) → Starting number of the sequence OR the starting value (default is 0).
- Stop or upper limit → Generate numbers up to OR the stopping value (but not including last number).
- step (Optional) → Difference between each number in the sequence OR the increment value (default is 1).
NOTE:
- All parameters must be integers. And can be positive or negative and The default step value will be +1
- The default lower limit begin with 0 and goes up till upper limit minus1
- If only one value in range() it means it is stop value and if two value in range it means it is start & stop value.
- If Three value given in range() it means it is start , stop and step value
Python use range ( ) function in three ways:
A. range (stop): By default, it starts from 0 and increments by 1 and ends up to stop, but not including stop value.
for i in range(4):
print(i) Output: 0 1 2 3
B. range(start, stop): It starts from the start value and up to stop, but not including stop value.
for i in range(2, 6):
print(i) Output: 2 3 4 5
C. range (start, stop, step): Third parameter specifies to increment or decrement the value by adding
or subtracting the value
for i in range(3, 8, 2):print(i) |
Output: 3 5 7Explanation: 3 is starting value, 8 is stop value and 2 is step value. First print 3 and increase it by 2, that is 5, again increase it by 2, that is 7 so the output can’t exceed stop-1 value that is 8 here. So, the output is 3, 5, 7 |
Example:
Statement |
Values Generated |
range(10)range(5,10)range(3,7)range(5,15,3)range(9,3,-1)range(10,1,-2) |
0,1,2,3,4,5,6,7,8,95,6,7,8,93,4,5,65,8,11,149,8,7,6,5,410,8,6,4,2 |
|
Q1: Write a Print the first 10 natural numbers using for loop. for i in range(1,11): print(i) |
Q 2: Write a Python program to print all the even numbers up to 20. for i in range(21): if i%2==0: print(i) |
|
Q3: Write a Python program to calculate the sum of all numbers from 1 to a given n number. n = int(input("Plz enter the number")) sum = 0 for i in range(1,n+1): sum+=i print(sum) |
Q 4: Write a Python program to calculate the sum of all the odd numbers upto 10. num = 10 sum = 0 for i in range(num): if i%2!=0: sum+=i print(sum) |
Q 5: Write a Python program to print a multiplication table of a given numbernumber = 5 for i in range(11): print (number," x",i," =",5*i) |
Q6: Write a Python program to count the total number of digits in a number.num = input("Plz Enter the Number") count=0 for i in num: count += 1 print(“Total digit in a Number is = “,count) |
Example 7: Write a Python program that accepts a word from the user and reverses it.str = input(“Please enter the word”) str1 = "" for i in str: str1 = i + str1 print(str1) |
Example 8: Write a Python program to check if the given string is a palindrome.str = input("Please enter the word") str1 = "" for i in word: str1 = i+str1 if(str == str1): print("The string", str,"is a Palindrome.") else: print("The string", str,"is NOT a Palindrome.") |
Example 9: Write a Python program to print all even numbers between 50 and 100, inclusive of bothfor i in range(50,101): if i%2==0: print(i,"",end=" ") |
Example 10: Write a Python program to print the following series- 1, 4, 9, 16, 25, ……….. 15 termsfor i in range(1,16): j=i*i print(j,"",end=" ") |
Example 11: Write a Python program to print a number which is divisible by 3 between 30-70for i in range(30,70,3): print(i,"",end=" ") OR for i in range(30,71): if i%3==0: print(i,"",end=" ") |
Example 12: Write a Python program count the number of character in a string. And character is enter by userc=0 str=input("plz enter the string") ch=input("Plz enter the character to be search") for i in str: if i==ch: c=c+1 print(" Entered String is :=",str) print("\n No. of", ch,"in the string are=",c) |
Example 13: Write a Python program to enter a character and print the position of its each occurrence in string.str=input("plz enter the string") ch=input("Plz enter the character to be search") pos=0 for i in str: pos=pos+1 if i==ch: print(ch, "is present at position:",pos) |
Example 14: Write a Python program to print the factorial of a number.num=int(input("Plz enter the character to be search")) fact=1 for i in range (1,num+1): fact=fact*i print("factorial of a number is :",fact). |