Computer Programming and Software

Semester III (PS03EMTH32)
old course
same as Sem IV (PS04EMTH32)


Syllabus

Unit-I The Basics: Literal constants, numbers, strings, variables, identifier naming, data types, objects, logical and physical lines, indentation. Operators, operator precedence, expressions. Control flow: the if statement, the while statement, the for loop, the break statement, the continue statement.
Unit-II Functions: Defining a function, local variables, default argument values, keyword arguments, the return statement, DocStrings. Modules: using the sys module, the from import statement, creating modules, the dir() function.
Unit-III & IV List of computer practicals.


Reference Books

  1. Swaroop C. H., A byte of Python.
  2. James Payne, Beginning Python: Using Python 2.6 and Python 3, Wiley India, 2010.
  3. Amit Saha, Doing Math with Python, No Starch Press (2015).
  4. SCILAB - A Free software to MATLAB by Er. Hema Ramachandran and Dr. Achuthsankar S. Nair., S. Chand and Company Ltd. (2008).


Practicals

  1. To find the minimum/maximum of a given list of numbers.
  2. To check whether a given number is odd or even. To check whether a given year is a leap year or not.
  3. To find the real roots of a quadratic equation.
  4. To compute \(n!\), \(a^n\), sum and average of a list of numbers. To prepare the result of a student.
  5. Primality lists: To check whether a given number is prime or not, to list all the prime numbers within a given range, to factorize a number.
  6. Manipulation of numbers: to check whether a given number is perfect or not, to check whether a given number is palindrome or not, to compute the sum of digits of a given number, to compute the sum of squares of the digits, to print a given number in reverse order of its digits.
  7. To compute GCD and LCM of two numbers, to evaluate the functions \(\sigma(n)\), \(\tau(n)\), \(\phi(n)\), \(\mu(n)\) for a given positive integer \(n\).
  8. To generate Fibonacci sequence and Lucas sequences; to compute the sum of the series and hence evaluate \(e^x\), \(\sin(x)\), \(\cos(x)\), \(\tan(x)\), \(\sinh(x)\), \(\cosh(x)\) (terminate the program after n terms of the series or terminate the program at the desired level of accuracy).
  9. Basics of Scilab 1: Sum of matrices, determinant of a matrix, product of matrices, inverse of a matrix, row reduced echelon form.
  10. Basics of Scilab 2: Plotting Cartesian, polar and parametric curves, commands for plotting functions.

Programs’ code

Codes of some programs (need not be in the best and efficient form) from the above syllabus of python programming are given below. You may try it out by clicking on the Output button and on Run to execture the code.

1. To find the maximum of a given list of numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
lst = []
n=int(input('How many numbers are there? '))

for i in range(n):
    num = float(input('Enter {}th number: '.format(i+1)))
    lst.append(num)
# k=range(n)
# print(k)
maxi=lst[0]
# print(maxi)
for i in range(1,n):
    if (maxi < lst[i]):
        maxi=lst[i]
print('Maximum computed manually is: ', maxi)
print('Maximum number from the list is: ', max(lst))


2(a). To check whether a given number is odd or even

1
2
3
4
5
6
# Program to check whether a number is odd or even
n=int(input('Enter your number: '))
if n%2 == 0:
    print('Your entered number', n, 'is even')
else:
    print('Your entered number', n, 'is odd')

2(b). To check whether a given year is a leap year or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Program to check if the input year is a leap year or not

year = int(input("Enter a year number of your choice: "))

#if year%400 == 0 or (year %4==0 and year%100!=0):
#    print('The year {} is a leap year.'.format(year))
#else:
#    print('The year {} is not a leap year.'.format(year))

if (year % 4 == 0):
    if (year % 100 == 0):
        if year % 400 == 0:
            print("{} is a leap year".format(year))
        else:
            print("{} is not a leap year".format(year))
    else:
        print("{} is a leap year".format(year))
else:
    print("{} is not a leap year".format(year))


3. To find the real roots of a quadratic equation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Program to find roots of quadratic equation

print('Consider the equation of the form ax^2+bx+c=0')

a=float(input('Enter the non-zero coefficient a of x^2: '))
b=float(input('Enter the coefficient b of x: '))
c=float(input('Enter the constant term c: '))

delta = b**2 - 4 * a * c
d=delta**(0.5)

if (delta<0):
    print('The given quadratic equation {}x^2 + {}x + c =0 has no real roots'.format(a,b,c))
