Share @ LinkedIn Facebook  python, basics
Python Programming Guide - Simple Python Programs

Python Programming Guide - Simple Python Programs

Simple Python Programs That You Would Learn Through This Tutorial

  • Python Program to Calculate the Average of Numbers in a Given List
  • Program to Exchange the Values of Two Numbers Without Using a Temporary Variable
  • Program to Reverse a Given Number
  • Program to Check Whether a Number is Positive or Negative
  • Program to Find the Sum of Digits in a Number
  • Program to Print Odd Numbers Within a Given Range
  • Program to Count the Number of Digits in a Number
  • Program to Check if a Number is a Palindrome

Python Program to Calculate the Average of Numbers in a Given List

In [1]:
n=int(input("Enter the number of elements to be inserted: "))
a=[]
for i in range(0,n):
    element=int(input("Enter element: "))
    a.append(element)
avg=sum(a)/n
print("Average of elements in the list",round(avg,2))
Enter the number of elements to be inserted: 3
Enter element: 3
Enter element: 4
Enter element: 5
Average of elements in the list 4.0

Understanding The Program:

The program will be making use of "For Loop"

  • The user is required to enter the number which will be stored in the variable 'n'.
  • The value of the variable 'i' will range from 0 to the 'number of element' entered by the user.
  • The number entered by the user will be stored in the variable 'element'.
  • The built-in function will append the number to the list
  • Now the value of i is incremented to 2.
  • Every new value entered by the user will iterate and get stored in the variable "element" and then appended to the list.
  • Then the 'sum' of all the values will be calculated and after that average.

Program to Exchange the Values of Two Numbers Without Using a Temporary Variable

In [2]:
a=int(input("Enter value of first variable: "))
b=int(input("Enter value of second variable: "))
a=a+b
b=a-b
a=a-b
print("a is:",a," b is:",b)
Enter value of first variable: 2
Enter value of second variable: 4
a is: 4  b is: 2

Understanding The Program:

  1. In this program, the user is asked to enter the two values for both the elements.
  2. The first element is assigned the sum of the first two elements.
  3. The second element is assigned the difference between the sum in the first variable and the second variable, which is basically the first element.
  4. Swapping of the numbers is carried out by the difference between the sum in the variable and the second variable, which is the second element.
  5. Then the swapped values are printed.

Program to Reverse a Given Number

In [3]:
n=int(input("Enter number: "))
rev=0
while(n>0):
    dig=n%10
    rev=rev*10+dig
    n=n//10
print("Reverse of the number:",rev)
Enter number: 23
Reverse of the number: 32

Understanding The Program

  1. In this program, the user is asked to enter the value and store it in a variable n. (To witness the reversal, you must enter a two-digit number).
  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. In the final step, the reverse of the number is then printed.

Program to Check Whether a Number is Positive or Negative

In [4]:
n=int(input("Enter number: "))
if(n>0):
    print("Number is positive")
else:
    print("Number is negative")
Enter number: 34
Number is positive

Understanding The Program

  1. Users must first enter the value and store it in a variable.
  2. Use an if statement to make a decision.
  3. If the value of the number is greater than 0, “Number is positive” is printed.
  4. If the value of the number if lesser than 0, ”Number is negative” is negative.

Program to Find the Sum of Digits in a Number

In [5]:
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)
Enter a number:465
The total sum of digits is: 15

Understanding The Program

  1. Users 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.

Program to Print Odd Numbers Within a Given Range

In [6]:
lower=int(input("Enter the lower limit for the range:"))
upper=int(input("Enter the upper limit for the range:"))
for i in range(lower,upper+1):
    if(i%2!=0):
        print(i)
Enter the lower limit for the range:23
Enter the upper limit for the range:32
23
25
27
29
31

Understanding The Program

  1. Users must enter the upper range limit and the lower range limit.
  2. The for loop ranges from the lower range limit to the upper range limit.
  3. The expression within the if-statement checks if the remainder obtained when the number divided by 2 is one or not (using the % operator).
  4. If the remainder isn’t equal to 0, the number is odd and hence the number is printed.

Program to Count the Number of Digits in a Number

In [7]:
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)
Enter number:335678
The number of digits in the number are: 6

Understanding The Program

  1. Here the 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.

Program to Check if a Number is a Palindrome

In [8]:
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!")
Enter number:55
The number is a palindrome!

Understanding The Program

  1. Users 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.

Dolly Solanki  Dolly Solanki