0% found this document useful (0 votes)
20 views2 pages

Python Programming Examples and Exercises

The document provides examples of Python programs to print series of numbers, find sums and perform other calculations. It includes programs to print numbers from 1 to 10, odd numbers from 1 to 20, and numbers from 10 to 1. Other examples show how to find the sum of natural numbers between two ranges, the sum of even numbers between ranges, and calculate simple interest over a number of years. Multiplication tables for a number up to a certain times are also demonstrated.

Uploaded by

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

Python Programming Examples and Exercises

The document provides examples of Python programs to print series of numbers, find sums and perform other calculations. It includes programs to print numbers from 1 to 10, odd numbers from 1 to 20, and numbers from 10 to 1. Other examples show how to find the sum of natural numbers between two ranges, the sum of even numbers between ranges, and calculate simple interest over a number of years. Multiplication tables for a number up to a certain times are also demonstrated.

Uploaded by

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

Python book examples: -----

1. Write a program in python to print the numbers from 1 to 10.


2. Write a program in python to print the following series:
1 3 5 7 9 11….
3. Write a program in python to display the vales from 10 to 1.

10 9 8 7 6 5 4 3 2 1

4. Write a program in python to print all natural numbers from 21 to 30.


5. Write a program in python to display the following series:

2 4 6 8 10 12 14 16 18 20.

6. Write a program in python to display the following series which is separated from coma(,):

2, 4, 6 , 8 , 10, 12, 14 , 16, 18, 20.

Python question
Q1. Program to find the sum of the natural number from 21 to 30 .
sum=0
for x in range(21,31):
sum=sum+x
print("sum=",sum)

Q2. Program to find the sum of the natural number from m to n, where m and n are input by user.
sum=0
n=int(input("enter starting value"))
m=int(input("enter ending value"))
for x in range(n,m+1):
sum=sum+x
print("sum=",sum)

Q3. Program to find the sum of even numbers from m to n, where m and n are input by user.
Sm=0
n=int(input("enter starting value"))
m=int(input("enter ending value"))
for x in range(n,m+1):
if(x%2==0):
sum=sum+x
print("even number=",sum)

Q4. Program to display multiplication table of a number entered by the user p to 10 times.
number = int(input ("Enter the number"))
#print ("The Multiplication Table ", number)
for i in range(1, 11):
print (number, 'x', i, '=', number * i)
Q5. Program to display multiplication table of m ,up to n times where number m and n are entered
by user.
m= int(input ("Enter the number"))
n= int(input ("number of times."))
print ("The Multiplication Table ", number)
for i in range(1, n):
print (m, 'x', i, '=', m * i)

Q6. Program to input a salary of an employee and display the salary that the person will receive for
each of next 5 years if he gets an increment of 10% every year.
Salary=int(input(“enter salary yearly”))
Inc=input(“write yes or no ”)
for x in range(1,6,1)
if(inc==”yes”):
salary=salary + salary*10/100
print(“salary is :”,salary)

Q7program to input the Principal, rate of Interest and display the simple interest to be paid at the
end of each year for n years.
p = float(input("principal? "))
rate = float(input("rate? "))
t = int(input("Number of years? "))
i = 0
for x in range(1, t+1,1):
si=(p*rate*t)/100
print(“si is :”,si)

Q8. Program to display the sum of factors of n which is input by user.


n=int(input("Enter a number: "))
s=0
for i in range(1,n+1):
if n%i==0:
s+=i
print("Sum of factors: ",s)

Q9. Program to display the sum of even factors of number input by the user.
n=int(input("Enter a number: "))
s=0
for i in range(1,n+1):
if n%i==0:
if i%2==0:
print(i)
s+=i
print("Sum of factors: ",s)

Common questions

Powered by AI

The program uses a loop that iterates five times, and during each iteration, it recalculates the salary by adding 10% of the current salary. This cumulative approach ensures that each year's salary is correctly built upon the previous year's updated salary, resulting in a compounding effect across the five years provided that the increment condition is met as checked with 'if(inc=="yes")' .

The discrepancy in the multiplication table program arises because the range for iterations is incorrectly defined using 'range(1, n)'. To include exactly n times, the correct implementation should use 'range(1, n+1)' to ensure the loop iterates through n times inclusive. This oversight leads to the final iteration being omitted .

The given logic erroneously uses the product of 'p*rate*t' for each year, which results in the same simple interest being printed each time. To correct it, the interest should be calculated annually as 'si = (p * rate * 1) / 100' for each year, and the principal should be updated only if cumulative interest is to be reflected across subsequent years .

The computation of the sum of all factors involves a direct aggregation of divisors for a number, while the sum of even factors introduces an additional condition requiring a check for evenness (i%2==0). This added conditional logic filters out odd factors, including only those divisible by 2 in the summation process .

To modify the Python program to use while loops, the index variable can be manually initialized before the loop, and a conditional expression in the while loop can control the iteration based on desired termination criteria. For instance, to print numbers from 1 to 10 using a while loop, start with 'i = 1' and use 'while i <= 10:' with necessary print function and increment 'i' inside the loop .

User interaction greatly enhances program versatility and engagement, allowing dynamic input-driven operations, such as repeated summations or products, tailored to specific use cases and contexts. However, it also introduces complexity in terms of validation and data type compliance, potentially increasing error management while maintaining flexibility and control based on the user's immediate needs and inputs .

Errors can occur if the user inputs non-integer values or inputs where the starting value is greater than the ending value. These can be mitigated by implementing type checks to ensure that integers are input and logic checks that enforce n < m, prompting the user to re-enter values until valid inputs are provided. Additionally, error handling can be introduced to catch exceptions and guide users to correct inputs .

The program with a predefined range (21 to 30) directly iterates over this range and calculates the sum . In contrast, when parameters 'm' and 'n' are provided by the user, the program dynamically initializes the range based on user input, requiring additional input operations. The logic for summation remains similar but is more versatile due to user-defined inputs, which requires boundary validation to ensure m < n .

Input validation is crucial because user-driven summation programs rely on user inputs that determine the range and behavior of the programs. Invalid inputs such as non-numeric values or inappropriate range values (e.g., start greater than end) can lead to runtime errors, incorrect results, or program crashes. Robust validation ensures only acceptable inputs pass through, maintaining program integrity and usability .

In multiplication table generation, iterative control structures such as the for or while loop dictate the repetition needed for sequential multiplication of the base number by iterating over a defined range. They establish the rhythm for operations repetitiously until termination, effectively controlling how data is transformed, displayed, and processed through consistent updates across each iteration, demonstrating determinism in the flow of the program .

You might also like