else:
    r1 = (-b+d)/(2*a)
    r2 = (-b-d)/(2*a)
    print('The real roots of given quadratic equation {}x^2 + {}x + {} =0 are {} and {}'.format(a,b,c,r1,r2))


4. To compute \(n!\) (factorial of a given non-negative integer \(n\)).

1
2
3
4
5
6
7
8
9
10
11
12
# Python program to compute n factorial
n = int(input('Enter a non-negative integer: '))
if n<0:
    print('Please enter a non-negative integer.')
elif n==0:
    print('0! = 1')
else:
    i=n
    nfact=1
    for i in range(n,0,-1):
        nfact = nfact * i
    print('{}! = {}'.format(n,nfact))


5(a). To check whether a given number is prime or not.

1
2
3
4
5
6
7
8
9
# Python program to check whether a number is prime or not
n=int(input('Enter an integer n>2: '))
for i in range(2,int((n**0.5)+1)):
    if n%i==0:
        print("The entered number {} is not prime.".format(n))
        print(i)
        break
else:
    print("The entered number {} is a prime number.".format(n))

5(b). To list all prime numbers within a given range.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Python program to list all the prime numbers in a given range
a = int(input('Give the starting point a (> 0) of the range: '))
b = int(input('Give the ending point b (> a) of the range: '))
if b > a > 0:
    if a == 1:
        a1 = 2
    else:
        a1 = a
    print('The prime numbers between {} and {} are the following:'.format(a,b))
    for n in range(a1, b+1):
        test = 0
        for i in range(2, int((n**0.5)+1)):
            if n % i == 0:
                test = 1
                break
#        else:
        if test == 0:
            print(n, end=', ')
else:
    print("Please enter a valid range.")

5(c). To factorize a given number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Program to factorize a given number
n = int(input("Enter the number (n > 1) which you want to factorize: "))
lst = [1]
#lst.append(1)
for i in range(2, n+1):
    if n % i == 0:
        lst.append(i)
if len(lst) == 2:
    print("The number {} is prime and it only factors are 1 and {}.".format(n,n))
else:
    print('The factors of {} are the following:'.format(n))
    for p in lst:  # range(len(lst)):
        print(p, end=', ')
# Extending the program to check whether the number is perfect or not
print('')
if n == sum(lst)-n:
    print('The number',n,'is perfect')

5(d). To print the \(n^{\rm th}\) prime.

1
2
3
4
5
6
7
8
9
10
11
12
13
# Program to compute the nth prime number

n = int(input('Enter the value of n to compute the nth prime number: '))
c = 2
x = 5
while c < n:
    for i in range(2,int(x**0.5)+1):
        if x % i == 0:
            break
    else:
        c = c + 1
    x = x + 2
print(n,"th prime number is ",x-2)


6. Manipulation of numbers: to check whether a given number is perfect or not, to check whether a given number is palindrome or not, to compute the sum of digits of a given number, to compute the sum of squares of the digits, to print a given number in reverse order of its digits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Program to do various manipulation of numbers
# We check number is perfect or not, palindrome or not, compute sum of digits, sum of squares of digits and reverse the number

n = int(input('Enter the number: '))

sumdiv = 0  # for storing sum of divisors

for i in range(1,int(n/2)+1):
    if n % i == 0:
        sumdiv = sumdiv + i

if sumdiv == n:
    print('The entered number {} is perfect'.format(n))
else:
    print('The entered number {} is Not perfect'.format(n))

num = n  # num will be used for manipulation without disturbing n
sum = 0  # will be used to store the sum of digits of n
sumsq = 0  # will be used to store the sum of squares of digits
rev = 0  # will be used to reverse the number

while num > 0:
    digit = num % 10
    sum = sum + digit
    sumsq = sumsq + (digit**2)
    rev = (rev * 10) + digit
    num = num // 10

if rev == n:
    print('The entered number {} is palindrome'.format(n))
else:
    print('The entered number {} is Not palindrome'.format(n))

print('The sum of digits of {} is {}'.format(n,sum))
print('The sum of the squares of digits of {} is {}'.format(n,sumsq))
print('The entered number {} in the reverse order of its digits is {}'.format(n,rev))


7(a). To compute GCD and LCM of two numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Program to find gcd and lcm of two numbers
a = int(input('Enter first positive integer a: '))
b = int(input('Enter second positive integer b: '))

gc = 1

def gcd(x,y):
    global gc
    x1=x
    y1=y
    if x1 % y1 == 0:
        print('GCD of {} and {} computed by recurrence is {}'.format(a,b,y1))
        gc = y1
    else:
        gcd(y1,(x1%y1))


