Python Programs

This page is updated regularly, so please check again for more new programs.

#Area and volume of a sphere
#Author: Mukesh N Tekwani

#Function to compute the area of a sphere
#Input parameter: radius r
def Area(r):
        a = 4 * 3.142 * r * r
        return a    #returns area 
# End of function

#Function to compute the volume of a sphere
#Input parameter: radius r
def Volume(r):
        v = 4/3 * 3.142 * r * r * r
        return v    #returns volume
# End of function

#Take input
rad = int(input("Please enter the radius of the sphere "))

#Store area in variable ar
ar = Area(rad)
#Now use the value in ar to print area
print("Area = ", ar)

#Call the function Volume from print function
print ("Volume = ", Volume(rad))

Write a program to reverse a string and check if it is a palindrome.

#Simple trick to reverse a string

str = "debug"  #declare string
revstr = str[::-1]
print("Reversed string is ", revstr)

#Now check if str is palindromic
#words which remain the same when spelt backwards are called palindromes
#E.g. madam, noon, radar, refer, stats
if str == revstr:
    print(str, " is a palindrome")
    print(str, " is not a palindrome")

Write a program to compute the factorial of a number entered by the user.

#Factorial of a number
def factorial(n):
        f = 1
        for i in range(1, n+1):
                f = f * i
        print ("Factorial = ", f)
#EO Function  - end of function. It helps to put this comment at 
#the end of a function to know where a function has ended. 
#The braces { and } in C, C++ #and Java were useful in that 
#sense to identify a block of code.
n = int(input("Enter a no. "))
factorial(n)      # call the function

Write a program to count the number of digits in an integer.

#To count the digits in a number
#E.g., Input 2387, output : 4
#input 23986, output 5

num = int(input('Enter a number '))

ctr = 0   # to keep a count of digits in the given number

while num > 0:
      r = num % 10   #% operator gives remainder after division by 10
      ctr = ctr + 1
      num = num // 10 #truncate the given number as last digit is used
#EOWhileLoop  This comment helps to identify the end of a loop
print ("There are ", ctr, " digits in that number")

Write a program to create complex numbers and perform addition and subtraction on these numbers.

#Complex numbers representation
#Author : Mukesh N Tekwani

#Create a complex no with real part 2 and imaginary part 3
#Complex number is denoted in the form a + ib
#a and b are real numbers and i is square root of -1 (imaginary)

x = complex(2,3)

print (type(x))   #print type of variable x
print (x)         #print value of x

y = complex(4, 2)
print (y)

z = x + y     #add the two complex numbers
print (z)

d = x - y     #difference of two complex numbers
print (d)
#End of program

Write a program to compute and print the first 10 Fibonacci numbers.

#Fibonacci numbers
#Author : Mukesh N Tekwani
a = 1
b = 1
ctr = 2
print (a, end = ' ') #end = ' ' means print value of a & stay on same line
print (b, end = ' ')

while ctr < 10:
      sum = a + b
      print (sum, end = ' ')
      ctr += 1  
      a = b
      b = sum
#End of program

Write a program to check if a number entered by the user is a prime number.

#Prime number check
#Author : Mukesh N Tekwani

#Function to check if a number entered by the user is a Prime number
#A prime number is one that is divisible only by 1 and itself.
# 5, 11, 29 are prime numbers but 6, 45, 216 are not prime numbers

def IsPrime(n):
        flag = 0
        for i in range(2, n):
                if (n % i) == 0:
                        flag = 1
                #End of if stmt
        # End of for loop
        if flag == 1:
                return False
                return True
# End of function

n = int(input("Pls enter a number "))

if IsPrime(n):
        print (n, " is a Prime number")
        print (n, " is not a Prime number")
#End of if stmt
#EO Program       
%d bloggers like this: