Python For Wbsu 1st Sem.
Python For Wbsu 1st Sem.
Interactive Mode
1. Use python as a simple calculator (use arithmetic operators: +, -, *,/, %, **)
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
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
Programming Assignment 1
Wap which inputs three of the components of the vector and outputs
i) the norm ii) angle with each of the axes.
Interactive Mode
Concept of list, tuple, string are to be introduced
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
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?
Assignment Sheet 3
Generate the following the data of the following function by writing codes in
Python. Use gnuplot to see the graph.
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
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 𝑡 ∨ +𝜙)