gcd(a, b)

for i in range (min(int(a/2),int(b/2))+1,0,-1):
    if a % i == 0 and b % i == 0:
        print('GCD of {} and {} computed by loop is {}'.format(a,b,i))
        break

print('LCM of {} and {} is {} (using y1)'.format(a,b,int(a*b/gc)))
print('LCM of {} and {} is {} (using i)'.format(a,b,int(a*b/i)))

7(b). To evaluate the function \(\phi(n)\) for a given positive integer \(n\).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Python Program to compute Euler's phi function for n >= 2

g=1
def gcd(x,y):
    global g
    if x % y == 0:
        g = y
#        return y
    else:
        gcd(y,x%y)

n = int(input('Enter a positive integer: '))
c = 0

for i in range(1,n):
    gcd(n,i)
    if g == 1:
        c = c + 1
print('Euler\'s function of {} is {}'.format(n,c))

7(c). To evaluate the Möbius function \(\mu(n)\) for a given positive integer \(n\).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Python Program to compute the mobius function mu(n)
n = int(input('Enter the value of n: '))
u=1
def is_prime(x):
    for i in range(2,int(x**(0.5))+1):
        if x%i==0:
            return 0
    else:
        return 1

if n <=0:
    print('Enter a valid positive integer n')
elif n==1:
    print('u(1)=1')
else:
    for k in range(2,n+1):
        if is_prime(k)==1 and n%k==0:
            if n%(k*k)==0:
                u=0
                break
            else:
                u = u*(-1)
    print('Mobius function u({})={}'.format(n,u))


8(a). To generate Fibonacci sequence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Python program to compute n terms of fibonacci sequence

def fibo(x):
    if x <= 0:
        print('Enter a positive integer.')
    elif x  == 1:
        return 0
    elif x == 2:
        return 1
    else:
        return fibo(x-1)+fibo(x-2)

n = int(input('Enter the term n: '))

#print(fibo(n))  # To print the nth fibonacci number

#for i in range(1,n+1):  # To print first n fibonacci numbers
#    print(fibo(i))

i = 1
while fibo(i)<= n:  # To print fibonacci numbers upto n
    print(fibo(i))
    i = i + 1

8(b). To compute the sum of the series and hence evaluate \(e^x\), \(\sin(x)\), \(\cos(x)\), \(\tan(x)\), \(\sinh(x)\), \(\cosh(x)\) (terminate the program after n terms of the series or terminate the program at the desired level of accuracy).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Python program to compute the sum of the series e^x, sin x, cos x, sinh x and cosh x upto n terms

import math

x = float(input('Enter the value of x: '))
n = int(input('Enter the number of terms of the series n: '))

#x = y*math.pi/180

def expo(x):
    sumex = 1
    termex = 1
    for m in range(1,n):
        termex = (termex * x)/m
        sumex = sumex + termex
    return sumex

sumcos = 1
termcos = 1
termsin = x
sumsin = termsin

#i=1
#while math.fabs(termcos) > 0.0001:
for i in range(1,n):
    termsin = (termsin * (x**2) * (-1))/((2*i)*(2*i+1))
    sumsin = sumsin + termsin
    termcos = (termcos * (x**2) * (-1))/((2*i-1)*(2*i))
    sumcos = sumcos + termcos
#    i = i + 1

print('')
print('The computed value of e^{} upto {} terms is {}.'.format(x,n,expo(x)))
print('The actual value of e^{} using math module is {}.'.format(x,math.exp(x)))
print('')

print('The computed value of sin({}) upto {} terms is {}.'.format(x,n,sumsin))
print('The actual value of sin({}) using math module is {}.'.format(x,math.sin(x)))
print('')

print('The computed value of cos({}) upto {} terms is {}.'.format(x,n,sumcos))
print('The actual value of cos({}) using math module is {}.'.format(x,math.cos(x)))
print('')

print('The computed value of sinh({}) upto {} terms is {}.'.format(x,n,(expo(x)-expo(-1*x))/2))
print('The actual value of sinh({}) using math module is {}.'.format(x,math.sinh(x)))
print('')

print('The computed value of cosh({}) upto {} terms is {}.'.format(x,n,(expo(x)+expo(-1*x))/2))
print('The actual value of cosh({}) using math module is {}.'.format(x,math.cosh(x)))
print('')


Lecture Notes

Download the PDF file of lecture notes