0% found this document useful (0 votes)
3 views6 pages

SPPU Python Range Programs

The document provides Python programming exercises for Savitribai Phule Pune University students, focusing on range-based programs. It includes tasks to print all odd and even numbers within a user-defined range, as well as to calculate the summation of all numbers in that range. Each exercise is accompanied by a problem statement, algorithm, sample code, and explanations of the concepts involved.

Uploaded by

koynangp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

SPPU Python Range Programs

The document provides Python programming exercises for Savitribai Phule Pune University students, focusing on range-based programs. It includes tasks to print all odd and even numbers within a user-defined range, as well as to calculate the summation of all numbers in that range. Each exercise is accompanied by a problem statement, algorithm, sample code, and explanations of the concepts involved.

Uploaded by

koynangp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SPPU – TE Electrical (2019 Course)

Python Programming – Range-Based Programs


Savitribai Phule Pune University | Reference: R6 – Learning Python with Raspberry Pi

Question 6: Print All ODD Numbers in a Range [9 Marks]

Problem Statement
Write a program in Python to accept lower and upper limit of a range of numbers from the user and
print all the odd numbers in that range.

Concept
• A number is odd if it is NOT divisible by 2, i.e., number % 2 != 0
• The program accepts two integers: lower limit and upper limit from the user
• A for loop iterates through all numbers in the range (inclusive of both limits)
• An if condition checks whether each number is odd and prints it

Algorithm
• Step 1: Accept lower limit from the user using input()
• Step 2: Accept upper limit from the user using input()
• Step 3: Convert inputs to integers using int()
• Step 4: Use a for loop from lower to upper+1 (to include upper limit)
• Step 5: Inside the loop, check if number % 2 != 0
• Step 6: If condition is true, print the number

