You are on page 1of 6

Python assignments

Interactive Mode
1. Use python as a simple calculator (use arithmetic operators: +, -, *,/, %, **)

2. Use little algebra like


a=2
b=5
c=(a+b)/(a-b)
c

3. Type
>>dir(__builtins__)
# note the double underscores in each side
you will get a list of functions
try to find out what does the following function do (by typing help(abs) etc)

abs, complex, divmod, , float, int, pow, round, bin, eval, chr, ord

In each case write a small program or use it in the interactive environment to


show that you have understood the purpose of the above functions

4. Type
>> from math import *
>>dir()
(you will find a lot of new functions, some of them are obvious like sin, cos, tan)
Use help to find what the following functions do
asin, acosh, ceil, degrees, floor.
In each case write a small program or use it in the interactive environment to
show that you have understood the purpose of the above functions

5. exit from python. Renter python and type


>>from cmath import *
Try sqrt, exp

Programming Assignment 1

6. Input temperature in degree, output in Kelvin as well as in Fahrenheit.


7. Input angle in degree, output in radian
8. WAP to covert Cartesian to Polar co-ordinate (angles in degrees)
9. Swap two numbers
^) is a vector.
10. . 𝑥⃗ = (𝑥𝑖^ + 𝑦𝑗^ + 𝑧𝑘

Wap which inputs three of the components of the vector and outputs
i) the norm ii) angle with each of the axes.

11. Input six components of two vectors and output the


i) angle between them in degrees
ii) the norm of the difference vector
iii) if these vectors are 𝑂𝐴⃗ and 𝑂𝐵
⃗⃗ find the area of the triangle AOB
iv) Find the co-ordinate of the parallelogram whose three vertices are
𝑂, 𝐴, 𝐵. Coordinate of O is (0,0)

Following two assignment uses if –elif-else command

12. Arrange three numbers in ascending order


13. Input from the user unit price of items and the unit purchased until the user asks
to stop. Calculate the total price. Give discount of 5% if the total price is
between 100 and 500, 10% between 500 to 1500, 15% between 1500 to 3000
and 25% if the total price is above 3000. Output the price to be paid.

Interactive Mode
Concept of list, tuple, string are to be introduced

14. Use the command


>> x=range(1:11)
>> type (x)
>>y=sum(x)
what do you understand the function of range and sum? You should try few other lists
to confirm your hypothesis.

10. >> x+x : what happens? >>3*x: what happens?

15. . >> help(x)


What does the following functions do?
max, min, len,
what does the following methods do?
append, count, pop, insert, sort
Do you understand the difference in syntax for using functions and methods?

16. . type
>>x=”I am a student”
or any other string
find out what are x[0], x[1] etc …..
find out what are x[0:6], x[2:8] etc…
find out what are x[0:2:11] etc ..
find out what are x[2:] etc ..
find out what are x[:14] etc ..
find out what are x[-1] etc ..
find out what are x[-2:] etc ..
find out what are x[-5:-2] etc ..

(These techniques, called slicing, are equally applicable to the lists and tuples)

17. a) type
help(str.center) etc
to find out what does the following methods do

(to use the method center() on a string x type)

x.center(25,’*’)

b) change the values of the number above(25) and the character above(‘*’) to
understand what is actually happening. )

capitalize, center, count, strartswitdth, find, isalpha, upper, rjust, replace, split, find,
strip,

Introduction to the control structures (for, while) are necessary for the following
assignments.

Assignment Sheet 2
(Control Structure)
18. Input a line, count the number of letters and words
Or,
19. Input a letter in lower case, convert it in upper case
1 1
20. Sum the series ∑𝑛𝑟=1 𝑟 and the sum ∑𝑛𝑟=1 𝑟 2 for
for n=10, 100, 1000, 10000, 100000, 1000000
Can you see any difference in these two cases?

21. Input a, n, r and sum the


