Python Programs List
Python Programs List
Given List
This is a Python Program to Calculate the Average of Numbers in a Given List.
Problem Description
The program takes the elements of the list one by one and displays the average of the
elements of the list.
Problem Solution
1. Take the number of elements to be stored in the list as input.
2. Use a for loop to input elements into the list.
3. Calculate the total sum of elements in the list.
4. Divide the sum by total number of elements in the list.
5. Exit.
Program/Source Code
Here is source code of the Python Program to Calculate the Average of Numbers in a Given
List. The program output is also shown below.
Program Explanation
1. User must first enter the number of elements which is stored in the variable n.
2. The value of I ranges from 0 to the number of elements and is incremented each time
after the body of the loop is executed.
3. Then, the element that the user enters is stored in the variable elem.
4. a.append(elem) appends the element to the list.
5. Now the value of i is incremented to 2.
6. The new value entered by the user for the next loop iteration is now stored in elem which
is appended to the list.
7. The loop runs till the value of i reaches n.
8. sum(a) gives the total sum of all the elements in the list and dividing it by the total number
of elements gives the average of elements in the list.
9. round(avg,2) rounds the average upto 2 decimal places.
10. Then the average is printed after rounding.
Case 1:
Enter the number of elements to be inserted: 3
Enter element: 23
Enter element: 45
Enter element: 56
Average of elements in the list 41.33
Case 2:
Enter the number of elements to be inserted: 5
Enter element: 12
Enter element: 24
Enter element: 33
Enter element: 25
Enter element: 18
Average of elements in the list 22.4
Problem Description
The program takes both the values from the user and swaps them without using temporary
variable.
Problem Solution
1. Take the values of both the elements from the user.
2. Store the values in separate variables.
3. Add both the variables and store it in the first variable.
4. Subtract the second variable from the first and store it in the second variable.
5. Then, subtract the first variable from the second variable and store it in the first variable.
6. Print the swapped values.
7. Exit.
Program/Source Code
Here is source code of the Python Program to exchange the values of two numbers without
using a temporary variable. The program output is also shown below.
Program Explanation
1. User must first enter the values for both the elements.
2. The first element is assigned the sum of the first two elements.
3. Second element is assigned the difference between the sum in the first variable and the
second variable, which is basically the first element.
4. Later the first element is assigned the difference between the sum in the variable and the
second variable, which is the second element.
5. Then the swapped values are printed.
Case 1
Enter value of first variable: 3
Enter value of second variable: 5
a is: 5 b is: 3
Case 2
Enter value of first variable: 56
Enter value of second variable: 25
a is: 25 b is: 56
Problem Description
The program takes a number and reverses it.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and store the reversed number in
another variable.
3. Print the reverse of the number.
4. Exit.
Program/Source Code
Here is the source code of the Python Program to reverse a given number.
Program Explanation
1. User must first enter the value and store it in a variable n.
2. The while loop is used and the last digit of the number is obtained by using the modulus
operator.
3. The last digit is then stored at the one’s place, second last at the ten’s place and so on.
4. The last digit is then removed by truly dividing the number with 10.
5. This loop terminates when the value of the number is 0.
6. The reverse of the number is then printed.
Case 1:
Enter number: 124
Reverse of the number: 421
Case 2:
Enter number: 4538
Reverse of the number: 8354
Problem Description
The program takes in the marks of 5 subjects and displays the grade.
Problem Solution
1. Take in the marks of 5 subjects from the user and store it in different variables.
2. Find the average of the marks.
3. Use an else condition to decide the grade based on the average of the marks.
4. Exit.
Program/Source Code
Here is source code of the Python Program to take in the marks of 5 subjects and display
the grade. The program output is also shown below.
Program Explanation
1. User must enter 5 different values and store it in separate variables.
2. Then sum up all the five marks and divide by 5 to find the average of the marks.
3. If the average is greater than 90, “Grade: A” is printed.
4. If the average is in between 80 and 90, “Grade: B” is printed.
5. If the average is in between 70 and 80, “Grade: C” is printed.
6. If the average is in between 60 and 70, “Grade: D” is printed.
7. If the average is anything below 60, “Grade: F” is printed.
Case 2:
Enter marks of the first subject: 81
Enter marks of the second subject: 72
Enter marks of the third subject: 94
Enter marks of the fourth subject: 85
Enter marks of the fifth subject: 80
Grade: B
Problem Description
The program takes in a number and finds the sum of digits in a number.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and add the digits to a variable.
3. Print the sum of the digits of the number.
4. Exit.
Program/Source Code
Here is the source code of the Python Program to find the sum of digits in a number. The
program output is also shown below.
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
Program Explanation
1. User must first enter the value and store it in a variable.
2. The while loop is used and the last digit of the number is obtained by using the modulus
operator.
3. The digit is added to another variable each time the loop is executed.
4. This loop terminates when the value of the number is 0.
5. The total sum of the number is then printed.
Case 1:
Enter a number:1892
The total sum of digits is: 20
Case 2:
Enter a number:157
The total sum of digits is: 13
Problem Description
The program takes in an integer and prints the smallest divisor of the integer.
Problem Solution
1. Take in an integer from the user.
2. Use a for loop where the value of i ranges from 2 to the integer.
3. If the number is divisible by i, the value of i is appended to the list.
4. The list is then sorted and the smallest element is printed.
5. Exit.
Program/Source Code
Here is the source code of the Python Program to find the smallest divisor of an integer.
The program output is also shown below.
n=int(input("Enter an integer:"))
a=[]
for i in range(2,n+1):
if(n%i==0):
a.append(i)
a.sort()
print("Smallest divisor is:",a[0])
Program Explanation
1. User must enter an integer
2. The for loop ranges from 2 to the number
3. If the remainder of the number divided by the value of i is 0 that means that the element
is a divisor of the number
4. The divisor of the number is then appended to the list
5. The list is then sorted and the smallest element which is the element with index 0 is
printed
Case 1:
Enter an integer:75
Smallest divisor is: 3
Case 2:
Enter an integer:64
Smallest divisor is: 2
Problem Description
The program takes the number and prints the number of digits in the number.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and increment the count each time a
digit is obtained.
3. Print the number of digits in the given integer.
4. Exit.
Program/Source Code
Here is source code of the Python Program to count the number of digits in a number. The
program output is also shown below.
n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
print("The number of digits in the number are:",count)
Program Explanation
1. User must first enter the value of the integer and store it in a variable.
2. The while loop is used and the last digit of the number is obtained by using the modulus
operator.
3. Each time a digit is obtained, the count value is incremented.
4. This loop terminates when the value of the number is 0.
5. The total count of the number of digits is printed.
Case 1:
Enter number:123
The number of digits in the number are: 3
Case 2:
Enter number:1892
The number of digits in the number are: 4
Problem Description
The program takes a number and checks whether it is a palindrome or not.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Transfer the value of the integer into another temporary variable.
3. Using a while loop, get each digit of the number and store the reversed number in
another variable.
4. Check if the reverse of the number is equal to the one in the temporary variable.
5. Print the final result.
6. Exit.
Program/Source Code
Here is source code of the Python Program to check whether a given number is a
palindrome. The program output is also shown below.
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Program Explanation
1. User must first enter the value of the integer and store it in a variable.
2. The value of the integer is then stored in another temporary variable.
3. The while loop is used and the last digit of the number is obtained by using the modulus
operator.
4. The last digit is then stored at the one’s place, second last at the ten’s place and so on.
5. The last digit is then removed by truly dividing the number with 10.
6. This loop terminates when the value of the number is 0.
7. The reverse of the number is then compared with the integer value stored in the
temporary variable.
8. If both are equal, the number is a palindrome.
9. If both aren’t equal, the number isn’t a palindrome.
10. The final result is then printed.
Case 1
Enter number:121
The number is a palindrome!
Case 2
Enter number:567
The number isn't a palindrome!
Problem Description
The program takes a number n and prints an inverted star pattern of the desired size.
Problem Solution
1. Take a value from the user and store it in a variable n.
2. Use a for loop where the value of i ranges between the values of n-1 and 0 with a
decrement of 1 with each iteration.
3. Multiply empty spaces with n-i and ‘*’ with i and print both of them.
4. Exit.
Program/Source Code
Here is the source code of the Python Program to read a number n and print an inverted
star pattern of the desired size. The program output is also shown below.
n=int(input("Enter number of rows: "))
for i in range (n,0,-1):
print((n-i) * ' ' + i * '*')
Program Explanation
1. User must first enter the value and store it in a variable n.
2. The for loop enables i to range between n-1 and 0 with a decrement of 1 with each
iteration.
3. For each iteration, ” ” is multiplied with n-i and ‘*’ is multiplied with i to ensure correct
spacing of the stars.
4. The required pattern is printed.
Case 1:
Enter number of rows: 5
*****
****
***
**
*
Case 2:
Enter number of rows: 10
**********
*********
********
*******
******
*****
****
***
**
*