Program
▶ Python Code:
# Program to print all Odd numbers in a given range # SPPU - TE Electrical
(2019 Course) # Accept lower and upper limits from the user lower =
int(input('Enter the Lower Limit: ')) upper = int(input('Enter the Upper
Limit: ')) print('\nOdd numbers between', lower, 'and', upper, 'are:') #
Iterate through the range for num in range(lower, upper + 1): if num % 2 !
= 0: # Check if number is odd print(num, end=' ') print() # For
new line at end

Sample Output
Input/Output — Example 1:
Enter the Lower Limit: 1 Enter the Upper Limit: 20 Odd numbers between 1 and
20 are: 1 3 5 7 9 11 13 15 17 19
Input/Output — Example 2:
Enter the Lower Limit: 5 Enter the Upper Limit: 15 Odd numbers between 5 and
15 are: 5 7 9 11 13 15

Explanation
The range(lower, upper + 1) function generates all integers from lower to upper inclusive. The
modulus operator % gives the remainder when divided by 2. If the remainder is not 0, the number is
odd. The end=' ' argument in print() places numbers on the same line separated by spaces.
Alternative Method – Using range() with step 2:
We can also use range() with a step of 2 to directly generate odd numbers:
lower = int(input('Enter the Lower Limit: ')) upper = int(input('Enter the
Upper Limit: ')) # Adjust start to nearest odd number if lower is even start
= lower if lower % 2 != 0 else lower + 1 print('\nOdd numbers between',
lower, 'and', upper, 'are:') for num in range(start, upper + 1, 2): # step =
2 print(num, end=' ') print()
Question 9: Print Summation of All Numbers in a Range [9 Marks]

Problem Statement
Write a program in Python to accept lower and upper limit of a range of numbers from the user and
print the summation of all numbers in that range.

Concept
• Summation means adding all integers from the lower limit to the upper limit (inclusive)
• A variable 'total' is initialized to 0 before the loop
• Each number in the range is added to 'total' inside the loop
• After the loop, the final sum is displayed
• Python also provides a built-in sum() function with range() for the same purpose

Algorithm
• Step 1: Accept lower limit from the user
• Step 2: Accept upper limit from the user
• Step 3: Convert both to integers
• Step 4: Initialize variable total = 0
• Step 5: Use a for loop from lower to upper+1
• Step 6: Add each number to total (total = total + num)
• Step 7: After the loop, print the value of total

Program
▶ Python Code:
# Program to print the Summation of all numbers in a given range # SPPU - TE
Electrical (2019 Course) # Accept lower and upper limits from the user lower
= int(input('Enter the Lower Limit: ')) upper = int(input('Enter the Upper
Limit: ')) # Initialize sum variable total = 0 # Iterate through the range
and accumulate sum for num in range(lower, upper + 1): total = total + num
# Add each number to total print('\nNumbers from', lower, 'to', upper)
print('Summation =', total)

Sample Output
Input/Output — Example 1:
Enter the Lower Limit: 1 Enter the Upper Limit: 10 Numbers from 1 to 10
Summation = 55
Input/Output — Example 2:
Enter the Lower Limit: 5 Enter the Upper Limit: 15 Numbers from 5 to 15
Summation = 110

Explanation
The loop runs from lower to upper (inclusive, hence upper + 1 in range). In each iteration, the
current number is added to total. After the loop completes, total holds the sum of all numbers in the
range.
Verification for Example 1: 1+2+3+4+5+6+7+8+9+10 = 55. This can also be verified using the
formula n(n+1)/2 = 10×11/2 = 55.
Alternative Method – Using built-in sum():
lower = int(input('Enter the Lower Limit: ')) upper = int(input('Enter the
Upper Limit: ')) # Python built-in sum() with range() total =
sum(range(lower, upper + 1)) print('Summation from', lower, 'to', upper, '=',
total)

Alternative Method – Using while loop:


lower = int(input('Enter the Lower Limit: ')) upper = int(input('Enter the
Upper Limit: ')) total = 0 num = lower while num <= upper: total = total
+ num num = num + 1 print('Summation =', total)
Question 14: Print All EVEN Numbers in a Range [9 Marks]

Problem Statement
Write a program in Python to accept lower and upper limit of a range of numbers from the user and
print all the even numbers in that range.

Concept
• A number is even if it is exactly divisible by 2, i.e., number % 2 == 0
• The program accepts two integers: lower limit and upper limit from the user
• A for loop iterates through all numbers in the range (inclusive of both limits)
• An if condition checks whether each number is even and prints it

Algorithm
• Step 1: Accept lower limit from the user using input()
• Step 2: Accept upper limit from the user using input()
• Step 3: Convert inputs to integers using int()
• Step 4: Use a for loop from lower to upper+1 (to include upper limit)
• Step 5: Inside the loop, check if number % 2 == 0
• Step 6: If condition is true, print the number

Program
▶ Python Code:
# Program to print all Even numbers in a given range # SPPU - TE Electrical
(2019 Course) # Accept lower and upper limits from the user lower =
int(input('Enter the Lower Limit: ')) upper = int(input('Enter the Upper
Limit: ')) print('\nEven numbers between', lower, 'and', upper, 'are:') #
Iterate through the range for num in range(lower, upper + 1): if num % 2
== 0: # Check if number is even print(num, end=' ') print() #
For new line at end

Sample Output
Input/Output — Example 1:
Enter the Lower Limit: 1 Enter the Upper Limit: 20 Even numbers between 1 and
20 are: 2 4 6 8 10 12 14 16 18 20
Input/Output — Example 2:
Enter the Lower Limit: 10 Enter the Upper Limit: 25 Even numbers between 10
and 25 are: 10 12 14 16 18 20 22 24

Explanation
The range(lower, upper + 1) function generates all integers from lower to upper inclusive. The
modulus operator % gives the remainder when divided by 2. If the remainder is 0, the number is
even. The end=' ' argument in print() displays all numbers on a single line separated by spaces.
Alternative Method – Using range() with step 2:
We can use range() with a step of 2 starting from the nearest even number to directly generate even
numbers:
lower = int(input('Enter the Lower Limit: ')) upper = int(input('Enter the
Upper Limit: ')) # Adjust start to nearest even number if lower is odd start
= lower if lower % 2 == 0 else lower + 1 print('\nEven numbers between',
lower, 'and', upper, 'are:') for num in range(start, upper + 1, 2): # step =
2 print(num, end=' ') print()
Comparison: Odd vs Even Programs
Feature Odd Number Program (Q6) Even Number Program (Q14)
Condition num % 2 != 0 num % 2 == 0
Check for Remainder NOT zero Remainder IS zero
Output (1-10) 13579 2 4 6 8 10
Step method range(start, end+1, 2) from odd range(start, end+1, 2) from even

Reference: R6 – Alex Bradbury & Ben Everard, Learning Python with Raspberry Pi, 1st Edition, John Wiley & Sons, Feb
2014.
— End of Programs —

You might also like