a) G.P. Series ∑𝑛𝑚=0 ar 𝑚
OR,
b) A.P. Series ∑𝑛𝑚=0(𝑎 + mr)
22. Input a character, change its case (using lower() etc and also handling the ascii
value directly, using chr() and ord() )
Or,
23. Input a character line find the sum of ascii values
24. Generate Fibonacci Sequence :
a) up to user given no of terms
b) less than a user given upper limit
c) verify if a given number belongs to the Fibonacci sequence.
𝑡
d) Determine the value of 𝛷 =( lim 𝑛+1 )
𝑛→∞ 𝑛𝑡
Any two of the following
25. Sum an infinite GP series .
26. Sum an infinite exponential series
27. Sum an infinite sine series
28. Sum of infinite cosine series
29. Input n, output factorial of n.

Assignment Sheet 3
Generate the following the data of the following function by writing codes in
Python. Use gnuplot to see the graph.

30. A) log(x) : x=0.00001 to 100000, change by a multiplier of 10.


B) 𝑡𝑎𝑛−1 𝑥: x=-10 to 10
𝜃
C) Legendre Polynomial 𝑐𝑜𝑠: 𝜃 = −180180degree, l=2,3
𝑃𝑙
D) Confluent Hypergeometric function: x=0 to 10 step 0.01
𝑎 𝑎(𝑎 + 1) 2 𝑎(𝑎 + 1)(𝑎 + 2) 3
1 𝐹1 (𝑎, 𝑐, 𝑥) = 1 + 𝑥 + 𝑥 + 𝑥
𝑐 𝑐(𝑐 + 1) 𝑐(𝑐 + 1)(𝑐 + 2)
+ ⋯…
For a=1, c=2 and for a=2, c=3.

Assignment 4
All the following problems have to be worked out involving strings, lists, tuples and
loops, explicitly using the loop index and reference to the sequence element
31. Wap to perform tasks similar to the string method count() and find().
32. Input two vectors of user given dimensions and determine their sum and dot
product
33. Input n (user given) numbers and determine their sum and s.d
34. Input n (user given) numbers and arrange them in ascending order using
a) bubble sort c) Selection sort d) insertion sort algorithm.
35. Determine the median of n given numbers.
Assignments 5

36. Read numbers from a file (number containing some repetition. Find the frequency
distribution and hence the mode of the distribution. Write the frequency
distribution in a different file.
37. Read numbers from a file. Calculate histogram of the data Also find the
cumulative histogram
38. Define a function which calculates the transpose of a matrix. Test it with a main.
39. Define a function which calculates the trace of a matrix. Test it with a main.

Python Assignment 7
42.
a) i) Determine the value of m by fitting the supplied data with a theoretical function
𝑦 = mx
ii) Plot the data and the fitted curve using gnuplot
b) Repeat exercise a) but this time with another supplied data with the function 𝑦 =
mx + 𝑐
𝑏
c) Repeat exercise a) but this time with a function 𝑦 = 𝑎 + 𝑥 2 (a, b are the parameters
to be determined by the least square method) and another supplied data

d) The supplied data should be fitted with the function 𝑦 = 𝐴𝑒𝑥𝑝(−𝑏𝑥)


WAP to fit the data by least square mehod.

Python Assignment 7
1. Write a class named matrix which should be instantiated by the number of rows
and the number of columns. Write methods like
i) readmat – to read the elements of the matrix
ii) __repr__ - to print the matrix in the workspace
iii) transpose – to return the transpose of the matrix
iv) trace – to return the trace of the matrix
v) __add__ - to add self with the other
vi) __sub__ - to subtract other from self
vii) __mul__ - to multiply the matrix with a scaler

Assignment Sheet 8
OPTIONAL
(Turtle)
4. Import turtle. WAP to input n (an integer) and r ( the length of the arm). Draw a
uniform n-gon of arm r.
5. Input three values of radii, draw concentric circles of those radii values. Color
them differently.
6. Write a function pset(x,y) which plots a point (a circle of radius 1) at the co-
ordinate (x,y).
7. Draw to axes though the center of the turtle canvas. Draw the Lissajous Figure of
𝑥 = 𝐴𝑠𝑖𝑛𝜔1 𝑡 and 𝑦 = 𝐵𝑠𝑖𝑛(𝜔2 𝑡 ∨ +𝜙)

You might also like