Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Programming Mock Papers, Exams of Computer Programming

Semester exam level questions on introductory-level physics-oriented programming in Python for beginners. TOPICS - Terminal, Shell, Bash, Scripts, Vim, Nano, Jupyter Notebook, functions and arguments, for and while loops, iterations and nested loops, continue, break, and pass control statements, if, else, and elif conditional statements, nested statements, recursion, random numbers, Numpy, linalg, matplotlib, Scipy, integrate, solve_ivp, dictionary, pandas.

Typology: Exams

2021/2022

Available from 05/09/2023

rishab-kumar-3
rishab-kumar-3 🇮🇳

7 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Section A
(Answer all questions -- do not spend more than 1 hour)
Each question carries
2
marks.
A
1.
Create a list
of
tuples containing the name and age of five imaginary friends.
A2. Using (!) or otherwise, use a bash command
to
output the string, "This
is
a programming lab course!"
A3
.
Define a
recursive
function for f
(x)
=
x
! ,
where
X
!
==
I
*
2
*
3
* ...
x
and f
(0)
=
1.
Print the output for f (
10).
A4. Using a single line statement, create a random 2 x 2 matrix
A
with elements between 0 and 5.
You
can include
an
additional
import
statement.
A5. Create a list from an array
of
100 real numbers between 0
and
1. Extract the last five numbers.
AG.
Define a function that calculates the trace and determinant of a 2 x 2 matrix
A,
and returns a tuple at the output, i.e
.,
output
is
(Tr(A)
,
det(A)).
Section 8
(Answer any 3 question
--
do not spend more than 45 mins
on
each)
Each question carries 6 marks.
81.
From linear algebra (and from elementary quantum mechanics), we know that all Hermitian matrices have real eigenvalues. However,
not all Hermitian matrices have positive eigenvalues
(A;
~
0,
V
i).
a) Write a function that checks
if
a random 2 x 2 matrix
is
Hermitian, both Hermitian and positive or neither.
(3
Marks)
Hint: Use the
numpy
(as
np)
function
np.round(x,
10)
to
round
x
to
nearest
10
decimal points, and eliminate any small imaginary part that
remains due
to
numerical error. Moreover, the commands np.real and np.imag can be used
to
isolate real and imaginary parts. Use the
fact that
for
real eigenvalues, the imaginary part must be equal
to
0.
pf3
pf4
pf5

Partial preview of the text

Download Python Programming Mock Papers and more Exams Computer Programming in PDF only on Docsity!

Section A (Answer all questions -- do not spend more than 1 hour) Each question carries 2 marks.

  • A 1. Create a list of tuples containing the name and age of five imaginary friends.
  • A2. Using (!) or otherwise, use a bash command to output the string , "Thisisa programming lab course!"
  • A3. Define arecursivefunction for f (x) = x !, where X! == I * 2 * 3 * ... x and f (0) = 1. Print the output for f (10).
  • A4. statement. Using a single line statement, create a random 2 x 2 matrix A with elements between 0 and 5. You can include an additionalimport
    • A5. (^) Create a list from an array of 100 real numbers between 0 and 1. Extract the last five numbers.
    • AG. det(A)). Define a function that calculates the trace and determinant of a 2 x 2 matrix A, and returns a tuple at the output, i.e.,output is(Tr(A) ,

Section 8 (Answer any 3 question -- do not spend more than 45 minsoneach) Each question carries 6 marks.

  • 81. From linear algebra (and from elementary quantum mechanics), we know that all Hermitian matrices have real eigenvalues. However, not all Hermitian matrices have positive eigenvalues (A; ~ 0, V i). a) Write a function that checks if a random 2 x 2 matrix is Hermitian, both Hermitian and positive or neither. (3 Marks) Hint: Use theremains due tonumpy numerical error. Moreover, the commands np.real and np.imag can be used (as np) function np.round(x,10) to round x tonearest 10 decimal points, and eliminate any small imaginary part that fact that for real eigenvalues, the imaginary part must be equal toisolate real and imaginary parts. Use the to 0.

b) Write the matrix·· p^ =^ -(^102 +^ a^ a-^ +^ a^ +^ )^ ct^ h^ ·^ ·

dimensional identity matrix. Using the above function^2 l^ x^ 2 0-y^ ato^3 show that^ cr^2.^ Here,^ p^^ O' is Herm iti x,^ O'y^ anan^ O'^ but not positve for y^ are t e usual^ Paull^ a^^ =ma^ {tn l, ce^ l , s^^ a l} , nd^ but^ a^2 isis^ thbothetw^ foro- a= {0,1,0}. (3 Marks)

  • 82. The finite difference formula for the derivative allowsusa simple numerical solutiontoa ordinary differential equation (ODE). For--^ df(x) = /'(x),we^ have,/(x+^ h)^ =^ J(x)^ +^ hf'(x),^ for^ x^ =^ {xo,^ xi,^. .. ,^ x,,}.^ Here,^ h^ =^ x 11^ -^ Xn-l. dx Write a function that solves the ODE ,-- df(x) -^ =/(x),^ for^ the initial state/(0)^ =^ l .Youcan^ use10,100,^ and^ 1000 values of^ x between 0 and5. (3 Marks)^ dx Note^ : You donot^ needto find the derivative explicitly. Simply use the above relation. Plot the solutionsofthe above ODE for the three cases , along with the exact solution (dashed line).^ (3 Marks)
  • 83. The transverse field Ising Hamiltonian for three quantum spins is given by

Here, O'x and O'Y are the usual Pauli matrix and 02 is the two-dimensional identity matrix. a) Write a function that creates H for arguments^ J^ and^ g.^ (3^ Marks) b) Find out the dimensionof^ H.^ (1Mark) c) Find out the eigenvaluesof^ Hand^ write the diagonal form of^ H.^ (1+^ 1 Mark) Note: No transformationsofmatrix required.

  • 84. Wnte^.^ down the function,•^ /( X,^ t)^ - - 3x^2 t^ +^ 4t^ • with arguments^ x^ and^ t.^ Consider^ x^ =^^1 and^ t^ =^2 as pre-defined values while defining the function. (2 Marks)

Section^ A

Answer all questions^ -^ do not spend more than 1 hour. Each question carries^2 marks.

1.^ Define a function that takes anaturalnumber^ n,^ and uses a^ while^ or^ for^ loop^ to outputa list^ which^ contains^ the sum^ and^ productof allnatural^ numbersup^ ton (including^ n).^ Donot^ use^ NumPyor^ SciPy.^ Print^ theoutputfor^ n^ =^ 5.

dx

  1. Solve^ the^ ordinary^ differential^ equation^ given by:^ dt^ =^ x^2 -^ l ,where^ x(O)^ 0.

Evaluate^ and^ print^ x (t)^ for five values^ oft^ in^ the^ range^ t^ E^ [O,^ l].

  1. Create a dictionary 'celeb_dict'^ that^ has^ two^ keys,^ "male"and"female".Undereach key, are embeddeddictionarieswith^ "name",^ "profession"and"state"^ oftworeal orfictitious celebrities, according to^ the^ correct gender.^ Display^ the^ "female"^ key values as^ a^ panda dataframe.
  2. Integrate^ the^ function^ f^ (x )^ =^ x^2 log(x ),^ using^ the^ - trapeziumrule, between^ x^ =^1 and^ x^ =^ 10. Also,^ compare^ thiswiththe^ integrate.quad^ function^ from^ SciPy, and givethe^ answer^ uptothe^ 3rd^ decimal place.
  3. Using^ matplotlib,^ plot^ the^ function^ J(t)^ =^ sin(t)^ +^ 3 cos(t),in^ the^ range^ t^ E^ [O,^ 41r], witha^ red-dashed^ line.^ Use atleast 1000^ plot^ points.^ Label^ the^ y-axis^ as^ f^ (t)^ and x-axis as^ t.^ The title of the^ plot^ should^ be^ "Power^ fluctuation".

Section^ B

Answer all^ questions^ -^ do^ not^ spend^ more than 1.5 hours. Each question^ carries 3+^ 1 marks, where 1 mark^ is^ for clarity^ in^ the answer.

  1. Solve the^ following differential^ equation^ for^ the^ state, x^ =^ [x^0 ,^ x^1 ,^ x^2 ]:

The initial state is,^ x(O)^ =^ [1, 1 , 1).^ i)Evaluate the above at 100 values of time^ in^ the range, t^ E [O,10). Store the valuesoft,^ x^0 ,^ x^1 ,^ and^ x^2 ina dictionary (with readable labels) andsave it in a json file, "your_rollno.json"and^ submit^ the file. ii) Plot^ X -i^ vs t, Vi on the^ same^ plot,^ using^ matplotlib.^ Use legends for the different plots.

  1. Say the^ tuple^ (x,^ y)^ represents a complex number,^ x+iy.^ Define a functionthattakes two tuples (or two complex^ numbers)andtheoutputis^ another^ tuple,whichis^ the productof the two^ complex^ numbers.^ DonotuseNumPyor^ SciPy. Using the above function,^ multiply^ the^ numbers^3 +^ 2iand^2 +^ 4i. 3.^ Sayyou^ areina casinoandare^ throwing^ a^ pair^ of dies. Using^ np.random.randint(), write a single line of code^ that^ simulates a 10000^ throws^ of^ thepairof dies. Write a code^ that^ countsi)the probability of getting^ 8,^ ii) the^ probability^ of^ getting a"hard"8 (samenumber^ on^ bothdie),andiii) the probability^ of^ gettingan^ "easy" 8 (differentnumber^ oneach die).^ Compare^ with^ exact calculations.

-^ -

(a) A hard 8 (b)^ An^ easy 8

  1. Using pandas, import^ the^ "ipLdata.csv"^ file.^ Do^ the^ following:^ i)^ Createand^ print the^ smaller^ panda^ dataframe^ that^ contains^ the^ values^ corresponding^ to^ season,^ host city and winners^ (please check^ the^ exact key).^ ii)^ Count^ the^ number^ of^ times^ Kolkata Knight Riders have won^ in^ 2010,^ when^ they^ have^ hosted^ the^ match^ in^ Kolkata.
  2. Define a^ function^ that^ takes a^ n^ x^ n^ matrix^ A^ as^ an^ input^ and^ outputs^ a^ n^ x^ n^ -^1 matrix B containing^ only^ the^ off-diagonal elements. Using numpy define^ A^ as a^ 4-dimensional^ identity^ matrix,^ and^ show^ that^ the^ out- put B is a null-matrix.