diff options
author | Jovina Dsouza | 2014-06-18 13:08:52 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 13:08:52 +0530 |
commit | a78126bbe4443e9526a64df9d8245c4af8843044 (patch) | |
tree | ec84778a693a93a076d64803afbe814b8f5c99be | |
parent | 206d0358703aa05d5d7315900fe1d054c2817ddc (diff) | |
download | Python-Textbook-Companions-a78126bbe4443e9526a64df9d8245c4af8843044.tar.gz Python-Textbook-Companions-a78126bbe4443e9526a64df9d8245c4af8843044.tar.bz2 Python-Textbook-Companions-a78126bbe4443e9526a64df9d8245c4af8843044.zip |
adding book
57 files changed, 10877 insertions, 0 deletions
diff --git a/Engineering_Physics_Aruldhas/Chapter10_1.ipynb b/Engineering_Physics_Aruldhas/Chapter10_1.ipynb new file mode 100644 index 00000000..b31ad5ec --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter10_1.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter10" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "10: Statistical Mechanics" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.1, Page number 222" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the ratio of occupancy of the excited to the ground state\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nk = 1.38*10**-23; #Boltzmann constant(J/K)\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\ng1 = 2; #The degeneracy of ground state\ng2 = 8; #The degeneracy of excited state\ndelta_E = 10.2; #Energy of excited state above the ground state(eV)\nT = 6000; #Temperature of the state(K)\n\n#Calculation\nD_ratio = g2/g1; #Ratio of degeneracy of states\nx = k*T/e;\nN_ratio = D_ratio*math.exp(-delta_E/x); #Ratio of occupancy of the excited to the ground state\n\n#Result\nprint \"The ratio of occupancy of the excited to the ground state is\",N_ratio", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The ratio of occupancy of the excited to the ground state is 1.10167326887e-08\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.2, Page number 222" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the ground state energy of 10 non-interacting bosons\n\n#Calculation\n#an energy level can accomodate any number of bosons. Hence 10 bosons will be in n=1 state\n#energy is given by E1 = (pi**2*h**2)/(2*m*a**2)\na = 10/2;\n#enegy of 10 bosons is E = (10*pi**2*h**2)/(2*m*a**2) = (5*pi**2*h**2)/(m*a**2)\n\n#Result\nprint \"enegy of 10 bosons is E = \",int(a),\"(pi**2*h**2)/(m*a**2)\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "enegy of 10 bosons is E = 5 (pi**2*h**2)/(m*a**2)\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.3, Page number 223" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the ground state energy of the system\n\n#importing modules\nimport math\n\n#Variable declaration\nn1=1; #1st level\nn2=2; #2nd level\nn3=3; #3rd level\nn4=4; #4th level\nn5=5; #5th level\n\n#Calculation\n#an energy level can accomodate only 2 fermions. hence there will be 2 fermions in each level\n#thus total ground state energy will be E = (2*E1)+(2*E2)+(2*E3)+(2*E4)+E5\n#let X = ((pi**2)*(h**2)/(2*m*a**2)). E = X*((2*n1**2)+(2*n2**2)+(2*n3**2)+(2*n4**2)+(n5**2))\nA = (2*n1**2)+(2*n2**2)+(2*n3**2)+(2*n4**2)+(n5**2);\n#thus E = A*X\n\n#Result\nprint \"the ground state energy of the system is\",A,\"(pi**2)*(h**2)/(2*m*a**2)\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "the ground state energy of the system is 85 (pi**2)*(h**2)/(2*m*a**2)\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.4, Page number 223" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number density of conduction electrons and Fermi energy of silver\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nN_A = 6.02*10**23; #Avogadro's number\nh = 6.626*10**-34; #Planck's constant(Js)\nme = 9.1*10**-31; #Mass of electron(kg)\nrho = 10.5; #Density of silver(g/cm)\nm = 108; #Molecular mass of silver(g/mol)\n\n#Calculation\nN_D = rho*N_A/m; #Number density of conduction electrons(per cm**3)\nN_D = N_D*10**6; #Number density of conduction electrons(per m**3)\nE_F = ((h**2)/(8*me))*(3/math.pi*N_D)**(2/3); #fermi energy(J)\nE_F = E_F/e; #fermi energy(eV)\nE_F = math.ceil(E_F*10**2)/10**2; #rounding off the value of E_F to 2 decimals\n\n#Result\nprint \"The number density of conduction electrons is\",N_D, \"per metre cube\"\nprint \"The Fermi energy of silver is\",E_F, \"eV\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The number density of conduction electrons is 5.85277777778e+28 per metre cube\nThe Fermi energy of silver is 5.51 eV\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.5, Page number 224" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electronic contribution to molar specific heat of sodium\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nN_A = 6.02*10**23; #Avogadro's number\nk = 1.38*10**-23; #Boltzmann constant(J/K)\nT = 293; #Temperature of sodium(K)\nE_F = 3.24; #Fermi energy of sodium(eV)\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\n\n#Calculation\nC_v = math.pi**2*N_A*k**2*T/(2*E_F*e); #Molar specific heat of sodium(per mole)\nC_v = math.ceil(C_v*10**2)/10**2; #rounding off the value of C_v to 2 decimals\n\n#Result\nprint \"The electronic contribution to molar specific heat of sodium is\",C_v, \"per mole\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The electronic contribution to molar specific heat of sodium is 0.32 per mole\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.6, Page number 224" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Fermi energy and mean energy of the electron\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nh = 6.626*10**-34; #Planck's constant(Js)\nm = 9.1*10**-31; #Mass of the electron(kg)\nN_D = 18.1*10**28; #Number density of conduction electrons in Al(per metre cube)\n\n#Calculation\nE_F = h**2/(8*m)*(3/math.pi*N_D)**(2/3); #N_D = N/V. Fermi energy of aluminium(J)\nE_F = E_F/e; #Fermi energy of aluminium(eV)\nE_F = math.ceil(E_F*10**3)/10**3; #rounding off the value of E_F to 3 decimals\nEm_0 = 3/5*E_F; #Mean energy of the electron at 0K(eV)\nEm_0 = math.ceil(Em_0*10**3)/10**3; #rounding off the value of Em_0 to 3 decimals\n\n#Result\nprint \"The Fermi energy of aluminium is\",E_F, \"eV\"\nprint \"The mean energy of the electron is\",Em_0, \"eV\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Fermi energy of aluminium is 11.696 eV\nThe mean energy of the electron is 7.018 eV\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter11_1.ipynb b/Engineering_Physics_Aruldhas/Chapter11_1.ipynb new file mode 100644 index 00000000..733fe6b7 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter11_1.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter11" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "11: Lasers" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.1, Page number 249" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the ratio of spontaneous emission to stimulated emission for visible and microwave region\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nh = 6.626*10**-34; #Planck's constant(Js)\nc = 3*10**8; #Speed of light in free space(m/s)\nk = 1.38*10**-23; #Boltzmann constant(J/K)\nT = 300; #Temperature at absolute scale(K)\nlamda1 = 5500; #Wavelength of visible light(A)\nlamda2 = 10**-2; #Wavelength of microwave(m)\n\n#Calculation\nlamda1 = lamda1*10**-10; #Wavelength of visible light(m)\nrate_ratio = math.exp(h*c/(lamda1*k*T))-1; #Ratio of spontaneous emission to stimulated emission\nrate_ratio1 = math.exp(h*c/(lamda2*k*T))-1; #Ratio of spontaneous emission to stimulated emission\nrate_ratio1 = math.ceil(rate_ratio1*10**5)/10**5; #rounding off the value of rate_ratio1 to 5 decimals\n\n#Result\nprint \"The ratio of spontaneous emission to stimulated emission for visible region is\",rate_ratio\nprint \"The ratio of spontaneous emission to stimulated emission for microwave region is\", rate_ratio1", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The ratio of spontaneous emission to stimulated emission for visible region is 8.19422217477e+37\nThe ratio of spontaneous emission to stimulated emission for microwave region is 0.00482\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.2, Page number 250" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the energy of excited state\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nh = 6.626*10**-34; #Planck's constant(Js)\nc = 3*10**8; #Speed of light in free space(m/s)\nlamda = 690; #Wavelength of laser light(nm)\nE_lower = 30.5; #Energy of lower state(eV)\n\n#Calculation\nlamda = lamda*10**-9; #Wavelength of laser light(m)\nE = h*c/lamda; #Energy of the laser light(J)\nE = E/e; #Energy of the laser light(eV)\nE_ex = E_lower + E; #Energy of excited state of laser system(eV)\nE_ex = math.ceil(E_ex*10**2)/10**2; #rounding off the value of E_ex to 2 decimals\n\n#Result\nprint \"The energy of excited state of laser system is\",E_ex, \"eV\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The energy of excited state of laser system is 32.31 eV\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.3, Page number 250" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To determine the condition under which stimulated emission equals spontaneous emission\n\n#importing modules\nimport math\nfrom __future__ import division\nimport numpy as np\n\n#Variable declaration\nh = 6.626*10**-34; #Planck's constant(Js)\nk = 1.38*10**-23; #Boltzmann constant(J/K)\n\n#Calculation\n#Stimulated Emission = Spontaneous Emission <=> exp(h*f/(k*T))-1 = 1 i.e.\n#f/T = log(2)*k/h = A\nA = np.log(2)*k/h; #Frequency per unit temperature(Hz/K)\nA = A/10**10;\nA = math.ceil(A*10**3)/10**3; #rounding off the value of A to 3 decimals\n\n#Result\nprint \"The stimulated emission equals spontaneous emission iff f/T =\",A,\"*10**10 Hz/k\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The stimulated emission equals spontaneous emission iff f/T = 1.444 *10**10 Hz/k\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.4, Page number 250" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the area of the spot and intensity at the focus \n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nlamda = 500; #Wavelength of laser light(nm)\nf = 15; #Focal length of the lens(cm)\nd = 2; #Diameter of the aperture of source(cm)\nP = 5; #Power of the laser(mW)\n\n#Calculation\nP = P*10**-3; #Power of the laser(W)\nlamda = lamda*10**-9; #Wavelength of laser light(m)\nd = d*10**-2; #Diameter of the aperture of source(m)\nf = f*10**-2; #Focal length of the lens(m)\na = d/2; #Radius of the aperture of source(m)\nA = math.pi*lamda**2*f**2/a**2; #Area of the spot at the focal plane, metre square\nI = P/A; #Intensity at the focus(W/m**2)\nI = I/10**7;\nI = math.ceil(I*10**4)/10**4; #rounding off the value of I to 1 decimal\n\n#Result\nprint \"The area of the spot at the focal plane is\",A, \"m**2\"\nprint \"The intensity at the focus is\",I,\"*10**7 W/m**2\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The area of the spot at the focal plane is 1.76714586764e-10 m**2\nThe intensity at the focus is 2.8295 *10**7 W/m**2\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.5, Page number 251" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the energy released per pulse and number of photons\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nh = 6.626*10**-34; #Planck's constant(Js)\nc = 3*10**8; #Speed of light in free space(m/s)\nlamda = 1064; #Wavelength of laser light(nm)\nP = 0.8; #Average power output per laser pulse(W)\ndt = 25; #Pulse width of laser(ms)\n\n#Calculation\ndt = dt*10**-3; #Pulse width of laser(s)\nlamda = lamda*10**-9; #Wavelength of laser light(m)\nE = P*dt; #Energy released per pulse(J)\nE1 = E*10**3;\nN = E/(h*c/lamda); #Number of photons in a pulse\n\n#Result\nprint \"The energy released per pulse is\",E1,\"*10**-3 J\"\nprint \"The number of photons in a pulse is\", N\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The energy released per pulse is 20.0 *10**-3 J\nThe number of photons in a pulse is 1.07053023443e+17\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.6, Page number 251" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the angular spread and diameter of the beam\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nlamda = 693; #Wavelength of laser beam(nm)\nD = 3; #Diameter of laser beam(mm)\nd = 300; #Height of a satellite above the surface of earth(km)\n\n#Calculation\nD = D*10**-3; #Diameter of laser beam(m)\nlamda = lamda*10**-9; #Wavelength of laser beam(m)\nd = d*10**3; #Height of a satellite above the surface of earth(m)\nd_theta = 1.22*lamda/D; #Angular spread of laser beam(rad)\ndtheta = d_theta*10**4;\ndtheta = math.ceil(dtheta*10**2)/10**2; #rounding off the value of dtheta to 2 decimals\na = d_theta*d; #Diameter of the beam on the satellite(m)\na = math.ceil(a*10)/10; #rounding off the value of a to 1 decimal\n\n#Result\nprint \"The height of a satellite above the surface of earth is\",dtheta,\"*10**-4 rad\"\nprint \"The diameter of the beam on the satellite is\",a, \"m\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The height of a satellite above the surface of earth is 2.82 *10**-4 rad\nThe diameter of the beam on the satellite is 84.6 m\n" + } + ], + "prompt_number": 25 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter12_1.ipynb b/Engineering_Physics_Aruldhas/Chapter12_1.ipynb new file mode 100644 index 00000000..440b2d59 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter12_1.ipynb @@ -0,0 +1,118 @@ +{ + "metadata": { + "name": "Chapter12" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "12: Holography and Fibre Optics" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.1, Page number 271" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the critical angle, critical propagation angle and numerical aperture \n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nn1 = 1.43; #Refractive index of fibre core\nn2 = 1.4; #Refractive index of fibre cladding\n\n#Calculation\n#As sin (alpha_c) = n2/n1, solving for alpha_c\nalpha_c = math.asin(n2/n1); #Critical angle for optical fibre(rad)\nalpha_c = alpha_c*57.2957795; #Critical angle for optical fibre(degrees)\nalpha_c = math.ceil(alpha_c*10**3)/10**3; #rounding off the value of alpha_c to 3 decimals\n#AS cos(theta_c) = n2/n1, solving for theta_c\ntheta_c = math.acos(n2/n1); #Critical propagation angle for optical fibre(rad)\ntheta_c = theta_c*57.2957795; #Critical propagation angle for optical fibre(degrees)\ntheta_c = math.ceil(theta_c*10**2)/10**2; #rounding off the value of theta_c to 2 decimals\nNA = math.sqrt(n1**2 - n2**2); #Numerical aperture for optical fibre\nNA = math.ceil(NA*10**3)/10**3; #rounding off the value of NA to 3 decimals\n\n#Result\nprint \"The critical angle for optical fibre is\",alpha_c, \"degrees\"\nprint \"The critical propagation angle for optical fibre is\",theta_c, \"degrees\"\nprint \"Numerical aperture for optical fibre is\",NA\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The critical angle for optical fibre is 78.244 degrees\nThe critical propagation angle for optical fibre is 11.76 degrees\nNumerical aperture for optical fibre is 0.292\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.2, Page number 271" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the numerical aperture, acceptance angle and relative refractive index difference\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nn1 = 1.45; #Refractive index of fibre core\nn2 = 1.4; #Refractive index of fibre cladding\n\n#Calculation\nNA = math.sqrt(n1**2 - n2**2); #Numerical aperture for optical fibre\nNA = math.ceil(NA*10**4)/10**4; #rounding off the value of NA to 4 decimals\n#As sin(theta_a) = sqrt(n1^2 - n2^2), solving for theta_a\ntheta_a = math.asin(math.sqrt(n1**2 - n2**2)); #Half of acceptance angle of optical fibre(rad)\ntheta_a = theta_a*57.2957795; #Half of acceptance angle of optical fibre(degrees)\ntheta_accp = 2*theta_a; #Acceptance angle of optical fibre(degrees)\ntheta_accp = math.ceil(theta_accp*10**2)/10**2; #rounding off the value of theta_accp to 2 decimals\nDelta = (n1 - n2)/n1; #Relative refractive index difference\nDelta = math.ceil(Delta*10**4)/10**4; #rounding off the value of Delta to 4 decimals\n\n#Result\nprint \"Numerical aperture for optical fibre is\", NA\nprint \"The acceptance angle of optical fibre is\",theta_accp, \"degrees\"\nprint \"Relative refractive index difference is\", Delta\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Numerical aperture for optical fibre is 0.3775\nThe acceptance angle of optical fibre is 44.36 degrees\nRelative refractive index difference is 0.0345\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.3, Page number 271" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the numerical aperture and acceptance angle\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nn1 = 1.55; #Refractive index of fibre core\nn2 = 1.53; #Refractive index of fibre cladding\nn0 = 1.3; #Refractive index of medium\n\n#Calculation\nNA = math.sqrt(n1**2 - n2**2); #Numerical aperture for optical fibre\nNA = math.ceil(NA*10**4)/10**4; #rounding off the value of NA to 4 decimals\n#n0*sin(theta_a) = sqrt(n1^2 - n2^2) = NA, solving for theta_a\ntheta_a = math.asin(math.sqrt(n1**2 - n2**2)/n0); #Half of acceptance angle of optical fibre(rad)\ntheta_a = theta_a*57.2957795; #Half of acceptance angle of optical fibre(degrees)\ntheta_accp = 2*theta_a; #Acceptance angle of optical fibre(degrees)\n\n#Result\nprint \"Numerical aperture for step index fibre is\",NA\nprint \"The acceptance angle of step index fibre is\",int(theta_accp), \"degrees\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Numerical aperture for step index fibre is 0.2482\nThe acceptance angle of step index fibre is 22 degrees\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.4, Page number 271 Theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.5, Page number 272" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the numerical aperture and acceptance angle\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nalpha = 2; #Power loss through optical fibre(dB/km)\nP_in = 500; #Poer input of optical fibre(micro-watt)\nz = 10; #Length of the optical fibre(km)\n\n#Calculation\n#As alpha = 10/z*log10(P_in/P_out), solving for P_out\nP_out = P_in/10**(alpha*z/10); #Output power in fibre optic communication(micro-Watt)\n\n#Result\nprint \"The output power in fibre optic communication is\",P_out, \"micro-Watt\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The output power in fibre optic communication is 5.0 micro-Watt\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter13_1.ipynb b/Engineering_Physics_Aruldhas/Chapter13_1.ipynb new file mode 100644 index 00000000..2366432d --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter13_1.ipynb @@ -0,0 +1,175 @@ +{ + "metadata": { + "name": "Chapter13" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "13: Dielectric Properties of Materials" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 13.1, Page number 287" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electronic polarizability \n\n#importing modules\nimport math\n\n#Variable declaration\nepsilon_0 = 8.85*10**-12; #Absolute electrical permittivity of free space(F/m)\nR = 0.52; #Radius of hydrogen atom(A)\nn = 9.7*10**26; #Number density of hydrogen(per metre cube)\n\n#Calculation\nR = R*10**-10; #Radius of hydrogen atom(m)\nalpha_e = 4*math.pi*epsilon_0*R**3; #Electronic polarizability of hydrogen atom(Fm**2)\n\n#Result\nprint \"The electronic polarizability of hydrogen atom is\", alpha_e, \"Fm**2\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The electronic polarizability of hydrogen atom is 1.56373503182e-41 Fm**2\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 13.2, Page number 287" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the capacitance of capacitor and charge on the plates\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nepsilon_0 = 8.854*10**-12; #Absolute electrical permittivity of free space(F/m)\nA = 100; #Area of a plate of parallel plate capacitor(cm**2)\nd = 1; #Distance between the plates of the capacitor(cm)\nV = 100; #Potential applied to the plates of the capacitor(V)\n\n#Calculation\nA= A*10**-4; #Area of a plate of parallel plate capacitor(m**2)\nd = d*10**-2; #Distance between the plates of the capacitor(m)\nC = epsilon_0*A/d; #Capacitance of parallel plate capacitor(F)\nQ = C*V; #Charge on the plates of the capacitor(C)\n\n#Result\nprint \"The capacitance of parallel plate capacitor is\",C, \"F\"\nprint \"The charge on the plates of the capacitor is\",Q, \"C\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The capacitance of parallel plate capacitor is 8.854e-12 F\nThe charge on the plates of the capacitor is 8.854e-10 C\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 13.3, Page number 288" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the dielectric displacement\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nepsilon_0 = 8.854*10**-12; #Absolute electrical permittivity of free space(F/m)\nepsilon_r = 5.0; #Dielectric constant of the material between the plates of capacitor\nV = 15; #Potential difference applied between the plates of the capacitor(V)\nd = 1.5; #Separation between the plates of the capacitor(mm)\n\n#Calculation\nd = d*10**-3; #Separation between the plates of the capacitor(m)\n#Electric displacement, D = epsilon_0*epsilon_r*E, as E = V/d, so \nD = epsilon_0*epsilon_r*V/d; #Dielectric displacement(C/m**2)\n\n#Result\nprint \"The dielectric displacement is\",D, \"C/m**2\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The dielectric displacement is 4.427e-07 C/m**2\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 13.4, Page number 288" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the relative dielectric constant\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nepsilon_0 = 8.854*10**-12; #Absolute electrical permittivity of free space(F/m)\nN = 3*10**28; #Number density of solid elemental dielectric(atoms/metre cube)\nalpha_e = 10**-40; #Electronic polarizability(Fm**2)\n\n#Calculation\nepsilon_r = 1 + (N*alpha_e/epsilon_0); #Relative dielectric constant of the material\nepsilon_r = math.ceil(epsilon_r*10**3)/10**3; #rounding off the value of epsilon_r to 3 decimals\n\n#Result\nprint \"The Relative dielectric constant of the material is\",epsilon_r\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Relative dielectric constant of the material is 1.339\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 13.5, Page number 288" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electronic polarizability\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nN_A = 6.02*10**23; #Avogadro's number(per mole)\nepsilon_0 = 8.854*10**-12; #Absolute electrical permittivity of free space(F/m)\nepsilon_r = 3.75; #Relative dielectric constant\nd = 2050; #Density of sulphur(kg/metre cube)\ny = 1/3; #Internal field constant\nM = 32; #Atomic weight of sulphur(g/mol)\n\n#Calculation\nN = N_A*10**3*d/M; #Number density of atoms of sulphur(per metre cube)\n#Lorentz relation for local fields give E_local = E + P/(3*epsilon_0) which gives\n#(epsilon_r - 1)/(epsilon_r + 2) = N*alpha_e/(3*epsilon_0), solving for alpha_e\nalpha_e = (epsilon_r - 1)/(epsilon_r + 2)*3*epsilon_0/N; #Electronic polarizability of sulphur(Fm**2)\n\n#Result\nprint \"The electronic polarizability of sulphur is\",alpha_e, \"Fm**2\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The electronic polarizability of sulphur is 3.2940125351e-40 Fm**2\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 13.6, Page number 289" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electronic polarizability\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nN = 3*10**28; #Number density of atoms of dielectric material(per metre cube)\nepsilon_0 = 8.854*10**-12; #Absolute electrical permittivity of free space(F/m)\nn = 1.6; #Refractive index of dielectric material\n\n#Calculation\n#As (n^2 - 1)/(n^2 + 2) = N*alpha_e/(3*epsilon_0), solving for alpha_e\nalpha_e = (n**2 - 1)/(n**2 + 2)*3*epsilon_0/N; #Electronic polarizability of dielectric material(Fm**2)\n\n#Result\nprint \"The electronic polarizability of dielectric material is\",alpha_e, \"Fm**2\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The electronic polarizability of dielectric material is 3.029e-40 Fm**2\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 13.7, Page number 289" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the ratio of electronic polarizability to ionic polarizability\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nepsilon_r = 4.9; #Absolute relative dielectric constant of material(F/m)\nn = 1.6; #Refractive index of dielectric material\n\n#Calculation\n#As (n^2 - 1)/(n^2 + 2)*(alpha_e + alpha_i)/alpha_e = N*(alpha_e + alpha_i)/(3*epsilon_0) = (epsilon_r - 1)/(epsilon_r + 2)\n#let alpha_ratio = alpha_i/alpha_e\nalpha_ratio = ((epsilon_r - 1)/(epsilon_r + 2)*(n**2 + 2)/(n**2 - 1) - 1)**(-1); #Ratio of electronic polarizability to ionic polarizability\nalpha_ratio = math.ceil(alpha_ratio*10**3)/10**3; #rounding off the value of alpha_ratio to 3 decimals\n\n#Result\nprint \"The ratio of electronic polarizability to ionic polarizability is\",alpha_ratio", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The ratio of electronic polarizability to ionic polarizability is 1.534\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter14_1.ipynb b/Engineering_Physics_Aruldhas/Chapter14_1.ipynb new file mode 100644 index 00000000..eb6cc153 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter14_1.ipynb @@ -0,0 +1,175 @@ +{ + "metadata": { + "name": "Chapter14" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "14: Magnetic Properties of Materials" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 14.1, Page number 306" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the spontaneous magnetisation\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nN = 6.02*10**23; #Avogadro's number(per mole)\nA = 56; #Atomic weight of the substance(g/mole)\nd = 7.9; #Density of the substance(g/cm**3)\nm_B = 9.27*10**-24; #Bohr's Magneton(J/T)\n\n#Calculation\nm = 2.2*m_B; #Magnetic moment of substance(J/T)\nn = d*N/A ; #Number of atoms per unit volume of the substance(per cm**3)\nn = n*10**6; #Number of atoms per unit volume of the substance(per m**3)\nM = n*m; #Spontaneous magnetisation of the substance(A/m)\nM = M/10**6;\nM = math.ceil(M*10**3)/10**3; #rounding off the value of M to 3 decimals\n\n#Result\nprint \"The spontaneous magnetisation of the substance is\",M,\"*10**6 A/m\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The spontaneous magnetisation of the substance is 1.732 *10**6 A/m\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 14.2, Page number 307" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the relative permeability\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nH = 200; #Field strength to which the ferromagnetic material is subjected(A/m)\nM = 3100; #Magnetisation of the ferromagnetic material(A/m)\n\n#Calculation\nchi = M/H; #Magnetic susceptibility\nmew_r = 1 + chi; #Relative permeability of ferromagnetic material\n\n#Result\nprint \"The relative permeability of ferromagnetic material is\",mew_r", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The relative permeability of ferromagnetic material is 16.5\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 14.3, Page number 307" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the relative permeability\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nH = 300; #Field strength to which the ferromagnetic material is subjected(A/m)\nM = 4400; #Magnetisation of the ferromagnetic material(A/m)\n\n#Calculation\nchi = M/H; #Magnetic susceptibility\nmew_r = 1 + chi; #Relative permeability of ferromagnetic material\nmew_r = math.ceil(mew_r*100)/100; #rounding off the value of mew_r to 2 decimals\n\n#Result\nprint \"The relative permeability of ferromagnetic material is\",mew_r\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The relative permeability of ferromagnetic material is 15.67\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 14.4, Page number 307" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the magnetisation and flux density\n\n#importing modules\nimport math\n\n#Variable declaration\nmew_0 = 4*math.pi*10**-7; #Magnetic permeability of free space(Tm/A)\nH = 10000; #Field strength to which the diamagnetic material is subjected(A/m)\nchi = -0.4*10**-5; #Magnetic susceptibility\n\n#Calculation\nM = chi*H; #Magnetisation of the diamagnetic material(A/m)\nB = mew_0*(H + M); #Magnetic flux density of diamagnetic material(T)\nB = math.ceil(B*10**4)/10**4; #rounding off the value of B to 4 decimals\n\n#Result\nprint \"The magnetisation of diamagnetic material is\",M, \"A/m\"\nprint \"The magnetic flux density of diamagnetic material is\",B, \"T\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The magnetisation of diamagnetic material is -0.04 A/m\nThe magnetic flux density of diamagnetic material is 0.0126 T\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 14.5, Page number 307" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the magnetisation, relative permeability and flux density\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nmew_0 = 4*math.pi*10**-7; #Magnetic permeability of free space(Tm/A)\nH = 1.2*10**5; #Field strength to which the diamagnetic material is subjected(A/m)\nchi = -4.2*10**-6; #Magnetic susceptibility\n\n#Calculation\nM = chi*H; #Magnetisation of the diamagnetic material(A/m)\nB = mew_0*(H + M); #Magnetic flux density of diamagnetic material(T)\nB = math.ceil(B*10**3)/10**3; #rounding off the value of B to 3 decimals\nmew_r = M/H + 1; #The relative permeability of diamagnetic material\nmew_r = math.ceil(mew_r*10**6)/10**6; #rounding off the value of mew_r to 6 decimals\n\n#Result\nprint \"The magnetisation of diamagnetic material is\",M, \"A/m\"\nprint \"The magnetic flux density of diamagnetic material is\",B, \"T\"\nprint \"The relative permeability of diamagnetic material is\",mew_r\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The magnetisation of diamagnetic material is -0.504 A/m\nThe magnetic flux density of diamagnetic material is 0.151 T\nThe relative permeability of diamagnetic material is 0.999996\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 14.6, Page number 308" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the mean radius of the atom\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nchi = 5.6*10**-6; #Magnetic susceptibility of diamagnetic material\nm = 9.1*10**-31; #Mass of an electron(kg)\nmew_0 = 4*math.pi*10**-7; #Magnetic permeability of free space(Tm/A)\nZ = 1; #Atomic number\ne = 1.6*10**-19; #Electronic charge(C)\na = 2.53; #Lattice parameter of bcc structure(A)\n\n#Calculation\na = a*10**-10; #Lattice parameter of bcc structure(m)\nN = 2/a**3; #The number of electrons per unit volume(per metre cube)\nr = math.sqrt(chi*6*m/(mew_0*Z*e**2*N)); #Mean radius of body centered cubic structure(m)\nr = r*10**10; #Mean radius of body centered cubic structure(A)\nr = math.ceil(r*100)/100; #rounding off the value of r to 2 decimals\n\n#Result\nprint \"The mean radius of body centered cubic structure is\",r, \"A\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The mean radius of body centered cubic structure is 0.88 A\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 14.7, Page number 308" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the magnetic susceptibility and magnetisation\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nmew_0 = 4*math.pi*10**-7; #Magnetic permeability of free space(Tm/A)\nN_A = 6.02*10**26; #Avogadro's number(per kmol)\nrho = 4370; #Density of paramegnetic salt(kg/metre cube)\nM = 168.5; #Molecular weight of paramagnetic salt(g/mol)\nT = 27; #Temperature of paramagnetic salt(C)\nH = 2*10**5; #Field strength to which the paramagnetic salt is subjected(A/m)\nmew_B = 9.27*10**-24; #Bohr's magneton(Am**2)\np = 2; #Number of Bohr magnetons per molecule\nk = 1.38*10**-23; #Boltzmann constant(J/K)\n\n#Calculation\nT = T+273; #Temperature of paramagnetic salt(K)\nN = rho*N_A/M; #Total density of atoms in the paramagnetic salt(per meter cube)\nchi_para = mew_0*N*p**2*mew_B**2/(3*k*T); #Magnetic susceptibility of paramagnetic salt\nchi_para = chi_para*10**4;\nchi_para = math.ceil(chi_para*10**2)/10**2; #rounding off the value of chi_para to 2 decimals\nM = chi*H; #Magnetisation of paramagnetic salt(A/m)\nM = math.ceil(M*10)/10; #rounding off the value of M to 1 decimal\n\n#Result\nprint \"The magnetic susceptibility of paramagnetic salt is\",chi_para,\"*10**-4\"\nprint \"The magnetisation of paramagnetic salt is\",M, \"A/m\"\n\n#answer for magnetisation is not given in the textbook", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The magnetic susceptibility of paramagnetic salt is 5.43 *10**-4\nThe magnetisation of paramagnetic salt is 1.2 A/m\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter15_1.ipynb b/Engineering_Physics_Aruldhas/Chapter15_1.ipynb new file mode 100644 index 00000000..cfabc2c6 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter15_1.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter15" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "15: Thermal Properties " + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 15.1, Page number 323" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Debye temperature\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nk = 1.38*10**-23; #Boltzmann constant(J/K)\nh = 6.626*10**-34; #Planck's constant(Js)\nf_D = 64*10**11; #Debye frequency for Al(Hz)\n\n#Calculation\ntheta_D = h*f_D/k; #Debye temperature(K)\ntheta_D = math.ceil(theta_D*10)/10; #rounding off the value of theta_D to 1 decimal\n\n#Result\nprint \"The Debye temperature of aluminium is\",theta_D, \"K\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Debye temperature of aluminium is 307.3 K\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 15.2, Page number 323" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the lattice specific heat\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nN = 6.02*10**26; #Avogadro's number(per kmol)\nk = 1.38*10**-23; #Boltzmann constant(J/K)\nh = 6.626*10**-34; #Planck's constant(Js)\nf_D = 40.5*10**12; #Debye frequency for Al(Hz)\nT = 30; #Temperature of carbon(Ks)\n\n#Calculation\ntheta_D = h*f_D/k; #Debye temperature(K)\nC_l = 12/5*math.pi**4*N*k*(T/theta_D)**3; #Lattice specific heat of carbon(J/k-mol/K)\nC_l = math.ceil(C_l*10**3)/10**3; #rounding off the value of C_l to 3 decimals\n\n#Result\nprint \"The lattice specific heat of carbon is\",C_l, \"J/k-mol/K\"\n\n#answer given in the book is wrong in the 2nd decimal", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The lattice specific heat of carbon is 7.132 J/k-mol/K\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 15.3, Page number 323" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To show that the frequency falls in the infrared region\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nk = 1.38*10**-23; #Boltzmann constant(J/K)\nh = 6.626*10**-34; #Planck's constant(Js)\ntheta_E = 1990; #Einstein temperature of Cu(K)\n\n#Calculation\nf_E = k*theta_E/h; #Einstein frequency for Cu(K)\n\n#Result\nprint \"The Einstein frequency for Cu is\",f_E, \"Hz\"\nprint \"The frequency falls in the near infrared region\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Einstein frequency for Cu is 4.14458194989e+13 Hz\nThe frequency falls in the near infrared region\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 15.4, Page number 323" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electronic and lattice heat capacities\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nN = 6.02*10**23; #Avogadro's number(per mol)\nT = 0.05; #Temperature of Cu(K)\nE_F = 7; #Fermi energy of Cu(eV)\nk = 1.38*10**-23; #Boltzmann constant(J/K)\nh = 6.626*10**-34; #Planck's constant(Js)\ntheta_D = 348; #Debye temperature of Cu(K)\n\n#Calculation\nC_e = math.pi**2*N*k**2*T/(2*E_F*e); #Electronic heat capacity of Cu(J/mol/K)\nC_V = (12/5)*math.pi**4*(N*k)*(T/theta_D)**3; #Lattice heat capacity of Cu(J/mol/K)\n\n#Result\nprint \"The electronic heat capacity of Cu is\",C_e, \"J/mol/K\"\nprint \"The lattice heat capacity of Cu is\",C_V, \"J/mol/K\"\n\n#answer for lattice heat capacity given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The electronic heat capacity of Cu is 2.52566877726e-05 J/mol/K\nThe lattice heat capacity of Cu is 5.76047891492e-09 J/mol/K\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 15.5, Page number 324" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the heat capacity\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nT = 1; #For simplicity assume temperature to be unity(K)\nR = 1; #For simplicity assume molar gas constant to be unity(J/mol/K)\ntheta_E = T; #Einstein temperature(K)\n\n#Calculation\nC_V = 3*R*(theta_E/T)**2*math.exp(theta_E/T)/(math.exp(theta_E/T)-1)**2; #Einstein lattice specific heat(J/mol/K)\nC_V = C_V/3;\nC_V = math.ceil(C_V*10**3)/10**3; #rounding off the value of C_V to 3 decimals\n\n#Result\nprint \"The Einstein lattice specific heat is\",C_V, \"X 3R\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Einstein lattice specific heat is 0.921 X 3R\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 15.6, Page number 324" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the molar electronic heat capacity\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nv = 2; #Valency of Zn atom\nN = v*6.02*10**23; #Avogadro's number(per mol)\nT = 300; #Temperature of Zn(K)\nE_F = 9.38; #Fermi energy of Zn(eV)\nk = 1.38*10**-23; #Boltzmann constant(J/K)\nh = 6.626*10**-34; #Planck's constant(Js)\n\n#Calculation\nN = v*6.02*10**23; #Avogadro's number(per mol)\nC_e = math.pi**2*N*k**2*T/(2*E_F*e); #Electronic heat capacity of Zn(J/mol/K)\nC_e = math.ceil(C_e*10**4)/10**4; #rounding off the value of C_e to 4 decimals\n\n#Result\nprint \"The molar electronic heat capacity of zinc is\",C_e, \"J/mol/K\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The molar electronic heat capacity of zinc is 0.2262 J/mol/K\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter17_1.ipynb b/Engineering_Physics_Aruldhas/Chapter17_1.ipynb new file mode 100644 index 00000000..f0df2691 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter17_1.ipynb @@ -0,0 +1,49 @@ +{ + "metadata": { + "name": "Chapter17" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "17: Ultrasonics" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 17.1, Page number 352" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the thickness of vibrating quartz\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nf = 3; #Fundamental vibrational frequency of quartz crystal(MHz)\nY = 7.9*10**10; #Young's modulus of quartz(N/m**2)\nrho = 2650; #Density of quartz(kg/m**3)\n\n#Calculation\nf = f*10**6; #Fundamental vibrational frequency of quartz crystal(Hz)\nl = 1/(2*f)*math.sqrt(Y/rho); #Thickness of vibrating quartz at resonance(m)\nl = l*10**3; #Thickness of vibrating quartz at resonance(mm)\nl = math.ceil(l*100)/100; #rounding off the value of l to 2 decimals\n\n#Result\nprint \"The thickness of vibrating quartz at resonance is\",l, \"mm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The thickness of vibrating quartz at resonance is 0.91 mm\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter18_1.ipynb b/Engineering_Physics_Aruldhas/Chapter18_1.ipynb new file mode 100644 index 00000000..aca20375 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter18_1.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter18" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "18: Acoustics of Buildings" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 18.1, Page number 361" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the output power of the sound source\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nr = 200; #Distance of the point of reduction from the source(m)\nI_0 = 10**-12; #Final intensity of sound(W/m**2)\nI_f = 60; #Intensity gain of sound at the point of reduction(dB)\n\n#Calculation\n#As A_I = 10*log10(I/I_0), solving for I\nI = I_0*10**(I_f/10); #Initial Intensity of sound(W/m**2)\nP = 4*math.pi*r**2*I; #Output power of the sound source(W)\nP = math.ceil(P*100)/100; #rounding off the value of P to 2 decimals\n\n#Result\nprint \"The output power of the sound source is\",P, \"W\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The output power of the sound source is 0.51 W\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 18.2, Page number 361" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the change in sound level\n\n#importing modules\nimport math\nfrom __future__ import division\nimport numpy as np\n\n#Variable declaration\nI1 = 1; #For simplicity assume first intensity level to be unity(W/m**2)\n\n#Calculation\nI2 = 2*I1; #Intensity level after doubling(W/m**2)\ndA_I = 10*np.log10(I2/I1); #Difference in gain level(dB)\n\n#Result\nprint \"The sound intensity level is increased by\",int(dA_I), \"dB\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The sound intensity level is increased by 3 dB\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 18.3, Page number 361" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the total absorption of sound in the hall\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nV = 8000; #Volume of the hall(m**3)\nT = 1.5; #Reverbration time of the hall(s)\n\n#Calculation\nalpha_s = 0.167*V/T; #Sabine Formula giving total absorption of sound in the hall(OWU)\nalpha_s = math.ceil(alpha_s*10)/10; #rounding off the value of alpha_s to 1 decimal\n\n#Result\nprint \"The total absorption of sound in the hall is\",alpha_s, \"OWU\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The total absorption of sound in the hall is 890.7 OWU\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 18.4, Page number 362" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the average absorption coefficient of the surfaces\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nV = 25*20*8; #Volume of the hall(m**3)\nT = 4; #Reverbration time of the hall(s)\n\n#Calculation\nS = 2*(25*20+25*8+20*8); #Total surface area of the hall(m**2)\nalpha = 0.167*V/(T*S); #Sabine Formule giving total absorption in the hall(OWU)\nalpha = math.ceil(alpha*10**4)/10**4; #rounding off the value of alpha to 4 decimals\n\n#Result\nprint \"The average absorption coefficient of the surfaces is\",alpha, \"OWU/m**2\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The average absorption coefficient of the surfaces is 0.0971 OWU/m**2\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 18.5, Page number 362" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the reverbration time for the hall\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nV = 475; #Volume of the hall(m**3)\nA_f = 100; #Area of the floor(m**2)\nA_c = 100; #Area of the ceiling(m**2)\nA_w = 200; #Area of the wall(m**2)\nalpha_w = 0.025; #Absorption coefficients of the wall(OWU/m**2)\nalpha_c = 0.02; #Absorption coefficients of the ceiling(OWU/m**2)\nalpha_f = 0.55; #Absorption coefficients of the floor(OWU/m**2)\n\n#Calculation\nalpha_s = (A_w*alpha_w)+(A_c*alpha_c)+(A_f*alpha_f); \nT = 0.167*V/alpha_s; #Sabine Formula for reverbration time(s)\nT = math.ceil(T*100)/100; #rounding off the value of T to 2 decimals\n\n#Result\nprint \"The reverbration time for the hall is\",T, \"s\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The reverbration time for the hall is 1.28 s\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 18.6, Page number 362" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the reverbration time for the hall\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nI0 = 1; #For simplicity assume initial sound intensity to be unity(W/m**2)\nA_I1 = 80; #First intensity gain of sound(dB)\nA_I2 = 70; #Second intensity gain of sound(dB)\n\n#Calculation\n#As A_I = 10*log10(I/I_0), solving for I1 and I2\nI1 = 10**(A_I1/10)*I0; #First intensity of sound(W/m**2)\nI2 = 10**(A_I2/10)*I0; #Second intensity of sound(W/m**2)\nI = I1 + I2; #Resultant intensity level of sound(W/m**2)\nA_I = 10*np.log10(I/I0); #Intensity gain of resultant sound(dB)\nA_I = math.ceil(A_I*10**3)/10**3; #rounding off the value of A_I to 3 decimals\n\n#Result\nprint \"The intensity gain of resultant sound is\",A_I, \"dB\"\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The intensity gain of resultant sound is 80.414 dB\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter1_1.ipynb b/Engineering_Physics_Aruldhas/Chapter1_1.ipynb new file mode 100644 index 00000000..c487a981 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter1_1.ipynb @@ -0,0 +1,217 @@ +{ + "metadata": { + "name": "Chapter1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "1: Oscillations and Waves" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.1, Page number 23" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the period of motion\n\n#import modules\nimport math\n\n#Variable declaration\nS=4; #SHM described by a particle(cm)\nx=0; #mean position\nv=12; #velocity at mean position(cm/s)\n\n#Calculation\nA=S/2; #amplitude of motion(cm)\nomega=v/A; #angular frequency(sec-1)\nT=(2*math.pi)/omega; #time period(sec)\nT=math.ceil(T*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint \"time period of motion is\",T, \"sec\"\nprint \"time period of motion is pi/3 sec\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "time period of motion is 1.048 sec\ntime period of motion is pi/3 sec\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.2, Page number 23" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the acceleration and maximum velocity\n\n#import modules\nimport math\n\n#Variable declaration\nT=0.1; #time period(sec)\nA=4; #amplitude of motion(cm)\nx=0.2; #distance from mean position(cm)\n\n#Calculation\nomega=(2*math.pi)/T; #angular frequency(sec-1)\na=(omega**2)*x; #acceleration(cm/sec^2)\na=math.ceil(a*10**2)/10**2; #rounding off to 2 decimals\n#maximum velocity is when particle is in the mean position\nv_max=omega*A; #maximum velocity(cm/sec)\nv_max=math.ceil(v_max*10**2)/10**2; #rounding off to 2 decimals\n\n#Result\nprint \"acceleration is\",a, \"cm/sec^2\"\nprint \"maximum velocity is\",v_max, \"cm/sec\"\n\n#answers given in the book are wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "acceleration is 789.57 cm/sec^2\nmaximum velocity is 251.33 cm/sec\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.3, Page number 24" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the damping constant\n\n#import modules\nimport math\nimport numpy as np\n\n#Variable declaration\nA1 = 40; #First amplitude of oscillation(cm)\nAn_plus_1 = 4; #Amplitude after 100 oscillations(cm)\nn = 100; #Number of oscillations\nT = 2.5; #Time period of oscillations(s)\n\n#Calculation\nt = T/4; #Time taken to reach the first amplitude from the mean position(s)\n#Now A1 = x0*math.exp(-lambda*t) and An_plus_1 = x0*math.exp(-lambda*(t+nT))\n#A1/An_plus_1 = math.exp(n*lambda*T)\nx=A1/An_plus_1;\nlamda=np.log(x)/(n*T); #Damping constant(per sec)\nlamda=lamda*10**2;\nlamda=math.ceil(lamda*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint \"Damping constant is\",lamda,\"*10**-2 per sec\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Damping constant is 0.922 *10**-2 per sec\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.4, Page number 24" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the amplitude and period of oscillation\n\n#import modules\nimport math\n\n#Variable declaration\nx1 = 3; #First position of the particle(cm)\nx2 = 4; #Second position of the particle(cm)\nv1 = 16; #Velocity of particle executing SHM at 1st position(cm/s)\nv2 = 12; #Velocity of particle executing SHM at 2nd position (cm/s)\n\n#Calculation\n#As v = omega*sqrt(A**2 - x**2) so\n#(v1/v2)**2=(A**2 - x1**2)/(A**2 - x2**2)\n#RHS gives (A**2-9)/(A**2-16)\n#(v2**2)*(A**2 - x1**2)=(v1**2)*(A**2 - x2**2), on solving we get\nA=math.sqrt((((v1**2)*(x2**2))-((v2**2)*(x1**2)))/((v1**2)-(v2**2))); #amplitude in cm\nomega=v1/math.sqrt(A**2-x1**2); #Angular speed of the particle(per sec)\nT=2*math.pi/omega; #Time period of oscillation(sec)\nT=math.ceil(T*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint \"The amplitude of SHM is\",A, \"cm\"\nprint \"The time period of oscillation is\",T, \"sec\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The amplitude of SHM is 5.0 cm\nThe time period of oscillation is 1.571 sec\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.5, Page number 25" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the spring constant, amplitude and maximum velocity\n\n#import modules\nimport math\n\n#Variable declaration\nm = 0.3; #Mass attached to the string(kg)\ng = 9.8; #Acceleration due to gravity(m/sec**2)\nx = 0.15; #Stretchness produced in the spring(m)\ns = 0.1; #spring is stretched and released(m)\n\n#Calculation\nF = m*g; #Restoring force acting on the mass(N)\nk = F/x; #Spring constant(N/m)\nA = s; #amplitude equals to the spring stretched and released\nomega = math.sqrt(k/m); #Angular frequency of oscillation(rad per sec)\nv0 = omega*A; #Maximum velocity during the oscillations(m/s)\nv0=math.ceil(v0*100)/100; #rounding off to 2 decimals\n\n#Result\nprint \"The spring constant is\",k, \"N/m\"\nprint \"The amplitude of oscillation is\",A, \"m\"\nprint \"The maximum velocity during oscillations is\",v0, \"m/s\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The spring constant is 19.6 N/m\nThe amplitude of oscillation is 0.1 m\nThe maximum velocity during oscillations is 0.81 m/s\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.6, Page number 25" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the frequency equivalent of visible region\n\n#import modules\nimport math\n\n#Variable declaration\nlambda1 = 400; #Lower limit of wavelength of visible region(nm)\nlambda2 = 700; #Upper limit of wavelength of visible region(nm)\nc = 3*10**8; #Speed of light in vacuum(m/s)\n\n#Calculation\nlambda1 = lambda1*10**-9 #Lower limit of wavelength(m) \nlambda2 = lambda2*10**-9 #upper limit of wavelength(m) \nnew_1 = c/lambda1; #Upper limit of frequency of visible region(m)\nnew_2 = c/lambda2; #Lower limit of frequency of visible region(m)\n\n#Result\nprint \"The frequency equivalent of 400 nm is\",new_1, \"Hz\"\nprint \"The frequency equivalent of 700 nm is\",new_2, \"Hz\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The frequency equivalent of 400 nm is 7.5e+14 Hz\nThe frequency equivalent of 700 nm is 4.28571428571e+14 Hz\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.7, Page number 26" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the amplitude, wavelength, frequency and velocity of the wave\n\n#import modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\n#Comparing the standard equation u(x,t) = A*sin(2*%pi(x/lambda-t/T)) with the given equation, we get\nA = 1.5*10**-3; #Amplitude of the sound wave(m)\nlamda = 8; #Wavelength of the sound wave(m)\nT = 1/40; #Time period of the sound wave(s)\n\n#Calculation\nA = A*10**3;\nnew = 1/T; #Frequency of the sound wave(Hz)\nv = new*lamda; #Velocity of the sound wave(m/s)\nT=math.ceil(T*100)/100; #rounding off to 2 decimals\n\n#Result\nprint \"The amplitude of the sound wave is\",A,\"*10**-3 m\"\nprint \"The wavelength of the sound wave is\",lamda, \"m\"\nprint \"The time period of the sound wave is\",T, \"s\"\nprint \"The frequency of the sound wave is\",new, \"Hz\"\nprint \"The velocity of the sound wave is\",v, \"m/s\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The amplitude of the sound wave is 1.5 *10**-3 m\nThe wavelength of the sound wave is 8 m\nThe time period of the sound wave is 0.03 s\nThe frequency of the sound wave is 40.0 Hz\nThe velocity of the sound wave is 320.0 m/s\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.8, Page number 26" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To find the equation of a wave\n\n#import modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nA = 2; #Amplitude of the wave(cm)\nT = 0.5; #Time period of the wave(sec)\nv = 200; #Wave velocity(cm/s)\n\n#Calculation\nf = 1/T; #Frequency of the wave(Hz)\nlamda = v/f; #Wavelength of the wave(cm)\n\n#Result\nprint \"frequency of wave is\",f, \"Hz\"\nprint \"wavelength of wave is\",lamda, \"cm\"\nprint \"The Equation of the wave moving along X-axis :\"\nprint \"u = \",A,\"*sin*2*math.pi*(x/\",lamda,\"- t/\",T,\")\" #x and y are in cm and t is in sec", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "frequency of wave is 2.0 Hz\nwavelength of wave is 100.0 cm\nThe Equation of the wave moving along X-axis :\nu = 2 *sin*2*math.pi*(x/ 100.0 - t/ 0.5 )\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.9, Page number 27" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To find the velocity and frequency of the wave\n\n#import modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nT = 1000; #Tension in the wire(N)\nM=15; #mass of the wire(kg)\nl=300; #length of the wire(m)\nlamda = 0.30; #Wavelength of wave along wire(m)\n\n#Calculation\nm = M/l; #Mass per unit length of the wire(kg/m)\nv = math.sqrt(T/m); #Velocity of wave through wire(m/s)\nv=math.ceil(v*100)/100; #rounding off to 2 decimals\nnew = v/lamda; #Frequency of wave through string(Hz)\nnew=math.ceil(new*100)/100; #rounding off to 2 decimals\n\n#Result\nprint \"The velocity of the wave through wire is\",v, \"m/s\"\nprint \"The frequency of the wave through wire is\",new, \"Hz\"\n\n#answer for frequency of the wave is wrong in the textbook", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The velocity of the wave through wire is 141.43 m/s\nThe frequency of the wave through wire is 471.44 Hz\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter2_1.ipynb b/Engineering_Physics_Aruldhas/Chapter2_1.ipynb new file mode 100644 index 00000000..f002fd31 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter2_1.ipynb @@ -0,0 +1,118 @@ +{ + "metadata": { + "name": "Chapter2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "2: Electromagnetic Theory" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.1, Page number 46" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To determine the peak value of displacement current\n\n#importing modules\nfrom __future__ import division\nfrom sympy import *\nimport math\n\n#Variable declaration\nC = 10; #Capacitance of the capacitor(pF)\n#given V=0.2*sin(120*math.pi*t) in volts\n\n#Calculation\nC=C*10**-12; #Capacitance of the capacitor(F)\nx, y, z, t = symbols('x y z t')\nk, m, n = symbols('k m n', integer=True)\nf, g, h = symbols('f g h', cls=Function)\n#I = C*dV/dt\n#let dV/dt be a\na=diff(0.2*sin(120*math.pi*t),t) #dV/dt\n#value of dV/dt is 75.398223686155*cos(376.991118430775*t)\n#for cosine function peak value occurs when 120*math.pi*t = 0\n#therefore value of dV/dt becomes d = 75.398223686155\nd = 75.398223686155; #value of dV/dt \nI=C*d; #displacement current(A)\n\n#Result\nprint \"value of dV/dt is\",a\nprint \"displacement current is\",I, \"A\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "value of dV/dt is 75.398223686155*cos(376.991118430775*t)\ndisplacement current is 7.53982236862e-10 A\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.2, Page number 46" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the peak value of displacement current density in a good conductor\n\n#importing modules\nfrom __future__ import division\nfrom sympy import *\nimport math\n\n#Variable declaration\nepsilon_r = 1; #Relative electrical permittivity of free space\nepsilon_0 = 8.854*10**-12; #Absolute electrical permittivity of free space(F/m)\n#given E=sin(120*math.pi*t) in volts\n\n#Calculation\nx, y, z, t = symbols('x y z t')\nk, m, n = symbols('k m n', integer=True)\nf, g, h = symbols('f g h', cls=Function)\n#J2 = epsilon*dE/dt\nepsilon=epsilon_0*epsilon_r;\n#let dE/dt be a\na=diff(sin(120*math.pi*t),t) #dE/dt\n#value of dE/dt is 376.991118430775*cos(376.991118430775*t)\n#for cosine function peak value occurs when 120*math.pi*t = 0\n#therefore value of dE/dt becomes d = 376.991118430775\nd = 376.991118430775; #value of dE/dt\nJ2=epsilon*d; #displacement current density(A/m**2)\n\n#Result\nprint \"value of dE/dt is\",a\nprint \"The peak value of displacement current density is\",J2, \"A/m**2\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "value of dE/dt is 376.991118430775*cos(376.991118430775*t)\nThe peak value of displacement current density is 3.33787936259e-09 A/m**2\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.3, Page number 47 (Theoritical proof)" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.4, Page number 47" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To determine the peak value of displacement current\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\np = 60; #Power rating of bulb(W)\nd = 0.5; #Distance from the bulb(m)\n\n#Calculation\nA=4*math.pi*d**2; #area(m**2)\nP = p/A; #Value of Poynting vector(W/m**2)\nP = math.ceil(P*100)/100; #rounding off value of P to 1 decimal\n\n#Result\nprint \"The value of Poynting vector is\",P, \"W/m**2\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The value of Poynting vector is 19.1 W/m**2\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.5, Page number 47" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To determine the velocity of wave, intrinsic impedence and peak value of magnetic intensity\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nE_peak = 6; #Peak value of electric field intensity(V/m)\nc = 3*10**8; #Speed of electromagnetic wave in free space(m/s)\nmew_0 = 4*math.pi*10**-7; #Absolute permeability of free space(Tm/A)\nepsilon_0 = 8.854*10**-12; #Absolute permittivity of free space(F/m)\nmew_r = 1; #Relative permeability of medium\nepsilon_r = 3; #Relative permittivity of the medium\n\n#Calculation\nv = c/math.sqrt(mew_r*epsilon_r); #Wave velocity(m/s)\nv = v/10**8;\nv = math.ceil(v*10**4)/10**4; #rounding off the value of v to 4 decimals\neta = math.sqrt((mew_0/epsilon_0)*(mew_r/epsilon_r)); #Intrinsic impedance of the medium(ohm)\neta = math.ceil(eta*10)/10; #rounding off the value of v to 1 decimal\nH_P = E_peak/eta; #Peak value of the magnetic intensity(A/m)\nH_P = H_P*10**2;\nH_P = math.ceil(H_P*10**2)/10**2; #rounding off the value of v to 2 decimals\n\n#Result\nprint \"The wave velocity is\",v,\"*10**8 m/s\"\nprint \"The intrinsic impedance of the medium is\",eta, \"ohm\"\nprint \"The peak value of the magnetic intensity is\",H_P,\"*10**-2 A/m\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The wave velocity is 1.7321 *10**8 m/s\nThe intrinsic impedance of the medium is 217.6 ohm\nThe peak value of the magnetic intensity is 2.76 *10**-2 A/m\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter3_1.ipynb b/Engineering_Physics_Aruldhas/Chapter3_1.ipynb new file mode 100644 index 00000000..e20ce99f --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter3_1.ipynb @@ -0,0 +1,238 @@ +{ + "metadata": { + "name": "Chapter3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "3: Interference" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.1, Page number 71" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the velocity of light\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nbeta = 0.51; #Fringe width(mm)\nd = 2.2; #Distance between the slits(mm)\nD = 2; #Distance between the slits and the screen(m)\n\n#Calculation\nbeta = beta*10**-1; #Fringe width(cm)\nd = d*10**-1; #Distance between the slits(cm)\nD=D*10**2; #Distance between the slits and the screen(cm)\nlamda = beta*d/D; #Wavelength of light(cm)\nlamda = lamda*10**8; #Wavelength of light(A)\n\n#Result\nprint \"The wavelength of light is\",lamda, \"angstrom\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The wavelength of light is 5610.0 angstrom\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.2, Page number 71" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the seperation between the third fringe\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nlambda1 = 4250; #First wavelength emitted by source of light(A)\nlambda2 = 5050; #Second wavelength emitted by source of light(A)\nD = 1.5; #Distance between the source and the screen(m)\nd = 0.025; #Distance between the slits(mm)\nn = 3; #Number of fringe from the centre\n\n#Calculation\nlambda1 = lambda1*10**-10; #First wavelength emitted(m)\nlambda2 = lambda2*10**-10; #Second wavelength emitted(m)\nd = d*10**-3; #Distance between the slits(m)\nx3 = n*lambda1*D/d; #Position of third bright fringe due to lambda1(m)\nx3_prime = n*lambda2*D/d; #Position of third bright fringe due to lambda2(m)\nx = x3_prime-x3; #separation between the third bright fringe(m)\nx = x*10**2; #separation between the third bright fringe(cm)\n\n#Result\nprint \"The separation between the third bright fringe due to the two wavelengths is\",x, \"cm\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The separation between the third bright fringe due to the two wavelengths is 1.44 cm\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.3, Page number 71" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the refractive index of the sheet of glass\n\n#importing modules\nimport math\n\n#Variable declaration\nlamda = 5.5*10**-5; #Wavelength emitted by source of light(cm)\nn = 4; #Number of fringes shifted\nt = 3.9*10**-4; #Thickness of the thin glass sheet(cm)\n\n#Calculation\nmew = (n*lamda/t)+1; #Refractive index of the sheet of glass\nmew = math.ceil(mew*10**4)/10**4; #rounding off the value of v to 4 decimals\n\n#Result\nprint \"The refractive index of the sheet of glass is\",mew", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The refractive index of the sheet of glass is 1.5642\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.4, Page number 72" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the least thickness of the film that appears bright and dark\n\n#importing modules\nimport math\n\n#Variable declaration\nlamda = 5893; #Wavelength of monochromatic lihgt used(A)\nn = 1; #Number of fringe for the least thickness of the film\ncosr = 1; #for normal incidence\nmew = 1.42; #refractive index of the soap film\n\n#Calculation\n#As for constructive interference, \n#2*mew*t*cos(r) = (2*n-1)*lambda/2, solving for t\nt = (2*n-1)*lamda/(4*mew*cosr); #Thickness of the film that appears bright(A)\n#As for destructive interference, \n#2*mu*t*cos(r) = n*lambda, solving for t\nt1 = n*lamda/(2*mew*cosr); #Thickness of the film that appears bright(A)\n\n#Result\nprint \"The thickness of the film that appears bright is\",t, \"angstrom\"\nprint \"The thickness of the film that appears dark is\",t1, \"angstrom\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The thickness of the film that appears bright is 1037.5 angstrom\nThe thickness of the film that appears dark is 2075.0 angstrom\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.5, Page number 72" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the angle of the wedge\n\n#importing modules\nimport math\n\n#Variable declaration\nlamda = 5893; #Wavelength of monochromatic lihgt used(A)\nn = 10; #Number of fringe that are found \nd = 1; #Distance of 10 fringes(cm)\n\n#Calculation\nbeta = d/n; #Fringe width(cm)\nlamda = lamda*10**-8; #Wavelength of monochromatic lihgt used(cm)\ntheta = lamda/(2*beta); #Angle of the wedge(rad)\ntheta = theta*10**4;\ntheta = math.ceil(theta*10**4)/10**4; #rounding off the value of theta to 4 decimals\n\n#Result\nprint \"The angle of the wedge is\",theta,\"*10**-4 rad\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The angle of the wedge is 2.9465 *10**-4 rad\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.6, Page number 72" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the separation between consecutive bright fringes\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nlamda = 5900; #Wavelength of monochromatic lihgt used(A)\nt = 0.010; #Spacer thickness(mm)\nl = 10; #Wedge length(cm)\n\n#Calculation\nt = t*10**-1; #Spacer thickness(cm)\ntheta = t/l; #Angle of the wedge(rad)\nlamda = lamda*10**-8; #Wavelength of monochromatic lihgt used(cm)\nbeta = lamda/(2*theta); #Fringe width(cm)\n\n#Result\nprint \"The separation between consecutive bright fringes is\",beta, \"cm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The separation between consecutive bright fringes is 0.295 cm\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.7, Page number 72" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the diameter of 20th dark ring\n\n#importing modules\nimport math\n\n#Variable declaration\nD4 = 0.4; #Diameter of 4th dark ring(cm)\nD12 = 0.7; #Diameter of 12th dark ring(cm)\n\n#Calculation\n#We have (dn_plus_k**2)-Dn**2 = 4*k*R*lamda\n#D12**2-D4**2 = 32*R*lamda and D20**2-D12**2 = 32*R*lamda for k = 8\n#since RHS are equal, by equating the LHS we get D12**2-D4**2 = D20**2-D12**2\nD20 = math.sqrt((2*D12**2)-D4**2); #Diameter of 20th dark ring(cm)\nD20 = math.ceil(D20*10**4)/10**4; #rounding off the value of D20 to 4 decimals\n\n#Result\nprint \"The diameter of 20th dark ring is\",D20, \"cm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The diameter of 20th dark ring is 0.9056 cm\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.8, Page number 73" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the refractive index of the liquid\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nDn = 0.30; #Diameter of nth dark ring with air film(cm)\ndn = 0.25; #Diameter of nth dark ring with liquid film(cm)\n\n#Calculation\nmew = (Dn/dn)**2; #Refractive index of the liquid\n\n#Result\nprint \"The refractive index of the liquid is\", mew\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The refractive index of the liquid is 1.44\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.9, Page number 73" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the wavelength of light\n\n#importing modules\nimport math\n\n#Variable declaration\nx = 0.002945; #Distance through which movable mirror is shifted(cm)\nN = 100; #Number of fringes shifted\n\n#Calculation\nx = x*10**-2; #Distance through which movable mirror is shifted(m)\nlamda = 2*x/N; #Wavelength of light(m)\nlamda = lamda*10**10; #Wavelength of light(A)\n\n#Result\nprint \"The wavelength of light is\",lamda, \"angstrom\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The wavelength of light is 5890.0 angstrom\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.10, Page number 73" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the shift in movable mirror\n\n#importing modules\nimport math\n\n#Variable declaration\nlambda1 = 5896; #Wavelength of D1 line of sodium(A)\nlambda2 = 5890; #Wavelength of D2 line of sodium(A)\n\n#Calculation\nlamda = (lambda1+lambda2)/2;\nx = (lamda**2)/(2*(lambda1-lambda2)); #Shift in movable mirror of Michelson Interferometer(A)\nx = x*10**-7; #Shift in movable mirror of Michelson Interferometer(mm)\nx = math.ceil(x*10**4)/10**4; #rounding off the value of D20 to 4 decimals\n\n#Result\nprint \"The shift in movable mirror is\",x, \"mm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The shift in movable mirror is 0.2894 mm\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter4_1.ipynb b/Engineering_Physics_Aruldhas/Chapter4_1.ipynb new file mode 100644 index 00000000..4cd8a196 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter4_1.ipynb @@ -0,0 +1,217 @@ +{ + "metadata": { + "name": "Chapter4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "4: Diffraction" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.1, Page number 91" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the velocity of light\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nD = 50; #Distance between source and the screen(cm)\nlamda = 6563; #Wavelength of light of parallel rays(A)\nd = 0.385; #Width of the slit(mm)\nn1 = 1; #Order of diffraction for first minimum\nn2 = 5; #Order of diffraction for fifth minimum\n\n#Calculation\nlamda = lamda*10**-8; #Wavelength of light of parallel rays(cm)\nd = d*10**-1; #Width of the slit(cm)\n#As sin(theta1) = n*lambda/d = x1/D, solving for x1\nx1 = n1*lamda*D/d; #Distance from the centre of the principal maximum to the first minimum(cm)\nx1 = x1*10; #Distance from the centre of the principal maximum to the first minimum(mm)\nx1 = math.ceil(x1*10**3)/10**3; #rounding off the value of x1 to 3 decimals\nx2 = n2*lamda*D/d; #Distance from the centre of the principal maximum to the fifth minimum(cm)\nx2 = x2*10; #Distance from the centre of the principal maximum to the fifth minimum(mm)\nx2 = math.ceil(x2*10**3)/10**3; #rounding off the value of x2 to 3 decimals\n\n#Result\nprint \"The Distance from the centre of the principal maximum to the first minimum is\",x1, \"mm\"\nprint \"The Distance from the centre of the principal maximum to the fifth minimum is\",x2, \"mm\"\n\n#answer for x2 given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Distance from the centre of the principal maximum to the first minimum is 0.853 mm\nThe Distance from the centre of the principal maximum to the fifth minimum is 4.262 mm\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.2, Page number 91" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the radii of first two dark rings\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nD = 0.04; #Diameter of circular aperture(cm)\nf = 20; #Focal length of convex lens(cm)\nlamda = 6000; #Wavelength of light used(A)\n\n#Calculation\nlamda = lamda*10**-8; #Wavelength of light used(cm)\n#We have sin(theta) = 1.22*lambda/D = theta, for small theta\n#For first dark ring\ntheta = 1.22*lamda/D; #The half angular width at central maximum(rad)\nr1 = theta*f; #The half width of central maximum for first dark ring(cm)\nr1 = r1*10**2;\n#We have sin(theta) = 5.136*lambda/(%pi*D) = theta, for small theta\n#For second dark ring\ntheta = 5.136*lamda/(math.pi*D); #The half angular width at central maximum(rad)\nr2 = theta*f; #The half width of central maximum for second dark ring(cm)\nr2 = r2*10**2;\nr2 = math.ceil(r2*100)/100; #rounding off the value of r2 to 2 decimals\n\n#Result\nprint \"The radius of first dark ring is\",r1,\"*10**-2 cm\"\nprint \"The radius of second dark ring is\",r2,\"*10**-2 cm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The radius of first dark ring is 3.66 *10**-2 cm\nThe radius of second dark ring is 4.91 *10**-2 cm\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.3, Page number 92" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the angle at which the light produces a second order maximum\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nn = 2; #Order of diffraction\nlamda = 650; #Wavelength of light used(nm)\nd = 1.2*10**-3; #Distance between two consecutive slits of grating(cm)\n\n#Calculation\n#We have sin(theta) = n*N*lambda = n*lambda/d, solving for theta\nlamda = lamda*10**-9; #Wavelength of light used(m)\nd = d*10**-2; #Distance between two consecutive slits of grating(m)\na=n*lamda/d;\ntheta = math.asin(a); #Angle at which the 650 nm light produces a second order maximum(rad)\ntheta = theta*57.2957795; #angle in degrees\ntheta = math.ceil(theta*10**2)/10**2; #rounding off the value of theta to 2 decimals\n\n#Result\nprint \"The angle at which the light produces a second order maximum is\",theta, \"degrees\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The angle at which the light produces a second order maximum is 6.22 degrees\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.4, Page number 92" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the highest order of spectra\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nlamda = 650; #Wavelength of light used(nm)\nN = 6000; #Number of lines per cm on grating\ntheta = 90; #Angle at which the highest spectral order is obtained(degrees)\n\n#Calculation\ntheta = theta*0.0174532925; #Angle at which the highest spectral order is obtained(rad)\n#We have sin(theta) = n*N*lambda, solving for n\nlamda = lamda*10**-9; #Wavelength of light used(m)\nN = N*10**2; #Number of lines per m on grating\nn = math.sin(theta)/(N*lamda); #The highest order of spectra with diffraction grating\nn = math.ceil(n*10**3)/10**3; #rounding off the value of theta to 3 decimals\ni,d = divmod(n, 1); #divides the value of n into integer and decimal parts where i is integer\n\n#Result\nprint \"value of n is\",n\nprint \"The highest order of spectra obtained with diffraction grating is\",i\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "value of n is 2.565\nThe highest order of spectra obtained with diffraction grating is 2.0\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.5, Page number 92" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To show that the blue line and red line overlap\n\n#importing modules\nimport math\n\n#Variable declaration\nN = 4000; #Number of lines per cm on grating\n#For Blue Line\nlamda1 = 450; #Wavelength of blue light(nm)\nn1 = 3; #Order of diffraction spectrum\n#For Red Line\nlamda2 = 700; #Wavelength of red light(nm)\nn2 = 2; #Order of diffraction spectrum\n\n#Calculation\nN = N*10**2; #Number of lines per m on grating\nlamda1 = lamda1*10**-9; #Wavelength of blue light(m)\nlamda2 = lamda2*10**-9; #Wavelength of red light(m)\n#We have sin(theta) = n*N*lambda, solving for sin(theta)\nsin_theta_3 = n1*N*lamda1; #Sine of angle at third order diffraction \nsin_theta_2 = n2*N*lamda2; #Sine of angle at second order diffraction\n\n#Result\nprint \"Sine of angle at third order diffraction is\",sin_theta_3\nprint \"Sine of angle at second order diffraction is\",sin_theta_2 \n#Check for overlapping\nif (sin_theta_2-sin_theta_3)<0.05:\n print \"The two orders overlap\"\nelse:\n print \"The two orders do not overlap\" ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Sine of angle at third order diffraction is 0.54\nSine of angle at second order diffraction is 0.56\nThe two orders overlap\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.6, Page number 93" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the width of first order spectrum on the screen\n\n#importing modules\nimport math\n\n#Variable declaration\nn = 1; #Order of diffraction spectrum\nN = 6000; #Number of lines per cm on diffraction grating\nD = 2; #Distance of screen from the source(m)\nlamda1 = 400; #Wavelength of blue light(nm)\nlamda2 = 750; #Wavelength of blue light(nm)\n\n#Calculation\nN = N*10**2; #Number of lines per m on grating\nlamda1 = lamda1*10**-9; #Wavelength of blue light(m)\nlamda2 = lamda2*10**-9; #Wavelength of blue light(m)\n#We have sin(theta1) = n*N*lamda1, solving for theta1\ntheta1 = math.asin(n*N*lamda1); #Angle at first order diffraction for Blue light(rad)\ntheta1_d = theta1*57.2957795; #Angle at first order diffraction for Blue light(degrees)\ntheta2 = math.asin(n*N*lamda2); #Angle at first order diffraction for Red light(rad)\ntheta2_d = theta2*57.2957795; #Angle at first order diffraction for Red light(degrees)\nx1 = D*math.tan(theta1); #Half width position at central maximum for blue color(m)\nx2 = D*math.tan(theta2); #Half width position at central maximum for red color(m)\nx = x2-x1; #width of first order spectrum on the screen(m)\nx = x*10**2; #width of first order spectrum on the screen(cm)\nx = math.ceil(x*10**2)/10**2; #rounding off the value of x to 2 decimals\n\n#Result\nprint \"The width of first order spectrum on the screen is\",x, \"cm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The width of first order spectrum on the screen is 51.34 cm\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.7, Page number 93" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the least separation between wavelengths\n\n#importing modules\nimport math\n\n#Variable declaration\nw = 5; #Width of the grating(cm)\nN = 32; #Number of lines per mm on grating\nlamda = 640; #Wavelength of light(nm)\nn = 2; #Order of diffraction\n\n#Calculation\nN= N*10; #Number of lines per cm on grating\nN0 = w*N; #Total number of lines on the grating\nd_lambda = lamda/(n*N0); #Separation between wavelengths(nm)\n\n#Result\nprint \"The separation between wavelengths which the grating can just resolve is\",d_lambda, \"nm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The separation between wavelengths which the grating can just resolve is 0.2 nm\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.8, Page number 93" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the separation of the centres of the images in the focal plane\n\n#importing modules\nimport math\n\n#Variable declaration\nlamda = 550; #Wavelength of light(nm)\nD = 3.2; #Diameter of circular lens(cm)\nf = 24; #Focal length of the lens(cm) \n\n#Calculation\nlamda = lamda*10**-9; #Wavelength of light(m)\nD = D*10**-2; #Diameter of circular lens(m)\ntheta_min = 1.22*lamda/D; #Minimum angle of resolution provided by the lens(rad)\n#As delta_x/f = theta_min, solving for delta_x\nf = f*10**-2; #Focal length of the lens(m) \ndelta_x = theta_min*f; #Separation of the centres of the images in the focal plane of lens(m)\ndelta_x = delta_x*10**6; #Separation of the centres of the images in the focal plane of lens(micro m)\n \n#Result\nprint \"The separation of the centres of the images in the focal plane is\",round(delta_x), \"micro-metre\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The separation of the centres of the images in the focal plane is 5.0 micro-metre\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.9, Page number 94" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the linear separation between two points\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nlamda = 550; #Wavelength of light(nm)\nD = 20; #Diameter of objective of telescope(cm)\nd = 6; #Distance of two points from the objective of telescope(km)\n\n#Calculation\nlamda = lamda*10**-9; #Wavelength of light(m)\nD = D*10**-2; #Diameter of objective of telescope(m)\nd = d*10**3; #Distance of two points from the objective of telescope(m)\ntheta = 1.22*lamda/D; #Angular separation between two points(rad)\nx = theta*d; #Linear separation between two points(m)\nx = x*10**3; #Linear separation between two points(mm)\n\n#Result\nprint \"The linear separation between two points is\",x, \"mm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The linear separation between two points is 20.13 mm\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter5_1.ipynb b/Engineering_Physics_Aruldhas/Chapter5_1.ipynb new file mode 100644 index 00000000..3731e0ad --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter5_1.ipynb @@ -0,0 +1,155 @@ +{ + "metadata": { + "name": "Chapter5" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "5: Polarization" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 5.1, Page number 113" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the angle of incidence for complete polarization\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nmew_g = 1.72; #Refractive index of glass\nmew_w = 4/3; #Refractive index of water\n\n#Calculation\n#For polarization to occur on flint glass, tan(i) = mew_g/mew_w\n#Solving for i\ni_g = math.atan(mew_g/mew_w); #angle of incidence for complete polarization for flint glass(rad)\na = 180/math.pi; #conversion factor from radians to degrees\ni_g = i_g*a; #angle of incidence(degrees)\ni_g = math.ceil(i_g*10**2)/10**2; #rounding off the value of i_g to 2 decimals\n#For polarization to occur on water, tan(i) = mew_w/mew_g\n#Solving for i\ni_w = math.atan(mew_w/mew_g); #angle of incidence for complete polarization for water(rad)\ni_w = i_w*a; #angle of incidence(degrees)\ni_w = math.ceil(i_w*10**3)/10**3; #rounding off the value of i_w to 3 decimals\n\n#Result\nprint \"The angle of incidence for complete polarization to occur on flint glass is\",i_g, \"degrees\"\nprint \"The angle of incidence for complete polarization to occur on water is\",i_w, \"degrees\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The angle of incidence for complete polarization to occur on flint glass is 52.22 degrees\nThe angle of incidence for complete polarization to occur on water is 37.783 degrees\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 5.2, Page number 113" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the percentage transmission of incident light\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nI0 = 1; #For simplicity, we assume the intensity of light falling on the second Nicol prism to be unity(W/m**2)\ntheta = 30; #Angle through which the crossed Nicol is rotated(degrees)\n\n#Calculation\ntheeta = 90-theta; #angle between the planes of transmission after rotating through 30 degrees\na = math.pi/180; #conversion factor from degrees to radians\ntheeta = theeta*a; ##angle between the planes of transmission(rad)\nI = I0*math.cos(theeta)**2; #Intensity of the emerging light from second Nicol(W/m**2)\nT = (I/(2*I0))*100; #Percentage transmission of incident light\nT = math.ceil(T*100)/100; #rounding off the value of T to 2 decimals\n\n#Result\nprint \"The percentage transmission of incident light after emerging through the Nicol prism is\",T, \"%\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The percentage transmission of incident light after emerging through the Nicol prism is 12.51 %\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 5.3, Page number 113" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the thickness of Quarter Wave plate\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nlamda = 6000; #Wavelength of incident light(A)\nmew_e = 1.55; #Refractive index of extraordinary ray\nmew_o = 1.54; #Refractive index of ordinary ray\n\n#Calculation\nlamda = lamda*10**-8; #Wavelength of incident light(cm)\nt = lamda/(4*(mew_e-mew_o)); #Thickness of Quarter Wave plate of positive crystal(cm)\n\n#Result\nprint \"The thickness of Quarter Wave plate is\",t, \"cm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The thickness of Quarter Wave plate is 0.0015 cm\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 5.4, Page number 114" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To show the behaviour of the plate\n\n#Calculation\n#the thickness of a half wave plate of calcite for wavelength lamda is\n#t = lamda/(2*(mew_e - mew_o)) = (2*lamda)/(4*(mew_e - mew_o))\n\n#Result\nprint \"The half wave plate for lamda will behave as a quarter wave plate for 2*lamda for negligible variation of refractive index with wavelength\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The half wave plate for lamda will behave as a quarter wave plate for 2*lamda for negligible variation of refractive index with wavelength\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 5.5, Page number 114" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the phase retardation\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nlamda = 500; #Wavelength of incident light(nm)\nmew_e = 1.5508; #Refractive index of extraordinary ray\nmew_o = 1.5418; #Refractive index of ordinary ray\nt = 0.032; #Thickness of quartz plate(mm)\n\n#Calculation\nlamda = lamda*10**-9; #Wavelength of incident light(m)\nt = t*10**-3; #Thickness of quartz plate(m)\ndx = (mew_e - mew_o)*t; #Path difference between E-ray and O-ray(m)\ndphi = (2*math.pi)/lamda*dx; #Phase retardation for quartz for given wavelength(rad)\ndphi = dphi/math.pi;\n\n#Result\nprint \"The phase retardation for quartz for given wavelength is\",dphi, \"pi rad\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The phase retardation for quartz for given wavelength is 1.152 pi rad\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 5.6, Page number 114" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Brewster angle at the boundary\n\n#importing modules\nimport math\n\n#Variable declaration\nC = 52; #Critical angle for total internal reflection(degrees)\n\n#Calculation\na = math.pi/180; #conversion factor from degrees to radians\nC = C*a; #Critical angle for total internal reflection(rad)\n#From Brewster's law, math.tan(i_B) = 1_mew_2\n#Also math.sin(C) = 1_mew_2, so that math.tan(i_B) = math.sin(C), solving for i_B\ni_B = math.atan(math.sin(C)); #Brewster angle at the boundary(rad)\nb = 180/math.pi; #conversion factor from radians to degrees\ni_B = i_B*b; #Brewster angle at the boundary(degrees)\n\n#Result\nprint \"The Brewster angle at the boundary between two materials is\",int(i_B), \"degrees\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Brewster angle at the boundary between two materials is 38 degrees\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter6_1.ipynb b/Engineering_Physics_Aruldhas/Chapter6_1.ipynb new file mode 100644 index 00000000..889b1e18 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter6_1.ipynb @@ -0,0 +1,307 @@ +{ + "metadata": { + "name": "Chapter6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "6: Crystallography" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.1, Page number 134" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the lattice parameter\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nM = 23+35.5; #Molecular weight of NaCl(kg/k-mole)\nd = 2.18*10**3; #Density of rock salt(kg/m**3)\nn = 4; #Number of atoms per unit cell for an fcc lattice of NaCl crystal\nN = 6.02*10**26; #Avogadro's No., atoms/k-mol\n\n#Calculation\na = (n*M/(d*N))**(1/3); #Lattice constant of unit cell of NaCl(m)\na = a*10**9; ##Lattice constant of unit cell of NaCl(nm)\na = math.ceil(a*10**3)/10**3; #rounding off the value of a to 3 decimals\n\n#Result\nprint \"Lattice parameter for the NaCl crystal is\",a, \"nm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Lattice parameter for the NaCl crystal is 0.563 nm\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.2, Page number 134" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the miller indices of the plane\n\n#importing modules\nimport math\n\n#Variable declaration\nm = 3;\nn = 2; \np = 1; #Coefficients of intercepts along three axes\n\n#Calculation\n#reciprocals of the intercepts are 1/m, 1/n, 1/p i.e 1/3, 1/2, 1\n#multiplying by LCM the reciprocals become 2, 3, 6\n\n#Result\nprint \"The required miller indices are : (2, 3, 6)\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The required miller indices are : (2, 3, 6)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.3, Page number 135" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the miller indices of the plane\n\n#importing modules\nimport math\n\n#Variable declaration\nm = 2; #Coefficient of intercept along x-axis\n#n = infinite Coefficient of intercept along y-axis\np = 3/2; #Coefficient of intercept along z-axis\n\n#Calculation\n#reciprocals of the intercepts are 1/m, 1/n, 1/p i.e 1/2, 0, 2/3\n#multiplying by LCM the reciprocals become 3, 0, 4\n\n#Result\nprint \"The required miller indices are : (3, 0, 4)\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The required miller indices are : (3, 0, 4)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.4, Sketching not possible" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.5, Page number 136" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the interplanar spacing between planes\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\n#For (110) planes\nh1 = 1;\nk1 = 1;\nl1 = 0; #Miller Indices for planes in a cubic crystal\na1 = 0.43; #Interatomic spacing(nm)\n#For (212) planes\nh2 = 2; \nk2 = 1;\nl2 = 2; #Miller Indices for planes in a cubic crystal\na2 = 0.43; #Interatomic spacing(nm)\n\n#Calculation\nd1 = a1/(h1**2+k1**2+l1**2)**(1/2); #The interplanar spacing for cubic crystals(nm)\nd1 = math.ceil(d1*10**4)/10**4; #rounding off the value of d1 to 4 decimals\nd2 = a2/(h2**2+k2**2+l2**2)**(1/2); #The interplanar spacing for cubic crystals(nm)\nd2 = math.ceil(d2*10**4)/10**4; #rounding off the value of d2 to 4 decimals\n\n#Result\nprint \"The interplanar spacing between consecutive (110) planes is\",d1, \"nm\";\nprint \"The interplanar spacing between consecutive (212) planes is\",d2, \"nm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The interplanar spacing between consecutive (110) planes is 0.3041 nm\nThe interplanar spacing between consecutive (212) planes is 0.1434 nm\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.6, Page number 136" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the interplanar spacing between planes\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nh = 2;\nk = 3;\nl = 1; #Miller Indices for planes in a cubic crystal\nr = 0.175; #Atomic radius of fcc lattice(nm)\n\n#Calculation\na = 2*math.sqrt(2)*r; #Interatomic spacing of fcc lattice(nm)\nd = a/(h**2+k**2+l**2)**(1/2); #The interplanar spacing for cubic crystals(nm)\nd = math.ceil(d*10**4)/10**4; #rounding off the value of d to 4 decimals\n\n#Result\nprint \"The interplanar spacing between consecutive (231) planes is\",d, \"nm\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The interplanar spacing between consecutive (231) planes is 0.1323 nm\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.7, Page number 136" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the angle of diffraction\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nlamda = 1.44; #Wavelength of X-rays(A)\nd = 2.8; #Interplanar spacing of rocksalt crystal(A)\nn1 = 1; #For 1st Order diffraction\nn2 = 2; #For 2nd Order diffraction\n\n#Calculation\ntheta1 = math.asin(n1*lamda/(2*d)); #Angle of diffraction(radians)\ntheeta1 = theta1*57.2957795; #Angle of diffraction(degrees)\ntheeta1 = math.ceil(theeta1*10**2)/10**2; #rounding off the value of theeta1 to 2 decimals\ntheta2 = math.asin(n2*lamda/(2*d)); #Angle of diffraction(radians)\ntheeta2 = theta2*57.2957795; #Angle of diffraction(degrees)\ntheeta2 = math.ceil(theeta2*10**2)/10**2; #rounding off the value of theeta2 to 2 decimals\n\n#Result\nprint \"The angle of diffraction for first order maxima is\",theeta1, \"degrees\"\nprint \"The angle of diffraction for second order maxima is\",theeta2, \"degrees\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The angle of diffraction for first order maxima is 14.91 degrees\nThe angle of diffraction for second order maxima is 30.95 degrees\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.8, Page number 136" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the percentage of actual volume occupied by the spheres\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\na = 1; #For convenience, assume interatomic spacing to be unity(m)\n\n#Calculation\nN = 8*(1/8) + 6*(1/2); #total number of spheres in a unit cell\nr = a/(2*math.sqrt(2)); #The atomic radius(m)\nV_atom = N*(4/3)*math.pi*r**3; #Volume of atoms(m**3)\nV_uc = a**3; #Volume of unit cell(m**3)\nPV = (V_atom/V_uc)*100; #percentage of actual volume\nPV = math.ceil(PV*10)/10; #rounding off the value of PV to 1 decimal\n\n#Result\nprint \"The percentage of actual volume occupied by the spheres in fcc structure is\",PV, \"percent\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The percentage of actual volume occupied by the spheres in fcc structure is 74.1 percent\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.9, Page number 137" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the interplanar spacing, wavelength and angle\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\n#For (221) planes\nh = 2; \nk = 2; \nl = 1; #Miller Indices for planes in a cubic crystal\na = 2.68; #Interatomic spacing(A)\nn1 = 1; #First Order of diffraction \nn2 = 2; #Second order of diffraction\ntheta1 = 8.5; #Glancing angle at which Bragg's reflection occurs(degrees)\n\n#Calculation\ntheta1 = theta1*0.0174532925; #Glancing angle at which Bragg's reflection occurs(radians)\na = a*10**-10; #Interatomic spacing(m)\nd = a/(h**2+k**2+l**2)**(1/2); #The interplanar spacing for cubic crystal(m)\nlamda = 2*d*math.sin(theta1)/n1; #Bragg's Law for wavelength of X-rays(m)\nlamda_A = lamda*10**10; #Bragg's Law for wavelength of X-rays(A)\nlamda_A = math.ceil(lamda_A*10**4)/10**4; #rounding off the value of lamda_A to 4 decimals\ntheta2 = math.asin(n2*lamda/(2*d)); #Angle at which second order Bragg reflection occurs(radians)\ntheta2 = theta2*57.2957795; #Angle at which second order Bragg reflection occurs(degrees)\ntheta2 = math.ceil(theta2*10)/10; #rounding off the value of theta2 to 1 decimal\n\n#Result\nprint \"The interplanar spacing between consecutive (221) planes is\",d, \"m\"\nprint \"The wavelength of X-rays is\",lamda_A, \"angstrom\"\nprint \"The angle at which second order Bragg reflection occurs is\",theta2, \"degrees\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The interplanar spacing between consecutive (221) planes is 8.93333333333e-11 m\nThe wavelength of X-rays is 0.2641 angstrom\nThe angle at which second order Bragg reflection occurs is 17.2 degrees\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.10, Page number 137" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the lattice parameter for cubic crystal\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nh = 1; \nk = 1;\nl = 0; #Miller Indices for planes in a cubic crystal\nn = 1; #First Order of diffraction \ntheta = 25; #Glancing angle at which Bragg's reflection occurs(degrees)\nlamda = 0.7; #Wavelength of X-rays(A)\n\n#Calculation\ntheta = theta*0.0174532925; #Glancing angle at which Bragg's reflection occurs(radians)\nd = n*lamda/(2*math.sin(theta)); #Interplanar spacing of cubic crystal(A)\na = d*(h**2+k**2+l**2)**(1/2); #The lattice parameter for cubic crystal(A)\na = math.ceil(a*10**3)/10**3; #rounding off the value of a to 3 decimals\n\n#Result\nprint \"The lattice parameter for cubic crystal is\",a, \"angstrom\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The lattice parameter for cubic crystal is 1.172 angstrom\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.11, Page number 138" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the wavelength of X-rays and maximum possible order of diffraction\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nd = 0.31; #Interplanar spacing(nm)\nn = 1; #First Order of diffraction \ntheta = 9.25; #Glancing angle at which Bragg's reflection occurs(degrees)\ntheta_max = 90; #Maximum possible angle at which reflection can occur(degrees)\ntheta_max = theta_max*0.0174532925; #Maximum possible angle at which reflection can occur(radians)\n\n#Calculation\ntheta = theta*0.0174532925; #Glancing angle at which Bragg's reflection occurs(radians)\nlamda = 2*d*math.sin(theta)/n; #Wavelength of X-rays(nm) (Bragg's Law)\nlamda = math.ceil(lamda*10**5)/10**5; #rounding off the value of lamda to 5 decimals\nn = 2*d*math.sin(theta_max)/lamda; #Maximum possible order of diffraction\n\n#Result\nprint \"The wavelength of X-rays is\",lamda, \"nm\"\nprint \"The Maximum possible order of diffraction is\",round(n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The wavelength of X-rays is 0.09967 nm\nThe Maximum possible order of diffraction is 6.0\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.12, Page number 138" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Bragg reflection angle\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\n#For (110) planes\nh1 = 1;\nk1 = 1;\nl1 = 0; #Miller indices for (110) planes\nd_110 = 0.195; #Interplanar spacing between (110) planes(nm)\n#For (210) planes\nh2 = 2;\nk2 = 1; \nl2 = 0; #Miller indices for (110) planes\nn = 2; #Second Order of diffraction \nlamda = 0.071; #Wavelength of X-rays(nm)\n\n#Calculation\na = d_110*(h1**2 + k1**2 + l1**2)**(1/2); #Lattice parameter for bcc crystal(nm)\nd_210 = a/(h2**2 + k2**2 + l2**2)**(1/2); #Interplanar spacing between (210) planes(nm)\ntheta = math.asin(n*lamda/(2*d_210)); #Bragg reflection angle for the second order diffraction(radians)\ntheeta = theta*57.2957795; #Bragg reflection angle for the second order diffraction(degrees)\ntheeta = math.ceil(theeta*10**3)/10**3; #rounding off the value of theeta to 3 decimals\n\n#Result\nprint \"Bragg reflection angle for the second order diffraction is\",theeta, \"degrees\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Bragg reflection angle for the second order diffraction is 35.149 degrees\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.13, Page number 138" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the distance between nearest neighbours\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nd = 2182; #Density of rock salt(kg/m**3)\nn = 4; #Number of atoms per unit cell for an fcc lattice of NaCl crystal\nN = 6.02*10**26; #Avogadro's number(atoms/k-mol)\n\n#Calculation\nM = 23+35.5; #Molecular weight of NaCl(kg/k-mole)\n#V = a^3 = M*n/(N*d)\na = (n*M/(d*N))**(1/3); #Lattice constant of unit cell of NaCl(m)\nD = a/2; #distance between nearest neighbours(m)\nD = D*10**9; #distance between nearest neighbours(nm)\nD = math.ceil(D*10**4)/10**4; #rounding off the value of D to 4 decimals\n\n#Result\nprint \"The distance between nearest neighbours of NaCl structure is\",D, \"nm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The distance between nearest neighbours of NaCl structure is 0.2814 nm\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.14, Page number 139" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the percentage change in volume\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nr1 = 1.258; #Atomic radius of bcc structure of iron(A)\nN1 = 2; #Number of atoms per unit cell in bcc structure\n#For fcc structure\nr2 = 1.292; #Atomic radius of fcc structure of iron(A)\nN2 = 4; #Number of atoms per unit cell in fcc structure\n\n#Calculation\na1 = 4*r1/math.sqrt(3); #Lattice parameter of bcc structure of iron(A)\nV1 = a1**3; #Volume of bcc unit cell(A)\nV_atom_bcc = V1/N1; #Volume occupied by one atom(A)\na2 = 2*math.sqrt(2)*r2; #Lattice parameter of fcc structure of iron(A)\nV2 = a2**3; #Volume of fcc unit cell(A)\nV_atom_fcc = V2/N2; #Volume occupied by one atom(A)\ndelta_V = (V_atom_bcc-V_atom_fcc)/V_atom_bcc*100; #Percentage change in volume due to structural change of iron\ndelta_V = math.ceil(delta_V*10**3)/10**3; #rounding off the value of delta_V to 3 decimals\n\n#Result\nprint \"The percentage change in volume of iron is\",delta_V, \"percent\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The percentage change in volume of iron is 0.494 percent\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter7_1.ipynb b/Engineering_Physics_Aruldhas/Chapter7_1.ipynb new file mode 100644 index 00000000..5b1859d1 --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter7_1.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "7: Superconductivity" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.1, Page number 152" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the critical field\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nTc=3.722; #critical temperature(K)\nT=2; #temperature(K)\nBc_0=0.0305; #critical field(T)\n\n#Calculation\nBc_T=Bc_0*(1-(T/Tc)**2); #critical field at 2K(T)\nBc_T = math.ceil(Bc_T*10**4)/10**4; #rounding off the value of Bc_T to 4 decimals\n\n#Result\nprint \"The critical field at 2K is\",Bc_T, \"T\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The critical field at 2K is 0.0217 T\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.2, Page number 152" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the frequency of Josephson current\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nV = 1; #DC voltage applied across the Josephson junction(micro-volt)\ne = 1.6*10**-19; #Charge on an electron(C)\nh = 6.626*10**-34; #Planck's constant(Js)\n\n#Calculation\nV = V*10**-6; #DC voltage applied across the Josephson junction(V)\nf = 2*e*V/h; #Frequency of Josephson current(Hz)\nf = f*10**-6; #Frequency of Josephson current(MHz)\nf = math.ceil(f*10**2)/10**2; #rounding off the value of f to 2 decimals\n\n#Result\nprint \"The frequency of Josephson current is\",f, \"MHz\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The frequency of Josephson current is 482.95 MHz\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.3, Page number 152" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the superconducting energy gap\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nT_c = 0.517; #Critical temperature for cadmium(K)\nk = 1.38*10**-23; #Boltzmann constant(J/K)\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\n\n#Calculation\nE_g = 3.5*k*T_c/e; #Superconducting energy gap at absolute zero(eV)\nE_g = E_g*10**4;\nE_g = math.ceil(E_g*10**3)/10**3; #rounding off the value of E_g to 3 decimals\n\n#Result\nprint \"The superconducting energy gap for Cd at absolute zero is\",E_g,\"*10**-4 eV\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The superconducting energy gap for Cd at absolute zero is 1.561 *10**-4 eV\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.4, Page number 152" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the wavelength of photon\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nc = 3*10**8; #Speed of light in free space(m/s)\nh = 6.626*10**-34; #Planck's constant(Js)\nE_g = 1.5*10**-4; #Superconducting energy gap for a material(eV)\n\n#Calculation\n#As E_g = h*new = h*c/lamda, solving for lambda\nlamda = h*c/(E_g*e); #Wavelength of photon to break up a Cooper-pair(m)\nlamda = lamda*10**3;\nlamda = math.ceil(lamda*10**3)/10**3; #rounding off the value of lamda to 3 decimals\n\n#Result\nprint \"The wavelength of photon to break up a Cooper-pair is\",lamda,\"*10**-3 m\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The wavelength of photon to break up a Cooper-pair is 8.283 *10**-3 m\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.5, Page number 153" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the London penetration depth of lead\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nlambda_0 = 37; #Penetration depth of lead at 0 kelvin(nm)\nT_c = 7.193; #Critical temperature of superconducting transition for lead(kelvin)\nT = 5.2; #Temperature at which penetration depth for lead becomes lambda_T(kelvin) \n\n#Calculation\nlambda_T = lambda_0*(1-(T/T_c)**4)**(-1/2); #Penetration depth of lead at 5.2 kelvin(nm)\nlambda_T = math.ceil(lambda_T*10)/10; #rounding off the value of lamda_T to 1 decimal\n\n#Result\nprint \"The penetration depth of lead is\",lambda_T, \"nm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The penetration depth of lead is 43.4 nm\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.6, Page number 153" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the mass of isotope of mercury\n\n#importing modules\nfrom __future__ import division\nimport math\n\n#Variable declaration\nM1 = 199; #Mass of an isotope of mercury(amu)\nT_C1 = 4.185; #Transition temperature of the isoptope of Hg(K)\nT_C2 = 4.153; #Transition temperature of another isoptope of Hg(K)\nalpha = 0.5; #Isotope coefficient\n\n#Calculation\nM2 = M1*(T_C1/T_C2)**(1/alpha); #Mass of another isotope of mercury(amu)\nM2 = math.ceil(M2*100)/100; #rounding off the value of M2 to 2 decimals\n\n#Result\nprint \"The mass of another isotope of mercury is\",M2, \"amu\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The mass of another isotope of mercury is 202.08 amu\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter8_1.ipynb b/Engineering_Physics_Aruldhas/Chapter8_1.ipynb new file mode 100644 index 00000000..8ec25fcf --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter8_1.ipynb @@ -0,0 +1,313 @@ +{ + "metadata": { + "name": "Chapter8" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "8: Special Theory of Relativity" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.1, Page number 171" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the speed\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nL_0 = 1; #For simplicity, we assume classical length to be unity(m)\nc = 1; #For simplicity assume speed of light to be unity(m/s)\n\n#Calculation\nL = (1-1/100)*L_0; #Relativistic length(m)\n#Relativistic length contraction gives L = L_0*sqrt(1-v^2/c^2), solving for v\nv = math.sqrt(1-(L/L_0)**2)*c; #Speed at which relativistic length is 1 percent of the classical length(m/s)\nv = math.ceil(v*10**4)/10**4; #rounding off the value of v to 4 decimals\n\n#Result\nprint \"The speed at which relativistic length is 1 percent of the classical length is\",v, \"c\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The speed at which relativistic length is 1 percent of the classical length is 0.1411 c\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.2, Page number 171" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the proper lifetime of particle\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 1; #For simplicity assume speed of light to be unity(m/s)\ndelta_t = 5*10**-6; #Mean lifetime of particles as observed in the lab frame(s)\n\n#Calculation\nv = 0.9*c; #Speed at which beam of particles travel(m/s)\ndelta_tau = delta_t*math.sqrt(1-(v/c)**2); #Proper lifetime of particle as per Time Dilation rule(s)\n\n#Result\nprint \"The proper lifetime of particle is\",delta_tau, \"s\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The proper lifetime of particle is 2.17944947177e-06 s\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.3, Page number 171. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.4, Page number 172" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the speed of second rocket for same and opposite direction\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 1; #For simplicity assume speed of light to be unity(m/s)\n\n#Calculation\nv = 0.6*c; #Speed with which the rocket leaves the earth(m/s)\nu_prime = 0.9*c; #Relative speed of second rocket w.r.t. the first rocket(m/s)\nu1 = (u_prime+v)/(1+(u_prime*v)/c**2); #Speed of second rocket for same direction of firing as per Velocity Addition Rule(m/s)\nu1 = math.ceil(u1*10**4)/10**4; #rounding off the value of u1 to 4 decimals\nu2 = (-u_prime+v)/(1-(u_prime*v)/c**2); #Speed of second rocket for opposite direction of firing as per Velocity Addition Rule(m/s)\nu2 = math.ceil(u2*10**4)/10**4; #rounding off the value of u2 to 4 decimals\n\n#Result\nprint \"The speed of second rocket for same direction of firing is\",u1,\"c\"\nprint \"The speed of second rocket for opposite direction of firing is\",u2,\"c\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The speed of second rocket for same direction of firing is 0.9741 c\nThe speed of second rocket for opposite direction of firing is -0.6521 c\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.5, Page number 172" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the speed and time dilation of spaceship\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 1; #For simplicity assume speed of light to be unity(m/s)\nL0 = 1; #For simplicity assume length in spaceship's frame to be unity(m)\ntau = 1; #Unit time in the spaceship's frame(s)\n\n#Calculation\nL = 1/2*L0; #Length as observed on earth(m)\n#Relativistic length contraction gives L = L_0*sqrt(1-v^2/c^2), solving for v\nv = math.sqrt(1-(L/L0)**2)*c; #Speed at which length of spaceship is observed as half from the earth frame(m/s)\nt = tau/math.sqrt(1-(v/c)**2); #Time dilation of the spaceship's unit time(s)\nv = math.ceil(v*10**4)/10**4; #rounding off the value of v to 4 decimals\n\n#Result\nprint \"The speed at which length of spaceship is observed as half from the earth frame is\",v, \"c\"\nprint \"The time dilation of the spaceship unit time is\",t,\"delta_tau\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The speed at which length of spaceship is observed as half from the earth frame is 0.8661 c\nThe time dilation of the spaceship unit time is 2.0 delta_tau\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.6, Page number 172" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the time difference and distance between the events\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 3*10**8; #Speed of light in vacuum(m/s)\nt1 = 2*10**-7; #Time for which first event occurs(s)\nt2 = 3*10**-7; #Time for which second event occurs(s)\nx1 = 10; #Position at which first event occurs(m)\nx2 = 40; #Position at which second event occurs(m)\n\n#Calculation\nv = 0.6*c; #Velocity with which S2 frame moves relative to S1 frame(m/s)\nL_factor = 1/math.sqrt(1-(v/c)**2); #Lorentz factor\ndelta_t = L_factor*(t2 - t1)+L_factor*v/c**2*(x1 - x2); #Time difference between the events(s)\ndelta_x = L_factor*(x2 - x1)-L_factor*v*(t2 - t1); #Distance between the events(m)\n\n#Result\nprint \"The time difference between the events is\",delta_t, \"s\" \nprint \"The distance between the events is\",delta_x, \"m\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The time difference between the events is 5e-08 s\nThe distance between the events is 15.0 m\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.7, Page number 173" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the speed of the unstable particle\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 3*10**8; #Speed of light in vacuum(m/s)\ntau = 2.6*10**-8; #Mean lifetime the particle in its own frame(s)\nd = 20; #Distance which the unstable particle travels before decaying(m)\n\n#Calculation\n#As t = d/v and also t = tau/sqrt(1-(v/c)^2), so that\n#d/v = tau/sqrt(1-(v/c)^2), solving for v\nv = math.sqrt(d**2/(tau**2+(d/c)**2)); #Speed of the unstable particle in lab frame(m/s)\nv = v/10**8;\nv = math.ceil(v*10)/10; #rounding off the value of v to 1 decimal\n\n#Result\nprint \"The speed of the unstable particle in lab frame is\",v,\"*10**8 m/s\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The speed of the unstable particle in lab frame is 2.8 *10**8 m/s\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.8, Page number 174" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the speed, effective mass and kinetic energy of mu-meson\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 1; #For simplicity assume speed of light to be unity(m/s)\nme = 1; #For simplicity assume mass of electron to be unity(kg)\ntau = 2.3*10**-6; #Average lifetime of mu-meson in rest frame(s)\nt = 6.9*10**-6; #Average lifetime of mu-meson in laboratory frame(s)\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nC = 3*10**8; #Speed of light in vacuum(m/s)\nm_e = 9.1*10**-31; #Mass of an electron(kg)\n\n#Calculation\n#Fromm Time Dilation Rule, tau = t*sqrt(1-(v/c)^2), solving for v\nv = c*math.sqrt(1-(tau/t)**2); #Speed of mu-meson in the laboratory frame(m/s)\nv = math.ceil(v*10**5)/10**5; #rounding off the value of v to 5 decimals\nm0 = 207*me; #Rest mass of mu-meson(kg)\nm = m0/math.sqrt(1-(v/c)**2); #Relativistic variation of mass with velocity(kg)\nm = math.ceil(m*10)/10; #rounding off the value of m to 1 decimal\nT = (m*m_e*C**2 - m0*m_e*C**2)/e; #Kinetic energy of mu-meson(eV)\nT = T*10**-6; #Kinetic energy of mu-meson(MeV)\nT = math.ceil(T*100)/100; #rounding off the value of T to 2 decimals\n \n#Result\nprint \"The speed of mu-meson in the laboratory frame is\",v, \"c\"\nprint \"The effective mass of mu-meson is\",m, \"me\"\nprint \"The kinetic energy of mu-meson is\",T, \"MeV\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The speed of mu-meson in the laboratory frame is 0.94281 c\nThe effective mass of mu-meson is 621.1 me\nThe kinetic energy of mu-meson is 211.97 MeV\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.9, Page number 174" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the speed of moving body\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 1; #For simplicity assume speed of light to be unity(m/s)\nm0 = 1; #For simplicity assume rest mass to be unity(kg)\n\n#Calculation\nm = (20/100+1)*m0; #Mass in motion(kg)\n#As m = m0/sqrt(1-(u/c)^2), solving for u\nu = math.sqrt(1-(m0/m)**2)*c; #Speed of moving mass(m/s) \nu = math.ceil(u*10**3)/10**3; #rounding off the value of u to 3 decimals\n\n#Result\nprint \"The speed of moving body is\",u, \"c\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The speed of moving body is 0.553 c\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.10, Page number 175" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the rate of decrease of mass\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 3*10**8; #Speed of light in vacuum(m/s)\ndE = 4*10**26; #Energy radiated per second my the sun(J/s)\n\n#Calculation\ndm = dE/c**2; #Rate of decrease of mass of sun(kg/s)\ndm = dm/10**9;\ndm = math.ceil(dm*10**3)/10**3; #rounding off the value of dm to 3 decimals\n\n#Result\nprint \"The rate of decrease of mass of sun is\",dm,\"*10**9 kg/s\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The rate of decrease of mass of sun is 4.445 *10**9 kg/s\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.11, Page number 175" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the momentum and velocity of the electron\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 1; #For simplicity assume speed of light to be unity(m/s)\nm0 = 9.1*10**-31; #Mass of the electron(kg)\nE0 = 0.512; #Rest energy of electron(MeV)\nT = 10; #Kinetic energy of electron(MeV)\n\n#Calculation\nE = T + E0; #Total energy of electron(MeV)\n# From Relativistic mass-energy relation E^2 = c^2*p^2 + m0^2*c^4, solving for p\np = math.sqrt(E**2-m0**2*c**4)/c; #Momentum of the electron(MeV)\np = math.ceil(p*100)/100; #rounding off the value of p to 2 decimals\n#As E = E0/sqrt(1-(u/c)^2), solving for u\nu = math.sqrt(1-(E0/E)**2)*c; #Velocity of the electron(m/s)\nu = math.ceil(u*10**4)/10**4; #rounding off the value of u to 4 decimals\n\n#Result\nprint \"The momentum of the electron is\",p,\"/c MeV\"\nprint \"The velocity of the electron is\",u, \"c\"\n\n#answer for velocity given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The momentum of the electron is 10.52 /c MeV\nThe velocity of the electron is 0.9989 c\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.12, Page number 175. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.13, Page number 176" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the rest mass of the body\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 3*10**8; #Speed of light in vacuum(m/s)\nE = 4.5*10**17; #Total energy of object(J)\npx = 3.8*10**8; #X-component of momentum(kg-m/s)\npy = 3*10**8; #Y-component of momentum(kg-m/s)\npz = 3*10**8; #Z-component of momentum(kg-m/s)\n\n#Calculation\np = math.sqrt(px**2+py**2+pz**2); #Total momentum of the object(kg-m/s)\n#From Relativistic mass-energy relation E^2 = c^2*p^2 + m0^2*c^4, solving for m0\nm0 = math.sqrt(E**2/c**4 - p**2/c**2); #Rest mass of the body(kg)\nm0 = math.ceil(m0*100)/100; #rounding off the value of m0 to 2 decimals\n\n#Result\nprint \"The rest mass of the body is\",m0, \"kg\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The rest mass of the body is 4.63 kg\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.14, Page number 176" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the momentum of the high speed probe\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 3*10**8; #Speed of light in vacuum(m/s)\nm = 50000; #Mass of high speed probe(kg)\n\n#Calculation\nu = 0.8*c; #Speed of the probe(m/s)\np = m*u/math.sqrt(1-(u/c)**2); #Momentum of the probe(kg-m/s)\n\n#Result\nprint \"The momentum of the high speed probe is\",p, \"kg-m/s\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The momentum of the high speed probe is 2e+13 kg-m/s\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.15, Page number 177" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the change in mass, work done and accelerating potential\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Electronic charge, C = Energy equivalent of 1 eV(J/eV)\nm0 = 9.11*10**-31; #Rest mass of electron(kg)\nc = 3*10**8; #Speed of light in vacuum(m/s)\n\n#Calculation\nu1 = 0.98*c; #Inital speed of electron(m/s)\nu2 = 0.99*c; #Final speed of electron(m/s)\nm1 = m0/math.sqrt(1-(u1/c)**2); #Initial relativistic mass of electron(kg)\nm2 = m0/math.sqrt(1-(u2/c)**2); #Final relativistic mass of electron(kg)\ndm = m2 - m1; #Change in relativistic mass of the electron(kg)\nW = dm*c**2/e; #Work done on the electron to change its velocity(eV)\nW = W*10**-6; #Work done on the electron to change its velocity(MeV)\nW = math.ceil(W*100)/100; #rounding off the value of W to 2 decimals\n#As W = eV, V = accelerating potential, solving for V\nV = W*10**6; #Accelerating potential(volt)\nV = V/10**6;\n\n#Result\nprint \"The change in relativistic mass of the electron is\",dm, \"kg\"\nprint \"The work done on the electron to change its velocity is\",W, \"MeV\"\nprint \"The accelerating potential is\",V, \"*10**6 volt\"\n\n#answers given in the book are wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The change in relativistic mass of the electron is 1.87996052912e-30 kg\nThe work done on the electron to change its velocity is 1.06 MeV\nThe accelerating potential is 1.06 *10**6 volt\n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/Chapter9_1.ipynb b/Engineering_Physics_Aruldhas/Chapter9_1.ipynb new file mode 100644 index 00000000..50e4b6bd --- /dev/null +++ b/Engineering_Physics_Aruldhas/Chapter9_1.ipynb @@ -0,0 +1,202 @@ +{ + "metadata": { + "name": "Chapter9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "9: Quantum Mechanics" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.1, Page number 202" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the De-Broglie wavelength of electron\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nV = 100; #Accelerating potential for electron(volt)\n\n#Calculation\nlamda = math.sqrt(150/V)*10**-10; #de-Broglie wavelength of electron(m)\n\n#Result\nprint \"The De-Broglie wavelength of electron is\",lamda, \"m\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The De-Broglie wavelength of electron is 1.22474487139e-10 m\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.2, Page number 203" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the De-Broglie wavelength of electron\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nh = 6.626*10**-34; #Planck's constant(Js)\nm = 9.11*10**-31; #Mass of the electron(kg)\nEk = 10; #Kinetic energy of electron(eV)\n\n#Calculation\np = math.sqrt(2*m*Ek*e); #Momentum of the electron(kg-m/s)\nlamda = h/p ; #de-Broglie wavelength of electron from De-Broglie relation(m)\nlamda = lamda*10**9; #de-Broglie wavelength of electron from De-Broglie relation(nm)\nlamda = math.ceil(lamda*10**2)/10**2; #rounding off the value of lamda to 2 decimals\n\n#Result\nprint \"The de-Broglie wavelength of electron is\",lamda, \"nm\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The de-Broglie wavelength of electron is 0.39 nm\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.3, Page number 203. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.4, Page number 203" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the uncertainty in position of electron\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nh = 6.626*10**-34; #Planck's constant(Js)\nm = 9.11*10**-31; #Mass of the electron(kg)\nv = 1.1*10**6; #Speed of the electron(m/s)\npr = 0.1; #precision in percent\n\n#Calculation\np = m*v; #Momentum of the electron(kg-m/s)\ndp = pr/100*p; #Uncertainty in momentum(kg-m/s)\nh_bar = h/(2*math.pi); #Reduced Planck's constant(Js)\ndx = h_bar/(2*dp); #Uncertainty in position(m)\n\n#Result\nprint \"The uncertainty in position of electron is\",dx, \"m\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The uncertainty in position of electron is 5.26175358211e-08 m\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.5, Page number 203" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the uncertainty in energy of the excited state\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\ne = 1.6*10**-19; #Energy equivalent of 1 eV(J/eV)\nh = 6.626*10**-34; #Planck's constant(Js)\ndt = 10**-8; #Uncertainty in time(s)\n\n#Calculation\nh_bar = h/(2*math.pi); #Reduced Planck's constant(Js)\ndE = h_bar/(2*dt*e); #Uncertainty in energy of the excited state(m)\n\n#Result\nprint \"The uncertainty in energy of the excited state is\",dE, \"eV\"\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The uncertainty in energy of the excited state is 3.2955020404e-08 eV\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.6, Page number 204" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the width of spectral line\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nc = 3*10**8; #Speed of light(m/s)\ndt = 10**-8; #Average lifetime(s)\nlamda = 400; #Wavelength of spectral line(nm)\n\n#Calculation\nlamda = lamda*10**-9; #Wavelength of spectral line(m)\n#From Heisenberg uncertainty principle,\n#dE = h_bar/(2*dt) and also dE = h*c/lambda^2*d_lambda, which give\n#h_bar/(2*dt) = h*c/lambda^2*d_lambda, solving for d_lambda\nd_lamda = (lamda**2)/(4*math.pi*c*dt); #Width of spectral line(m)\n\n#Result\nprint \"The width of spectral line is\",d_lamda, \"m\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The width of spectral line is 4.24413181578e-15 m\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.7, Page number 204. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.8, Page number 204. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.9, Page number 205. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.10, Page number 205. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.11, Page number 205. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.12, Page number 206. theoritical proof" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.13, Page number 206. theoritical proof " + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.14, Page number 207" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the probability of finding the electron\n\n#importing modules\nimport math\nfrom __future__ import division\nfrom scipy.integrate import quad\n\n#Variable declaration\na = 2*10**-10; # Width of 1D box(m)\nx1=0; # Position of first extreme of the box(m)\nx2=1*10**-10; # Position of second extreme of the box(m)\n\n#Calculation\ndef intg(x):\n return ((2/a)*(math.sin(2*math.pi*x/a))**2)\nS=quad(intg,x1,x2)[0]\n\n#Result\nprint \"The probability of finding the electron between x = 0 and x = 10**-10 is\",S", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The probability of finding the electron between x = 0 and x = 10**-10 is 0.5\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/README.txt b/Engineering_Physics_Aruldhas/README.txt new file mode 100644 index 00000000..77c00324 --- /dev/null +++ b/Engineering_Physics_Aruldhas/README.txt @@ -0,0 +1,10 @@ +Contributed By: KRISHNA CHAITANYA +Course: btech +College/Institute/Organization: JNTUH +Department/Designation: Computer Science +Book Title: Engineering Physics +Author: G. Aruldhas +Publisher: PHI Learning ( New Delhi ) +Year of publication: 2012 +Isbn: 9788120339163 +Edition: 2
\ No newline at end of file diff --git a/Engineering_Physics_Aruldhas/screenshots/Untitled.png b/Engineering_Physics_Aruldhas/screenshots/Untitled.png Binary files differnew file mode 100644 index 00000000..cb197fae --- /dev/null +++ b/Engineering_Physics_Aruldhas/screenshots/Untitled.png diff --git a/Engineering_Physics_Aruldhas/screenshots/Untitled_1.png b/Engineering_Physics_Aruldhas/screenshots/Untitled_1.png Binary files differnew file mode 100644 index 00000000..cb197fae --- /dev/null +++ b/Engineering_Physics_Aruldhas/screenshots/Untitled_1.png diff --git a/Engineering_Physics_Aruldhas/screenshots/Untitled_2.png b/Engineering_Physics_Aruldhas/screenshots/Untitled_2.png Binary files differnew file mode 100644 index 00000000..cb197fae --- /dev/null +++ b/Engineering_Physics_Aruldhas/screenshots/Untitled_2.png diff --git a/Engineering_Physics_Aruldhas/screenshots/shot1.PNG b/Engineering_Physics_Aruldhas/screenshots/shot1.PNG Binary files differnew file mode 100644 index 00000000..7e165764 --- /dev/null +++ b/Engineering_Physics_Aruldhas/screenshots/shot1.PNG diff --git a/Engineering_Physics_Aruldhas/screenshots/shot2.PNG b/Engineering_Physics_Aruldhas/screenshots/shot2.PNG Binary files differnew file mode 100644 index 00000000..c4b3d4ef --- /dev/null +++ b/Engineering_Physics_Aruldhas/screenshots/shot2.PNG diff --git a/Engineering_Physics_Aruldhas/screenshots/shot3.PNG b/Engineering_Physics_Aruldhas/screenshots/shot3.PNG Binary files differnew file mode 100644 index 00000000..e14ec109 --- /dev/null +++ b/Engineering_Physics_Aruldhas/screenshots/shot3.PNG diff --git a/Engineering_Physics_Marikani/Chapter_1.ipynb b/Engineering_Physics_Marikani/Chapter_1.ipynb new file mode 100644 index 00000000..4392ba50 --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_1.ipynb @@ -0,0 +1,133 @@ +{ + "metadata": { + "name": "Chapter 1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Ultrasonics" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.1, Page number 28 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the fundamental frequency of vibration\n\n#importing modules\nimport math\n\n#Variable declaration\nt=0.15*10**-2; #thickness of the quartz crystal in m\nY=7.9*10**10; #young's modulus of quartz in N/m^2\nrho=2650; #density of quartz in kg/m^3\n\n#Calculation\nx=math.sqrt(Y/rho);\nf=x/(2*t);\nf=f*10**-6; #converting f from Hz to MHz\nf=math.ceil(f*10**6)/10**6; #rounding off to 6 decimals\n\n#Result\nprint(\"fundamental frequency of vibration in MHz is\",f);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('fundamental frequency of vibration in MHz is', 1.819992)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.2, Page number 28 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the fundamental frequency and the first overtone\n\n#importing modules\nimport math\n\n#Variable declaration\nt=1e-03; #thickness of the quartz crystal in m\nY=7.9*10**10; #young's modulus of quartz in N/m^2\nrho=2650; #density of quartz in kg/m^3\n\n#Calculation\nx=math.sqrt(Y/rho);\np1=1; #for fundamental frequency p=1\nf1=(p1*x)/(2*t);\nF1=f1/10**6;\nF1=math.ceil(F1*10**5)/10**5; #rounding off to 5 decimals\nf_1=f1*10**-6; #converting f1 from Hz to MHz\nf_1=math.ceil(f_1*10**5)/10**5; #rounding off to 5 decimals\np2=2; #for first overtone p=2\nf2=(p2*x)/(2*t);\nF2=f2/10**6;\nF2=math.ceil(F2*10**5)/10**5; #rounding off to 5 decimals\nf_2=f2*10**-6; #converting f2 from Hz to MHz\nf_2=math.ceil(f_2*10**5)/10**5; #rounding off to 5 decimals\n\n#Result\nprint(\"fundamental frequency in Hz is\",F1,\"*10**6\");\nprint(\"fundamental frequency in MHz is\",f_1);\nprint(\"frequency of the first overtone in Hz is\",F2,\"*10**6\");\nprint(\"frequency of the first overtone in MHz is\",f_2);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('fundamental frequency in Hz is', 2.72999, '*10**6')\n('fundamental frequency in MHz is', 2.72999)\n('frequency of the first overtone in Hz is', 5.45998, '*10**6')\n('frequency of the first overtone in MHz is', 5.45998)\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.3, Page number 29 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the velocity of ultrasonic wave\n\n#importing modules\nimport math\n\n#Variable declaration\nlamda=589.3*10**-9; #wavelength of light in m\nf=100*10**6; #frequency of ultrasonic transducer in Hz\nn=1; #order of diffraction\ntheta=2.25; #angle of diffraction in degrees\ntheta=theta*0.0174532925; #converting degrees to radians\n\n#Calculation\nd=(n*lamda)/(2*math.sin(theta));\nd1=d*10**6; #converting d from m to micro m\nlamda1=2*d;\nv=f*lamda1;\nv=math.ceil(v*100)/100; #rounding off to 2 decimals\n\n#Result\nprint(\"wavelength of ultrasonic wave in m is\",lamda1);\nprint(\"velocity of ultrasonic wave in m/sec\",int(v));", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('wavelength of ultrasonic wave in m is', 1.5010258944908707e-05)\n('velocity of ultrasonic wave in m/sec', 1501)\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.4, Page number 29 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Doppler shifted frequency\n\n#importing modules\nimport math\n\n#Variable declaration\nf=2*10**6; #frequency of transducer in MHz\nv=3; #speed of blood in m/s\nc=800; #velocity of ultrasonic wave in m/s\ntheta=30; #angle of inclination in degrees\ntheta=theta*0.0174532925; #converting degrees to radians\n\n#Calculation\ndeltaf=(2*f*v*math.cos(theta))/c;\ndeltaf=deltaf*10**-6; #converting deltaf from Hz to MHz\ndeltaf=math.ceil(deltaf*10**6)/10**6; #rounding off to 6 decimals\n\n#Result\nprint(\"doppler shifted frequency in MHz is\",deltaf);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('doppler shifted frequency in MHz is', 0.012991)\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 1.5, Page number 30 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the velocity of ultrasonic wave\n\n#importing modules\nimport math\n\n#Variable declaration\nY=7.9*10**10; #young's modulus of quartz in N/m^2\nrho=2650; #density of quartz in kg/m^3\n\n#Calculation\nv=math.sqrt(Y/rho);\nv=math.ceil(v*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"velocity of ultrasonic waves in m/s is\",v);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('velocity of ultrasonic waves in m/s is', 5459.975)\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_10.ipynb b/Engineering_Physics_Marikani/Chapter_10.ipynb new file mode 100644 index 00000000..d1968a2f --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_10.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter 10" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Magnetic materials" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.1, Page number 305" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the intensity of magnetisation and flux density\n\n#importing modules\nimport math\n\n#Variable declaration\nH=10**6; #magnetic field strength in A/m\nchi=0.5*10**-5; #magnetic susceptibility\n\n#Calculation\nmew0=4*math.pi*10**-7;\nM=chi*H;\nB=mew0*(M+H);\nB=math.ceil(B*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"intensity of magnetisation in A/m is\",M);\nprint(\"flux density in Wb/m^2 is\",B);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('intensity of magnetisation in A/m is', 5.0)\n('flux density in Wb/m^2 is', 1.257)\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.2, Page number 306" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the magnetic moment\n\n#importing modules\nimport math\n\n#Variable declaration\nA=6.022*10**23; #avagadro number\nmew0=4*math.pi*10**-7;\nw=58.7; #atomic weight of Ni\nB=0.65; #saturation magnetic induction in Wb/m^2\nrho=8906; #density in kg/m^3\n\n#Calculation\nrho=rho*10**3; #converting into gm/m^3\nN=(rho*A)/w;\nmew_m=B/(N*mew0);\n#mew_m/(9.27*10^-24) gives mew_m in mewB\nmew_m=mew_m/(9.27*10**-24);\nmew_m=math.ceil(mew_m*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"magnetic moment of Ni is\",mew_m,\"mew_b\");\n#that is mew_m=0.61 mew_b", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('magnetic moment of Ni is', 0.611, 'mew_b')\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.3, Page number 306" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the permeability\n\n#importing modules\nimport math\n\n#Variable declaration\nmew_0=4*math.pi*10**-7;\nH=1800; #magnetic field in A/m\nphi=3*10**-5; #magnetic flux in Wb\nA=0.2; #area of cross section in cm^2\n\n#Calculation\nA=A*10**-4; #area in m^2\nB=phi/A;\nmew_r=B/(mew_0*H);\nmew_r=math.ceil(mew_r*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"permeability of material is\",mew_r);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('permeability of material is', 663.146)\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.4, Page number 307" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the saturation magnetisation \n\n#importing modules\nimport math\n\n#Variable declaration\nmew=18.4; #magnetic moment in mew_b\na=0.835; #lattice parameter in nm\n\n#Calculation\nmew=mew*9.27*10**-24;\na=a*10**-9; #converting nm to m\nV=a**3;\nM=mew/V;\nM=M/10**5;\nM=math.ceil(M*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"saturation magnetisation in A/m is\",M,\"*10**5\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('saturation magnetisation in A/m is', 2.9299, '*10**5')\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.5, Page number 307" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the value of B and M\n\n#importing modules\nimport math\n\n#Variable declaration\nmew_0=4*math.pi*10**-7;\nH=2*10**5; #magnetic field strength in A/m\nmew_r=1.01; #relative permeability\n\n#Calculation\nB=mew_0*mew_r*H;\nB=math.ceil(B*10**5)/10**5; #rounding off to 3 decimals\nM=(B/mew_0)-H;\nM=math.ceil(M*10**2)/10**2; #rounding off to 2 decimals\n\n#Result\nprint(\"magnetic flux density in Wb/m^2 is\",B);\nprint(\"magnetisation in A/m is\",M);\n\n#answer for magnetisation given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('magnetic flux density in Wb/m^2 is', 0.25385)\n('magnetisation in A/m is', 2007.42)\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 10.6, Page number 307" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the magnetic flux density\n\n#importing modules\nimport math\n\n#Variable declaration\nmew_0=4*math.pi*10**-7;\nH=500; #magnetic field strength in A/m\nchi=1.2; #susceptibility\n\n#Calculation\nM=chi*H;\nB=mew_0*(M+H);\nB=B*10**3;\nB=math.ceil(B*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"magnetic flux density in Wb/m^2 is\",B,\"*10**-3\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('magnetic flux density in Wb/m^2 is', 1.3824, '*10**-3')\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_11.ipynb b/Engineering_Physics_Marikani/Chapter_11.ipynb new file mode 100644 index 00000000..6e1a896d --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_11.ipynb @@ -0,0 +1,175 @@ +{ + "metadata": { + "name": "Chapter 11" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Dielectric materials" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.1, Page number 335" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the relative dielectric constant\n\n#importing modules\nimport math\n\n#Variable declaration\nepsilon_0=8.854*10**-12;\nA=10*10*10**-6; #area of capacitor in m^2\nd=2*10**-3; #distance of seperation in m\nC=10**-9; #capacitance in F\n\n#Calculation\nepsilon_r=(C*d)/(epsilon_0*A);\nepsilon_r=math.ceil(epsilon_r*10**2)/10**2; #rounding off to 2 decimals\n\n#Result\nprint(\"dielectric constant of material is\",epsilon_r);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('dielectric constant of material is', 2258.87)\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.2, Page number 335" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electronic polarizability of atoms\n\n#Variable declaration\nepsilon_0=8.854*10**-12;\nepsilon_r=1.0000684; #dielectric constant of He gas\nN=2.7*10**25; #concentration of dipoles per m^3\n\n#Calculation\n#alpha_e=P/(N*E) and P=epsilon_0(epsilon_r-1)*E\n#therefore alpha_e=epsilon_0(epsilon_r-1)/N\nalpha_e=(epsilon_0*(epsilon_r-1))/N;\n\n#Result\nprint(\"electronic polarizability of He gas in Fm^2 is\",alpha_e);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('electronic polarizability of He gas in Fm^2 is', 2.2430133333322991e-41)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.3, Page number 336" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the polarisation\n\n#Variable declaration\nepsilon_0=8.854*10**-12;\nepsilon_r=6; #dielectric constant\nE=100; #electric field intensity in V/m\n\n#Calculation\nP=epsilon_0*(epsilon_r-1)*E;\n\n#Result\nprint(\"polarization in C/m^2 is\",P);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('polarization in C/m^2 is', 4.426999999999999e-09)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.4, Page number 336" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electronic polarizability of Ne\n\n#importing modules\nimport math\n\n#Variable declaration\nepsilon_0=8.854*10**-12;\nR=0.158; #radius of Ne in nm\n\n#Calculation\nR=R*10**-9; #converting nm to m\nalpha_e=4*math.pi*epsilon_0*R**3;\n\n#Result\nprint(\"electronic polarizability in Fm^2 is\",alpha_e);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('electronic polarizability in Fm^2 is', 4.3885458748002144e-40)\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.5, Page number 336" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the area of metal sheet\n\n#importing modules\nimport math\n\n#Variable declaration\nepsilon_0=8.854*10**-12;\nC=0.02; #capacitance in micro farad\nepsilon_r=6; #dielectric constant\nt=0.002; #thickness of mica in cm\nd=0.002; #thickness of metal sheet in cm\n\n#Calculation\nC=C*10**-6; #converting micro farad to farad\nd=d*10**-2; #converting cm to m\nA=(C*d)/(epsilon_0*epsilon_r);\nA=A*10**3;\nA=math.ceil(A*10**4)/10**4; #rounding off to 4 decimals\nA1=A*10; #converting m**2 to cm**2\nA1=math.ceil(A1*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"area of metal sheet in m^2 is\",A,\"*10**-3\");\nprint(\"area of metal sheet in cm^2 is\",A1);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('area of metal sheet in m^2 is', 7.5296, '*10**-3')\n('area of metal sheet in cm^2 is', 75.296)\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.6, Page number 336" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the relative permitivity of the crystal\n\n#importing modules\nimport math\n\n#Variable declaration\nepsilon_0=8.854*10**-12;\nE=1000; #electric field in V/m\nP=4.3*10**-8; #polarization in C/m^2\n\n#Calculation\nepsilon_r=(P/(E*epsilon_0)+1);\nepsilon_r=math.ceil(epsilon_r*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"dielectric constant is\",epsilon_r);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('dielectric constant is', 5.8566)\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 11.7, Page number 337" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the polarisability of the material\n\n#Variable declaration\nepsilon_0=8.854*10**-12;\nchi=4.94; #relative susceptibility\nN=10**28; #number of dipoles per m^3\n\n#Calculation\n#polarisation P=N*alpha*E and P=epsilon_0*chi*E. equate the two equations\n#epsilon_0*chi*E=N*alpha*E\nalpha=(epsilon_0*chi)/N;\n\n#Result\nprint(\"polarisability of material in F/m^2 is\",alpha);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('polarisability of material in F/m^2 is', 4.373876e-39)\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_12.ipynb b/Engineering_Physics_Marikani/Chapter_12.ipynb new file mode 100644 index 00000000..5d0b74d0 --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_12.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter 12" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Superconducting Materials" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.1, Page number 356" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the critical field\n\n#importing modules\nimport math\n\n#Variable declaration\nTc=3.7; #critical temperature in K\nH0=0.0306; #magnetic field in T\nT=2; #temperature in K\n\n#Calculation\nHc=H0*(1-(T**2/Tc**2));\nHc=math.ceil(Hc*10**5)/10**5; #rounding off to 5 decimals\n\n#Result\nprint(\"critical field in T is\",Hc);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('critical field in T is', 0.02166)\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.2, Page number 356" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the critical field\n\n#importing modules\nimport math\n\n#Variable declaration\nTc=7.26; #critical temperature in K\nH0=6.4*10**3; #magnetic field in T\nT=5; #temperature in K\n\n#Calculation\nHc=H0*(1-(T**2/Tc**2));\nHc=math.ceil(Hc*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"critical field in T is\",Hc);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('critical field in T is', 3364.385)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.3, Page number 357" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the value of Tc\n\n#importing modules\nimport math\n\n#Variable declaration\nTc1=4.185; #critical temperature in K\nM1=199.5; #atomic mass\nM2=203.4; #atomic mass after changing\n\n#Calculation\n#according to maxwell equation Tc*M^0.5=constant\n#Tc1*M1^0.5=Tc2*M2^0.5\nTc2=(Tc1*M1**0.5)/M2**0.5;\nTc2=math.ceil(Tc2*10**6)/10**6; #rounding off to 6 decimals\n\n#Result\nprint(\"critical temperature of Hg in K is\",Tc2);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('critical temperature of Hg in K is', 4.144685)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.4, Page number 357" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the critical current density\n\n#importing modules\nimport math\n\n#Variable declaration\nd=1; #diameter of wire in mm\nT=4.2; #temperature in K\nTc=7.18; #critical temperature in K\nH0=6.5*10**4; #magnetic field\n\n#Calculation\nd=d*10**-3; #diameter in m\nR=d/2;\nHc=H0*(1-(T**2/Tc**2));\nHC=Hc/10**4;\nHC=math.ceil(HC*10**3)/10**3; #rounding off to 2 decimals\nIc=2*math.pi*R*Hc;\nIc=math.ceil(Ic*10**2)/10**2; #rounding off to 2 decimals\nA=math.pi*R**2;\nJ=Ic/A;\nJ=J/10**8;\nJ=math.ceil(J*10**5)/10**5; #rounding off to 5 decimals\n\n#Result\nprint(\"critical magnetic field at 4.2K in A/m is\",HC,\"*10**4\");\nprint(\"critical current in A is\",Ic);\nprint(\"critical current density in A/m^2 is\",J,\"*10**8\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('critical magnetic field at 4.2K in A/m is', 4.276, '*10**4')\n('critical current in A is', 134.33)\n('critical current density in A/m^2 is', 1.71035, '*10**8')\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.5, Page number 358" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the frequency of radiation\n\n#importing modules\nimport math\n\n#Variable declaration\ne=1.6*10**-19;\nh=6.626*10**-34;\nV=6; #voltage applied in micro volts\n\n#Calculation\nV=V*10**-6; #converting micro volts to volts\nnew=(2*e*V)/h;\nnew=new/10**9;\nnew=math.ceil(new*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"frequency of ac signal in Hz is\",new,\"*10**9\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('frequency of ac signal in Hz is', 2.8977, '*10**9')\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 12.6, Page number 358" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the band gap\n\n#importing modules\nimport math\n\n#Variable declaration\nKb=1.38*10**-23;\nTc=7.19; #critical temperature in K\n\n#Calculation\nEg=3.5*Kb*Tc;\nEg=Eg/(1.6*10**-19); #converting J to eV\nEg=Eg*10**3; #converting eV into milli eV\nEg=math.ceil(Eg*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"band gap of superconducting lead in meV is\",Eg);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('band gap of superconducting lead in meV is', 2.171)\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_2.ipynb b/Engineering_Physics_Marikani/Chapter_2.ipynb new file mode 100644 index 00000000..f57dc5cb --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_2.ipynb @@ -0,0 +1,238 @@ +{ + "metadata": { + "name": "Chapter 2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Laser" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.1, Page number 59 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number of photons emitted by laser\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.626*10**-34;\nc=3*10**8;\nlamda=632.8*10**-9; #wavelength in m\nP=5*10**-3; #output power in W\n\n#Calculation\nE=(h*c)/lamda; #energy of one photon\nE_eV=E/(1.6*10**-19); #converting J to eV\nE_eV=math.ceil(E_eV*1000)/1000; #rounding off to 3 decimals\nN=P/E; #number of photons emitted\n\n\n#Result\nprint(\"energy of one photon in eV is\",E_eV);\nprint(\"number of photons emitted per second is\",N);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('energy of one photon in eV is', 1.964)\n('number of photons emitted per second is', 1.5917094275077976e+16)\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.2, Page number 60" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the energy of emitted photons\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.626*10**-34;\nc=3*10**8;\nlamda=632.8*10**-9; #wavelength in m\n\n#Calculation\nE=(h*c)/lamda; #energy of one photon\nE_eV=E/(1.6*10**-19); #converting J to eV\nE_eV=math.ceil(E_eV*1000)/1000; #rounding off to 3 decimals\n\n#Result\nprint(\"energy of one photon in eV is\",E_eV);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('energy of one photon in eV is', 1.964)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.3, Page number 60" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the value of E3\n\n#importing modules\nimport math\n\n#Variable declaration\nE1=0; #value of 1st energy level in eV\nE2=1.4; #value of 2nd energy level in eV\nlamda=1.15*10**-6;\nh=6.626*10**-34;\nc=3*10**8;\n\n#Calculation\nE=(h*c)/lamda; #energy of one photon\nE_eV=E/(1.6*10**-19); #converting J to eV\nE3=E2+E_eV;\nE3=math.ceil(E3*100)/100; #rounding off to 2 decimals\n\n#Result\nprint(\"value of E3 in eV is\",E3);\n\n#answer given in the book for E3 is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('value of E3 in eV is', 2.49)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.4, Page number 60" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the wavelength of laser beam\n\n#Variable declaration\nh=6.626*10**-34;\nc=3*10**8;\nE2=3.2; #value of higher energy level in eV\nE1=1.6; #value of lower energy level in eV\n\n#Calculation\nE=E2-E1; #energy difference in eV\nE_J=E*1.6*10**-19; #converting E from eV to J\nlamda=(h*c)/E_J; #wavelength of photon\n\n#Result\nprint(\"energy difference in eV\",E);\nprint(\"wavelength of photon in m\",lamda);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('energy difference in eV', 1.6)\n('wavelength of photon in m', 7.76484375e-07)\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.5, Page number 60" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the wavelength of laser beam\n\n#Variable declaration\nh=6.626*10**-34;\nc=3*10**8;\nE=1.42*1.6*10**-19; #band gap of GaAs in J\n\n#Calculation\nlamda=(h*c)/E; #wavelength of laser\n\n#Result\nprint(\"wavelength of laser emitted by GaAs in m\",lamda);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('wavelength of laser emitted by GaAs in m', 8.74911971830986e-07)\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.6, Page number 61" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the relative population of energy levels\n\n#importing modules\nimport math\n\n#Variable declaration\nT=300; #temperature in K\nlamda=500*10**-9; #wavelength in m\nh=6.626*10**-34;\nc=3*10**8;\nk=1.38*10**-23;\n\n#Calculation\n#from maxwell and boltzmann law, relative population is given by\n#N1/N2=exp(-E1/kT)/exp(-E2/kT)\n#hence N1/N2=exp(-(E1-E2)/kT)=exp((h*new)/(k*T));\n#new=c/lambda\nR=(h*c)/(lamda*k*T);\nRP=math.exp(R);\n\n#Result\nprint(\"relative population between N1 and N2 is\",RP);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('relative population between N1 and N2 is', 5.068255595981255e+41)\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.7, Page number 61" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To examine the possibility of stimulated emission\n\n#importing modules\nimport math\n\n#Variable declaration\nT=300; #temperature in K\nh=6.626*10**-34;\nc=3*10**8;\nk=1.38*10**-23;\nlamda=600*10**-9; #wavelength in m\n\n#Calculation\nR=(h*c)/(lamda*k*T);\nRs=1/(math.exp(R)-1);\n\n#Result\nprint(\"the ratio between stimulated emission to spontaneous emission is\",Rs);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('the ratio between stimulated emission to spontaneous emission is', 1.7617782449453023e-35)\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.8, Page number 62" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the efficiency of a He-Ne laser\n\n#importing modules\nimport math\n\n#Variable declaration\nP=5*10**-3; #output power in W\nI=10*10**-3; #current in A\nV=3*10**3; #voltage in V\n\n#Calculation\ne=(P*100)/(I*V);\ne=math.ceil(e*10**6)/10**6; #rounding off to 6 decimals\n\n#Result\nprint(\"efficiency of laser in % is\",e);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('efficiency of laser in % is', 0.016667)\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.9, Page number 62" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the intensity of laser beam\n\n#importing modules\nimport math\n\n#Variable declaration\nP=1e-03; #output power in W\nd=1e-06; #diameter in m\n\n#Calculation\nr=d/2; #radius in m\nI=P/(math.pi*r**2); #intensity\nI=I/10**9;\nI=math.ceil(I*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"intensity of laser in W/m^2 is\",I,\"*10**9\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('intensity of laser in W/m^2 is', 1.2733, '*10**9')\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 2.10, Page number 62" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the angular speed and divergence of laser beam\n\n#importing modules\nimport math\n\n#Variable declaration\nlamda=632.8*10**-9; #wavelength in m\nD=5; #distance in m\nd=1*10**-3; #diameter in m\n\n#Calculation\ndeltatheta=lamda/d; #angular speed\ndelta_theta=deltatheta*10**4;\nr=D*deltatheta;\nr1=r*10**3; #converting r from m to mm\nA=math.pi*r**2; #area of the spread\n\n#Result \nprint(\"angular speed in radian is\",delta_theta,\"*10**-4\");\nprint(\"radius of the spread in mm is\",r1);\nprint(\"area of the spread in m^2 is\",A);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('angular speed in radian is', 6.328, '*10**-4')\n('radius of the spread in mm is', 3.164)\n('area of the spread in m^2 is', 3.1450157329451454e-05)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_3.ipynb b/Engineering_Physics_Marikani/Chapter_3.ipynb new file mode 100644 index 00000000..59daaddd --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_3.ipynb @@ -0,0 +1,154 @@ +{ + "metadata": { + "name": "Chapter 3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Fibre Optics" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.1, Page number 98 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the numerical aperture of an optical fibre\n\n#importing modules\nimport math\n\n#Variable declaration\nn1=1.6; #refractive index of core\nn2=1.5; #refractive index of cladding\n\n#Calculation\nNA=math.sqrt((n1**2)-(n2**2));\nNA=math.ceil(NA*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"the numerical aperture of the fibre is\",NA);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('the numerical aperture of the fibre is', 0.5568)\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.2, Page number 98 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the numerical aperture and acceptance angle of a fibre\n\n#importing modules\nimport math\n\n#Variable declaration\nn1=1.54; #refractive index of core\nn2=1.5; #refractive index of cladding\nn0=1;\n\n#Calculation\nNA=math.sqrt((n1**2)-(n2**2)); #numerical aperture of fibre\nNA=math.ceil(NA*10**5)/10**5; #rounding off to 5 decimals\nalpha=math.asin(NA/n0); #acceptance angle in radians\nalpha=alpha*57.2957795; #converting radians to degrees\nalpha=math.ceil(alpha*10**5)/10**5; #rounding off to 5 decimals\ndeg=int(alpha); #converting to degrees\nt=60*(alpha-deg); \nmi=int(t); #converting to minutes\nsec=60*(t-mi); #converting to seconds\nsec=math.ceil(sec*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"the numerical aperture of the fibre is\",NA);\nprint(\"the acceptance angle of the fibre in degrees is\",alpha);\nprint(\"acceptance angle of the fibre is\",deg,\"degrees\",mi,\"minutes\",sec,\"seconds\");\n\n#answer for the angle given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('the numerical aperture of the fibre is', 0.34872)\n('the acceptance angle of the fibre in degrees is', 20.40905)\n('acceptance angle of the fibre is', 20, 'degrees', 24, 'minutes', 32.581, 'seconds')\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.3, Page number 99" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the critical angle\n\n#importing modules\nimport math\n\n#Variable declaration\nn1=1.6; #refractive index of core\nn2=1.49; #refractive index of cladding\n\n#Calculation\nthetac=math.asin(n2/n1); #critical angle in radians\nthetac=thetac*57.2957795; #converting radians to degrees\ntheta_c=math.ceil(thetac*10**3)/10**3; #rounding off to 3 decimals\ndeg=int(thetac); #converting to degrees\nt=60*(thetac-deg); \nmi=int(t); #converting to minutes\nsec=60*(t-mi); #converting to seconds\nsec=math.ceil(sec*10**2)/10**2; #rounding off to 2 decimals\n\n#Result\nprint(\"the critical angle of the fibre in degrees is\",theta_c);\nprint(\"critical angle of the fibre is\",deg,\"degrees\",mi,\"minutes\",sec,\"seconds\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('the critical angle of the fibre in degrees is', 68.631)\n('critical angle of the fibre is', 68, 'degrees', 37, 'minutes', 49.85, 'seconds')\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.4, Page number 99" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the acceptance angle of a fibre\n\n#importing modules\nimport math\n\n#Variable declaration\nNA=0.15; #numerical aperture\nn2=1.55; #refractive index of cladding\nn0=1.33; #refractive index of water\n\n#Calculation\nn1=math.sqrt((NA**2)+(n2**2)); #refractive index\nn_1=math.ceil(n1*10**5)/10**5; #rounding off to 5 decimals\nalpha=math.asin(math.sqrt(n1**2-n2**2)/n0); #acceptance angle in radians\nalpha=alpha*57.2957795; #converting radians to degrees\nalphaa=math.ceil(alpha*10**3)/10**3; #rounding off to 3 decimals\ndeg=int(alpha); #converting to degrees\nt=60*(alpha-deg); \nmi=int(t); #converting to minutes\nsec=60*(t-mi); #converting to seconds\nsec=math.ceil(sec*10**2)/10**2; #rounding off to 2 decimals\n\n#Result\nprint(\"refractive index of the core is\",n_1);\nprint(\"the acceptance angle of the fibre in degrees is\",alphaa);\nprint(\"acceptance angle of the fibre is\",deg,\"degrees\",mi,\"minutes\",sec,\"seconds\");\n\n#answer for acceptance angle given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('refractive index of the core is', 1.55725)\n('the acceptance angle of the fibre in degrees is', 6.476)\n('acceptance angle of the fibre is', 6, 'degrees', 28, 'minutes', 32.55, 'seconds')\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.5, Page number 100" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the refractive index of cladding\n\n#importing modules\nimport math\n\n#Variable declaration\nNA=0.26; #numerical aperture\nn1=1.5; #refractive index of core\nd=100; #core diameter in micro meter\n\n#Calculation\nd=100*(10**-6); #core diameter in metre\nn2=math.sqrt((n1**2)-(NA**2));\nn2=math.ceil(n2*10**5)/10**5; #rounding off to 5 decimals\n\n#Result\nprint(\"refractive index of the cladding is\",n2);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('refractive index of the cladding is', 1.4773)\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 3.6, Page number 100" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the refractive indices of core and cladding\n\n#importing modules\nimport math\n\n#Variable declaration\nNA=0.26; #numerical aperture\ndelta=0.015; #refractive index difference\n\n#Calculation\n#NA=math.sqrt(n1**2-n2**2)\n#let A=n1**2-n2**2\n#therefore A=NA**2\nA=NA**2;\n#delta=(n1**2-n2**2)/2*(n1**2)\n#let 2*(n1**2) be B\n#therefore B=A/delta\nB=A/delta;\nn1=math.sqrt(B/2);\nn1=math.ceil(n1*100)/100; #rounding off to 2 decimals\nn2=math.sqrt(n1**2-NA**2);\nn2=math.ceil(n2*10**3)/10**3; #rounding off to 4 decimals\n\n#Result\nprint(\"refractive index of the core is\",n1);\nprint(\"refractive index of the cladding is\",n2);\n\n#answer for refractive index of cladding given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('refractive index of the core is', 1.51)\n('refractive index of the cladding is', 1.488)\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_4.ipynb b/Engineering_Physics_Marikani/Chapter_4.ipynb new file mode 100644 index 00000000..02198aef --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_4.ipynb @@ -0,0 +1,343 @@ +{ + "metadata": { + "name": "Chapter 4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Quantum Physics" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.1, Page number 133 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the change in wavelength\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.63*10**-34; #plancks constant in Js\nm0=9.1*10**-31; #mass of the electron in kg\nc=3*10**8; #velocity of light in m/s\nphi=135; #angle of scattering in degrees\nphi=phi*0.0174532925 #converting degrees to radians \n\n#Calculation\ndelta_lamda=(h*(1-math.cos(phi)))/(m0*c);\n\n#Result\nprint(\"change in wavelength in metres is\",delta_lamda);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('change in wavelength in metres is', 4.1458307496867315e-12)\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.2, Page number 134 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the compton shift, wavelength,energy and angle\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.63*10**-34; #plancks constant in Js\nm0=9.1*10**-31; #mass of the electron in kg\nc=3*10**8; #velocity of light in m/s\nlamda=2; #wavelength in angstrom\nlamdaA=lamda*10**-10; #converting lamda from Angstrom to m\nphi=90; #angle of scattering in degrees\nphi=phi*0.0174532925 #converting degrees to radians \n\n#Calculation\ndelta_lamda=(h*(1-math.cos(phi)))/(m0*c);\ndelta_lamda=delta_lamda*10**10; #converting delta_lamda from m to Angstrom\ndelta_lamda=math.ceil(delta_lamda*10**5)/10**5; #rounding off to 5 decimals\nlamda_dash=delta_lamda+lamda;\nlamdaA_dash=lamda_dash*10**-10; #converting lamda_dash from Angstrom to m\n#energy E=h*new-h*new_dash\nE=h*c*((1/lamdaA)-(1/lamdaA_dash));\nEeV=E/(1.602176565*10**-19); #converting J to eV\nEeV=math.ceil(EeV*10**3)/10**3; #rounding off to 3 decimals\nnew=c/lamda;\nnew_dash=c/lamda_dash;\ntheta=math.atan((h*new*math.sin(phi))/((h*new)-(h*new_dash*math.cos(phi))));\ntheta=theta*57.2957795; #converting radians to degrees\n\n#Result\nprint(\"change in compton shift in Angstrom is\",delta_lamda);\nprint(\"wavelength of scattered photons in Angstrom is\",lamda_dash);\nprint(\"energy of recoiling electron in J is\",E);\nprint(\"energy of recoiling electron in eV is\",EeV);\nprint(\"angle at which recoiling electron appears in degrees is\",int(theta));\n\n#answers given in the book are wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('change in compton shift in Angstrom is', 0.02429)\n('wavelength of scattered photons in Angstrom is', 2.02429)\n('energy of recoiling electron in J is', 1.1933272900621974e-17)\n('energy of recoiling electron in eV is', 74.482)\n('angle at which recoiling electron appears in degrees is', 45)\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.3, Page number 135" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the compton shift, wavelength of the scattered photon\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.626*10**-34; #plancks constant in Js\nm0=9.1*10**-31; #mass of the electron in kg\nc=3*10**8; #velocity of light in m/s\nphi=60; #angle of scattering in degrees\nphi=phi*0.0174532925; #converting degrees to radians\nE=10**6; #energy of photon in eV\nE=E*1.6*10**-19; #converting eV into J\n\n#Calculation\ndelta_lamda=(h*(1-math.cos(phi)))/(m0*c);\ndelta_lamda=delta_lamda*10**10; #converting metre to angstrom\ndelta_lamda=math.ceil(delta_lamda*10**4)/10**4; #rounding off to 4 decimals\nlamda=(h*c)/E;\nlamdaA=lamda*10**10; #converting metre to angstrom\nlamda_dash=delta_lamda+lamdaA;\nlamda_dash=math.ceil(lamda_dash*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"compton shift in angstrom is\",delta_lamda);\nprint(\"energy of incident photon in m\",lamda);\nprint(\"wavelength of scattered photons in angstrom is\",lamda_dash);\n\n#answer for wavelength of scattered photon given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('compton shift in angstrom is', 0.0122)\n('energy of incident photon in m', 1.242375e-12)\n('wavelength of scattered photons in angstrom is', 0.025)\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.4, Page number 135" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number of photons emitted\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.626*10**-34; #plancks constant in Js\nc=3*10**8; #velocity of light in m/s\nlamda=5893; #wavelength in angstrom\nP=60; #output power in Watt\n\n#Calculation\nlamda=lamda*10**-10; #wavelength in metre\nE=(h*c)/lamda;\nEeV=E/(1.602176565*10**-19); #converting J to eV\nEeV=math.ceil(EeV*10**4)/10**4; #rounding off to 4 decimals\nN=P/E;\n\n#Result\nprint(\"energy of photon in J is\",E);\nprint(\"energy of photon in eV is\",EeV);\nprint(\"number of photons emitted per se cond is\",N);\n\n#answer for energy in eV given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('energy of photon in J is', 3.373154590191753e-19)\n('energy of photon in eV is', 2.1054)\n('number of photons emitted per se cond is', 1.7787503773015396e+20)\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.5, Page number 136" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the momentum, energy and mass of a photon\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.626*10**-34; #plancks constant in Js\nc=3*10**8; #velocity of light in m/s\nlamda=10; #wavelength in angstrom\n\n#Calculation\nlamda=lamda*10**-10; #wavelength in metre\nE=(h*c)/lamda;\nEeV=E/(1.602176565*10**-19); #converting J to eV\nEeV=EeV*10**-3; #converting eV to keV\nEeV=math.ceil(EeV*10**3)/10**3; #rounding off to 3 decimals\nP=h/lamda;\nM=h/(lamda*c);\n\n#Result\nprint(\"energy of photon in J is\",E);\nprint(\"energy of photon in keV is\",EeV);\nprint(\"momentum in kg m/sec is\",P);\nprint(\"mass of photon in kg is\",M);\n\n#answer for energy of photon in keV given in the book is wrong by 1 decimal", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('energy of photon in J is', 1.9878e-16)\n('energy of photon in keV is', 1.241)\n('momentum in kg m/sec is', 6.626e-25)\n('mass of photon in kg is', 2.2086666666666664e-33)\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.6, Page number 136" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the wavelength\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.626*10**-34; #plancks constant in Js\nm=9.1*10**-31; #mass of the electron in kg\ne=1.602*10**-19;\nV=1.25; #potential difference in kV\n\n#Calculation\nV=V*10**3; #converting kV to V\nlamda=h/math.sqrt(2*m*e*V);\nlamda=lamda*10**10; #converting metre to angstrom\nlamda=math.ceil(lamda*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"de Broglie wavelength in angstrom is\",lamda);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('de Broglie wavelength in angstrom is', 0.3471)\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.7, Page number 136" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the de Broglie wavelength\n\n#importing modules\nimport math\n\n#Variable declaration\nE=45; #energy of electron in eV\nE=E*1.6*10**-19; #energy in J\nh=6.626*10**-34; #plancks constant in Js\nm=9.1*10**-31; #mass of the electron in kg\n\n#Calculation\nlamda=h/math.sqrt(2*m*E);\nlamda=lamda*10**10; #converting metres to angstrom\nlamda=math.ceil(lamda*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"de Broglie wavelength in angstrom is\",lamda);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('de Broglie wavelength in angstrom is', 1.8305)\n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.8, Page number 137" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the de Broglie wavelength\n\n#importing modules\nimport math\n\n#Variable declaration\nv=10**7; #velocity of electron in m/sec\nh=6.626*10**-34; #plancks constant in Js\nm=9.1*10**-31; #mass of the electron in kg\n\n#Calculation\nlamda=h/(m*v);\nlamda=lamda*10**10; #converting metres to angstrom\nlamda=math.ceil(lamda*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"de Broglie wavelength in angstrom is\",lamda);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('de Broglie wavelength in angstrom is', 0.7282)\n" + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.9, Page number 137" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the de Broglie wavelength of alpha particle\n\n#importing modules\nimport math\n\n#Variable declaration\nV=1000; #potential difference in V\nh=6.626*10**-34; #plancks constant in Js\nm=1.67*10**-27; #mass of proton in kg\ne=1.6*10**-19; #charge of electron in J\n\n#Calculation\nlamda=h/math.sqrt(2*m*e*V);\n\n#Result\nprint(\"de Broglie wavelength of alpha particle in metre is\",lamda);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('de Broglie wavelength of alpha particle in metre is', 9.063964727801313e-13)\n" + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.10, Page number 138" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the probability of finding the particle\n\n#importing modules\nimport math\n\n#Variable declaration\nL=25; #width of potential in armstrong\ndelta_x=0.05; #interval in armstrong\nn=1; #particle is in its least energy\nx=L/2; #particle is at the centre\npi=180; #angle in degrees\n\n#Calculation\npi=pi*0.0174532925; #angle in radians\nL=L*10**-10; #width in m\ndelta_x=delta_x*10**-10; #interval in m\n#probability P = integration of (A**2)*(math.sin(n*pi*x/L))**2*delta_x\n#but A=math.sqrt(2/L)\n#since the particle is in a small interval integration need not be applied\n#therefore P=2*(L**(-1))*(math.sin(n*pi*x/L))**2*delta_x\nP=2*(L**(-1))*((math.sin(n*pi*x/L))**2)*delta_x;\nP=math.ceil(P*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"probability of finding the particle is\",P);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('probability of finding the particle is', 0.004)\n" + } + ], + "prompt_number": 27 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.11, Page number 138" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the lowest energy of electron in eV\n\n#importing modules\nimport math\n\n#Variable declaration\nn=1;\nh=6.626*10**-34; #plancks constant in Js\nm=9.1*10**-31; #mass of the electron in kg\nL=1; #width of potential well in angstrom\n\n#Calculation\nL=L*10**-10; #converting angstrom into metre\nE=((n**2)*h**2)/(8*m*L**2);\nEeV=E/(1.6*10**-19); #converting J to eV\nEeV=math.ceil(EeV*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"lowest energy of electron in J is\",E);\nprint(\"lowest energy of electron in eV is\",EeV);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('lowest energy of electron in J is', 6.030752197802197e-18)\n('lowest energy of electron in eV is', 37.693)\n" + } + ], + "prompt_number": 28 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.12, Page number 139" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the lowest energy of the system\n\n#importing modules\nimport math\n\n#Variable declaration\nn=1;\nh=6.626*10**-34; #plancks constant in Js\nm=9.1*10**-31; #mass of the electron in kg\nL=1; #width of potential well in angstrom\n\n#Calculation\nL=L*10**-10; #converting angstrom into metre\nE=(2*(n**2)*h**2)/(8*m*L**2);\nE=E/(1.6*10**-19); #converting J to eV\nE=math.ceil(E*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"lowest energy of system in eV is\",E);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('lowest energy of system in eV is', 75.385)\n" + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.13, Page number 139" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the lowest energy of the system and quantum numbers\n\n#importing modules\nimport math\n\n#Variable declaration\nh=6.626*10**-34; #plancks constant in Js\nm=9.1*10**-31; #mass of the electron in kg\nL=1; #width of potential well in angstrom\n\n#Calculation\nL=L*10**-10; #converting angstrom into metre\n#according to pauli's exclusion principle, 1st electron occupies n1=1 and second electron occupies n2=2\nn1=1;\nn2=2;\nE=((2*(n1**2)*h**2)/(8*m*L**2))+(((n2**2)*h**2)/(8*m*L**2));\nE=E/(1.6*10**-19); #converting J to eV\nE=math.ceil(E*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"lowest energy of system in eV is\",E);\nprint(\"quantum numbers are\");\nprint(\"n=1,l=0,mL=0,mS=+1/2\");\nprint(\"n=1,l=0,mL=0,mS=-1/2\");\nprint(\"n=2,l=0,mL=0,mS=+1/2\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('lowest energy of system in eV is', 226.154)\nquantum numbers are\nn=1,l=0,mL=0,mS=+1/2\nn=1,l=0,mL=0,mS=-1/2\nn=2,l=0,mL=0,mS=+1/2\n" + } + ], + "prompt_number": 30 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.14, Page number 140" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the mass of the particle\n\n#Variable declaration\nn=1;\nh=6.626*10**-34; #plancks constant in Js\nL=100; #width of potential well in angstrom\n\n#Calculation\nL=L*10**-10; #converting angstrom into metre\nE=0.025; #lowest energy in eV\nE=E*(1.6*10**-19); #converting eV to J\nm=((n**2)*h**2)/(8*E*L**2);\n\n#Result\nprint(\"mass of the particle in kg is\",m);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('mass of the particle in kg is', 1.3719961249999998e-31)\n" + } + ], + "prompt_number": 31 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 4.15, Page number 141" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the energy density\n\n#importing modules\nimport math\n\n#Variable declaration\nk=1.38*10**-23;\nT=6000; #temperature in K\nh=6.626*10**-34; #plancks constant in Js\nc=3*10**8; #velocity of light in m/s\nlamda1=450; #wavelength in nm\nlamda2=460; #wavelength in nm\n\n#Calculation\nlamda1=lamda1*10**-9; #converting nm to metre\nlamda2=lamda2*10**-9; #converting nm to metre\nnew1=c/lamda1;\nnew2=c/lamda2;\nnew=(new1+new2)/2;\nA=math.exp((h*new)/(k*T));\nrho_v=(8*math.pi*h*new**3)/(A*c**3);\n\n#Result\nprint(\"energy density of the black body in J/m^3 is\",rho_v);\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('energy density of the black body in J/m^3 is', 9.033622836188887e-16)\n" + } + ], + "prompt_number": 32 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_6.ipynb b/Engineering_Physics_Marikani/Chapter_6.ipynb new file mode 100644 index 00000000..1445b978 --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_6.ipynb @@ -0,0 +1,400 @@ +{ + "metadata": { + "name": "Chapter 6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Crystallography" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.1, Page number 185" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the density of diamond\n\n#importing modules\nimport math\n\n#Variable declaration\nr=0.071; #radius in nm\nN=6.022*10**26; \n\n#Calculation\nr=r*10**-9; #converting r from nm to m\n#mass of carbon atom m = 12/N\nm=12/N;\n#mass of diamond M = 8*mass of one carbon atom\nM=8*m;\n#volume of diamond V = (8*r/sqrt(3))^3\nV=(8*r/math.sqrt(3))**3;\nd=M/V; #density in kg/m^3\nd=math.ceil(d*100)/100; #rounding off to 2 decimals\n\n#Result\nprint(\"density of diamond in kg/m^3 is\",d);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('density of diamond in kg/m^3 is', 4520.31)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.2, Page number 185" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the percentage volume change\n\n#importing modules\nimport math\n\n#Variable declaration\naBCC=0.332; #lattice constant in nm\naHCP=0.296; #lattice constant in nm\nc=0.468; #c in nm\n\n#Calculation\naBCC=aBCC*10**-9; #converting nm to m\nVbcc=aBCC**3;\naHCP=aHCP*10**-9; #converting nm to m\nc=c*10**-9; #converting nm to m\nVhcp=6*(math.sqrt(3)/4)*aHCP**2*c;\nV=Vhcp-Vbcc;\nVch=(V*100)/Vbcc;\nVch=math.ceil(Vch*100)/100; #rounding off to 2 decimals\n\n#Result\nprint(\"percentage change in volume is\",Vch);\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('percentage change in volume is', 191.12)\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.3, Page number 186" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the density\n\n#importing modules\nimport math\n\n#Variable declaration\nr=1.278; #atomic radius of Cu in Angstrom\nA=63.54; #atomic weight of Cu\nn=4; #for FCC n=4\nNa=6.022*10**26;\n\n#Calculation\nr=r*10**-10; #converting atomic radius from Angstrom to m\na=2*math.sqrt(2)*r; \nrho=(n*A)/(Na*a**3);\nrho=math.ceil(rho*100)/100; #rounding off to 2 decimals\n\n#Result\nprint(\"density of Cu in kg/m^3 is\",rho);\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('density of Cu in kg/m^3 is', 8935.92)\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.4, Page number 186" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the distance between adjacent atoms\n\n#importing modules\nimport math\nimport numpy as np\n\n#Variable declaration\nrho=2180; #density of NaCl in kg/m^3\nwNa=23; #atomic weight of Na\nwCl=35.5; #atomic weight of Cl\nn=4; #for FCC n=4\nNa=6.022*10**26;\n\n#Calculation\nA=wNa+wCl; #molecular weight of NaCl\nx=np.reciprocal(3.);\na=((n*A)/(Na*rho))**x;\n\n#Result\nprint(\"interatomic distance in NaCl in m is\",a); \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('interatomic distance in NaCl in m is', 5.6278114346454509e-10)\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.5, Page number 187" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the interplanar spacing\n\n#importing modules\nimport math\n\n#Variable declaration\na=0.42; #lattice constant in nm\nh1=1;\nk1=0;\nl1=1; #indices of the plane (101)\nh2=2;\nk2=2;\nl2=1; #indices of the plane (221)\n\n#Calculation\na=a*10**-9; #converting from nm to m\nd1=a/math.sqrt((h1**2)+(k1**2)+(l1**2)); #interplanar spacing for plane (101)\nd1=d1*10**9; #converting from m to nm\nd1=math.ceil(d1*10**5)/10**5; #rounding off to 5 decimals\nd2=a/math.sqrt((h2**2)+(k2**2)+(l2**2)); #interplanar spacing for plane (221)\nd2=d2*10**9; #converting from m to nm\n\n#Result\nprint(\"interplanar spacing for (101) in nm is\",d1);\nprint(\"interplanar spacing for (221) in nm is\",d2);\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('interplanar spacing for (101) in nm is', 0.29699)\n('interplanar spacing for (221) in nm is', 0.14)\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.6, Page number 187" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To identify the axial intercepts\n\n#Variable declaration\nh1=1;\nk1=0;\nl1=2; #indices for plane (102)\nh2=2;\nk2=3;\nl2=1; #indices for plane (231)\nh3=3;\nk3=-1;\nl3=2; #indices for plane (31'2)\n\n#Calculation\n#intercepts made by the plane is a/h, b/k, c/l\n#for plane (102) intercepts are a/1=a, b/0=infinite, c/2\n#for plane (231) intercepts are a/2, b/3, c/1=c\n#for plane (31'2) intercepts are a/3=a, b/-1=-b, c/2\n\n#Result\nprint(\"for plane (102) intercepts are a/1=a, b/0=infinite, c/2\");\nprint(\"for plane (231) intercepts are a/2, b/3, c/1=c\");\nprint(\"for plane (312) intercepts are a/3=a, b/-1=-b, c/2\");\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "for plane (102) intercepts are a/1=a, b/0=infinite, c/2\nfor plane (231) intercepts are a/2, b/3, c/1=c\nfor plane (312) intercepts are a/3=a, b/-1=-b, c/2\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.7, Page number 188" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the angle between 2 planes\n\n#importing modules\nimport math\n\n#Variable declaration\nu1=1;\nv1=1;\nw1=1; #indices for plane (111)\nu2=2;\nv2=1;\nw2=2; #indices for plane (212)\n\n#Calculation\nA=u1*u2+v1*v2+w1*w2; \nB1=math.sqrt((u1**2)+(v1**2)+(w1**2));\nB2=math.sqrt((u2**2)+(v2**2)+(w2**2));\nB=A/(B1*B2);\nB=math.ceil(B*10**4)/10**4; #rounding off to 4 decimals\ntheta=math.acos(B); #angle in radian\ntheta=theta*57.2957795; #converting radian to degrees\ntheeta=math.ceil(theta*10**3)/10**3; #rounding off to 3 decimals\ndeg=int(theta); #converting to degrees\nt=60*(theta-deg);\nmi=int(t); #converting to minutes\nsec=60*(t-mi); #converting to seconds\nsec=math.ceil(sec*10**2)/10**2; #rounding off to 2 decimals\n\n#Result\nprint(\"angle between the planes in degrees is\",theeta);\nprint(\"angle between the planes is\",deg,\"degrees\",mi,\"minutes\",sec,\"seconds\");\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('angle between the planes in degrees is', 15.783)\n('angle between the planes is', 15, 'degrees', 46, 'minutes', 57.85, 'seconds')\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.8, Page number 188" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#sketching the crystallographic planes", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.9, Page number 189" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the lattice constant\n\n#importing modules\nimport math\n\n#Variable declaration\nd=0.2338; #interplanar distance in nm\nh=-1;\nk=1;\nl=1; #indices of the plane (1'11)\n\n#Calculation\nd=d*10**-9; #converting from nm to m\na=d*math.sqrt((h**2)+(k**2)+(l**2));\na=a*10**9; #converting lattice constant from m to nm\na=math.ceil(a*10**5)/10**5; #rounding off to 5 decimals\n\n#Result\nprint(\"lattice constant in nm is\",a);\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('lattice constant in nm is', 0.40496)\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.10, Page number 189" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To show that for a simple cubic system d100:d110:d111=sqrt(6):sqrt(3):sqrt(2)\n\n#importing modules\nimport math\n\n#variable declaration\nh1=1;\nk1=0;\nl1=0; #indices for plane (100)\nh2=1;\nk2=1;\nl2=0; #indices for plane (110)\nh3=1;\nk3=1;\nl3=1; #indices for plane (111)\n\n#Calculation\n#d=a/math.sqrt((h**2)+(k**2)+(l**2))\n#d100=a/math.sqrt((h1**2)+(k1**2)+(l1**2))\nx1=math.sqrt((h1**2)+(k1**2)+(l1**2));\n#d100=a/x1 = a/1 = a\n#d110=a/math.sqrt((h2**2)+(k2**2)+(l2**2))\nx2=math.sqrt((h2**2)+(k2**2)+(l2**2));\nx2=math.ceil(x2*10**4)/10**4; #rounding off to 4 decimals\n#d110=a/x2 = a/sqrt(2)\n#d111=a/math.sqrt((h3**2)+(k3**2)+(l3**2))\nx3=math.sqrt((h3**2)+(k3**2)+(l3**2));\nx3=math.ceil(x3*10**4)/10**4; #rounding off to 4 decimals\n#d111=a/x3 = a/sqrt(3)\n#hence d100:d110:d111=a:a/sqrt(2):a/sqrt(3)\n#multiplying RHS by sqrt(6) we get d100:d110:d111=sqrt(6):sqrt(3):sqrt(2)\n\n#Result\nprint(\"value of x1 is\",x1);\nprint(\"value of x2 is\",x2);\nprint(\"value of x3 is\",x3);\nprint(\"d100:d110:d111=sqrt(6):sqrt(3):sqrt(2)\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('value of x1 is', 1.0)\n('value of x2 is', 1.4143)\n('value of x3 is', 1.7321)\nd100:d110:d111=sqrt(6):sqrt(3):sqrt(2)\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.11, Page number 190" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To find the ratio of intercepts in a simple cubic crystal\n\n#variable declaration\nh=2;\nk=3;\nl=1; #indices for plane (231)\n\n#Calculation\n#intercepts made by the plane is a/h, b/k, c/l\n#for a cubic unit cell, a=b=c\n#for plane (231) intercepts are a/2, a/3, a/1 = a\n#ratio of the intercepts is 1/2:1/3:1\n#LCM is 6. multiplying by LCM, we get ratio l1:l2:l3 = 3:2:6\n\n#Result\nprint(\"l1:l2:l3 = 3:2:6\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "l1:l2:l3 = 3:2:6\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.12, Page number 190" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To find the lengths of intercepts along Y and Z axes\n\n#variable declaration\nh=1;\nk=2;\nl=3; #indices for plane (123)\nl1=0.8; #l1 in armstrong\na=0.8; #a in armstrong\nb=1.2; #b in armstrong\nc=1.5; #c in armstrong\n\n#Calculation\n#intercepts made by the plane is a/h, b/k, c/l\n#for plane (123) intercepts are a/1 = a, b/2, c/3\n#ratio of the intercepts l1:l2:l3 = a:b/2:c/3\n#thus 0.8:l2:l3 = 0.8:1.2/2:1.5/3\nl2=1.2/2; #l2 in armstrong\nl3=1.5/3; #l3 in armstrong\n\n#Result\nprint(\"value of l2 in armstrong is\",l2);\nprint(\"value of l3 in armstrong is\",l3);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('value of l2 in armstrong is', 0.6)\n('value of l3 in armstrong is', 0.5)\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.13, Page number 191" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the nearest neighbour distance\n\n#Calculation\n#in simple cubic unit cell, corner atom is the nearest neighbour to another corner atom. \n#Hence nearest neighbour distance is a.\n#in BCC the body centered atom is the nearest neighbour to a corner atom.\n#the distance between body centered atom and corner atom is 2r\n#but r=sqrt(3)*a/4\n#distance = 2*sqrt(3)*a/4 = sqrt(3)*a/2\n#in FCC the face centered atom is the nearest neighbour to a corner atom.\n#the distance between face centered atom and corner atom is 2r\n#but r = a/sqrt(8)\n#distance = 2*a/sqrt(8) = a/sqrt(2)\n\n#Result\nprint(\"in simple cubic unit cell nearest neighbour distance is a\");\nprint(\"in body centered cubic unit cell nearest neighbour distance is sqrt(3)*a/2\");\nprint(\"in face centered cubic unit cell nearest neighbour distance is a/sqrt(2)\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "in simple cubic unit cell nearest neighbour distance is a\nin body centered cubic unit cell nearest neighbour distance is sqrt(3)*a/2\nin face centered cubic unit cell nearest neighbour distance is a/sqrt(2)\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.14, Page number 191" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the spacing of the lattice plane\n\n#importing modules\nimport math\n\n#variable declaration\na=2.04; #lattice parameter in armstrong\nh=2;\nk=1;\nl=2; #indices for plane (212)\n\n#Calculation\na=a*10**-10; #converting from armstrong to m\nd=a/math.sqrt((h**2)+(k**2)+(l**2));\nd=d*10**10; #converting from m to armstrong\nd=math.ceil(d*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"interplanar distance in armstrong is\",d);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('interplanar distance in armstrong is', 0.681)\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.15, Page number 191" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number of atoms per unit cell\n\n#importing modules\nimport math\n\n#variable declaration\nr=1.278; #radius of Cu in armstrong\nM=63.54; #atomic weight of Cu\nrho=8980; #density in kg/m^3\nNa=6.022*10**26;\n\n#Calculation\nr=r*10**-10; #radius in m\na=math.sqrt(8)*r;\nn=(rho*Na*a**3)/M;\n\n#Result\nprint(\"interatomic distance in m is\",a);\nprint(\"number of atoms per Cu unit cell is\",int(n));", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('interatomic distance in m is', 3.6147298654256317e-10)\n('number of atoms per Cu unit cell is', 4)\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.16, Page number 192" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Miller indices \n\n#variable declaration\na=0.429;\nb=1;\nc=0.379; #intercepts of an orthorhombic crystal\n\n#Calculation\n#ratio of intercepts are 0.214:1:0.188 = (a/0.429)*0.214:1:(c/0.379)*0.188 = a/2:b:c/2\n#thus the coefficients are 1/2:1:1/2. inverses are 2,1,2.\n#thus miller indices for the first plane are (212)\n#ratio of intercepts are 0.858:1:0.754 = (a/0.429)*0.0.858:1:(c/0.379)*0.754 = 2a:b:2c\n#thus the coefficients are 2:1:2. inverses are 1/2,1,1/2. LCM is 2. multiplying with LCM we get 1,2,1\n#thus miller indices for the second plane are (121)\n#ratio of intercepts are 0.429:infinite:0.126 = (a/0.429)*0.429:infinite:(c/0.379)*0.126 = a:infiniteb:c/3\n#thus the coefficients are 1:infinte:1/3. inverses are 1,0,3.\n#thus miller indices for the third plane are (103)\n\n#Result\nprint(\"miller indices for the first plane are (212)\");\nprint(\"miller indices for the second plane are (121)\");\nprint(\"miller indices for the third plane are (103)\");\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "miller indices for the first plane are (212)\nmiller indices for the second plane are (121)\nmiller indices for the third plane are (103)\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.17, Page number 193" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number of atoms per unit area\n\n#importing modules\nimport math\nimport numpy as np\n\n#variable declaration\nh1=1;\nk1=0;\nl1=0; #indices of the first plane (100)\nh2=1;\nk2=1;\nl2=0; #indices of the second plane (110)\nh3=1;\nk3=1;\nl3=1; #indices of the third plane (111)\n\n#Calculation\nn_1=np.reciprocal(4.);\nn_2=np.reciprocal(2.);\nn_3=np.reciprocal(6.);\nn1=(n_1*4)+1; #number of atoms per unit cell in (100)\n#number of atoms per m^2 is 2/a**2. but a=sqrt(8)*r.\n#hence number of atoms per m^2 is 1/(4*r**2)\nn2=(n_1*4)+(2*n_2); #number of atoms per unit cell in (110)\n#number of atoms per m^2 is 1/a*sqrt(2)*a. but a=sqrt(8)*r.\n#hence number of atoms per m^2 is 1/(8*sqrt(2)*r**2)\nn3=(n_3*3)+(3*n_2); #number of atoms per unit cell in (111)\n#number of atoms per m^2 is 2/(sqrt(3)/4)*a**2. but a=4*r.\n#hence number of atoms per m^2 is 1/(2*sqrt(3)*r**2)\n\n#Result\nprint(\"number of atoms per unit cell in (100)\",n1);\nprint(\"number of atoms per m^2 is 1/(4*r**2)\");\nprint(\"number of atoms per unit cell in (110)\",n2);\nprint(\"number of atoms per m^2 is 1/(8*sqrt(2)*r**2)\");\nprint(\"number of atoms per unit cell in (111)\",n3);\nprint(\"number of atoms per m^2 is 1/(2*sqrt(3)*r**2)\");\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('number of atoms per unit cell in (100)', 2.0)\nnumber of atoms per m^2 is 1/(4*r**2)\n('number of atoms per unit cell in (110)', 2.0)\nnumber of atoms per m^2 is 1/(8*sqrt(2)*r**2)\n('number of atoms per unit cell in (111)', 2.0)\nnumber of atoms per m^2 is 1/(2*sqrt(3)*r**2)\n" + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 6.18, Page number 194" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the atomic packing fraction and ionic packing fraction of NaCl\n\n#importing modules\nimport math\n\n#variable declaration\nr=0.97; #radius of Na+ ion in armstrong\nR=1.81; #radius of Cl- ion in armstrong\n\n#Calculation\n#atomic packing factor=packing density PD\n#PD=Volume of atoms/Volume of unit cell\n#volume of unit cell=a**3\n#volume of atoms=number of atoms*volume of 1 atom = 4*(4/3)*math.pi*r**3\n#but r=a/sqrt(8). hence PD = 4*(4/3)*math.pi*(a/(2*sqrt(2)))**3*(1/a**3) = 0.74\n#atomic packing factor = 0.74\nr=r*10**-10; #radius of Na+ ion in m\nR=R*10**-10; #radius of Cl- ion in m\nVna = (4*4*math.pi*r**3)/3; #volume of Na atoms\nVcl = (4*4*math.pi*R**3)/3; #volume of Cl atoms \nV=(2*(r+R))**3; #volume of unit cell\nIPF=(Vna+Vcl)/V; #ionic packing factor\nIPF=math.ceil(IPF*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"atomic packing factor = 0.74\");\nprint(\"ionic packing factor of NaCl crystal is\",IPF);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "atomic packing factor = 0.74\n('ionic packing factor of NaCl crystal is', 0.6671)\n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_7.ipynb b/Engineering_Physics_Marikani/Chapter_7.ipynb new file mode 100644 index 00000000..c59443c9 --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_7.ipynb @@ -0,0 +1,91 @@ +{ + "metadata": { + "name": "Chapter 7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Crystal Imperfections" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.1, Page number 207 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number of vacancies and vacancy fraction\n\n#importing modules\nimport math\n\n#Variable declaration\nk=1.38*10**-23;\nEv=0.98; #energy in eV/atom\nT1=900; #temperature in C\nT2=1000;\nA=6.022*10**26; #avagadro's constant\nw=196.9; #atomic weight in g/mol\nd=18.63; #density in g/cm^3\n\n#Calculation\nEv=Ev*1.6*10**-19; #converting eV to J\nd=d*10**3; #converting g/cm^3 into kg/m^3\nN=(A*d)/w;\nn=N*math.exp(-Ev/(k*T1));\n#let valency fraction n/N be V\nV=math.exp(-Ev/(k*T2));\n\n#Result\nprint(\"concentration of atoms per m^3 is\",N);\nprint(\"number of vacancies per m^3 is\",n);\nprint(\"valency fraction is\",V);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('concentration of atoms per m^3 is', 5.69780904012189e+28)\n('number of vacancies per m^3 is', 1.8742498047705634e+23)\n('valency fraction is', 1.1625392535344139e-05)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.2, Page number 208 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the energy for vacancy formation\n\n#importing modules\nimport math\n\n#Variable declaration\nk=1.38*10**-23;\nA=6.022*10**26; #avagadro's constant\nT=1073; #temperature in K\nn=3.6*10**23; #number of vacancies\nd=9.5; #density in g/cm^3\nw=107.9; #atomic weight in g/mol\n\n#Calculation\nd=d*10**3; #converting g/cm^3 into kg/m^3\nN=(A*d)/w; #concentration of atoms\nE=k*T*math.log((N/n), ); #energy in J\nEeV=E/(1.602176565*10**-19); #energy in eV\nEeV=math.ceil(EeV*10**2)/10**2; #rounding off to 2 decimals\n\n#Result\nprint(\"concentration of atoms per m^3 is\",N);\nprint(\"energy for vacancy formation in J\",E);\nprint(\"energy for vacancy formation in eV\",EeV);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('concentration of atoms per m^3 is', 5.3020389249304915e+28)\n('energy for vacancy formation in J', 1.762092900344914e-19)\n('energy for vacancy formation in eV', 1.1)\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 7.3, Page number 209 " + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number of Schotky defect\n\n#importing modules\nimport math\n\n#Variable declaration\nA=6.022*10**26; #avagadro's constant\nk=1.38*10**-23;\nw1=39.1; #atomic weight of K\nw2=35.45; #atomic weight of Cl\nEs=2.6; #energy formation in eV\nT=500; #temperature in C\nd=1.955; #density in g/cm^3\n\n#Calculation\nEs=Es*1.6*10**-19; #converting eV to J\nT=T+273; #temperature in K\nd=d*10**3; #converting g/cm^3 into kg/m^3\nN=(A*d)/(w1+w2);\nn=N*math.exp(-Es/(2*k*T));\n\n#Result\nprint(\"number of Schotky defect per m^3 is\",n);\n\n#answer given in the book is wrong by 3rd decimal point", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('number of Schotky defect per m^3 is', 5.373777171020081e+19)\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_8.ipynb b/Engineering_Physics_Marikani/Chapter_8.ipynb new file mode 100644 index 00000000..40606862 --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_8.ipynb @@ -0,0 +1,259 @@ +{ + "metadata": { + "name": "Chapter 8" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Conducting materials" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.1, Page number 231" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electrical resistivity\n\n#Variable declaration\nm=9.1*10**-31; #mass of the electron in kg\nn=2.533*10**28; #concentration of electrons per m^3\ne=1.6*10**-19;\ntow_r=3.1*10**-14; #relaxation time in sec\n\n#Calculation\nrho=m/(n*(e**2*tow_r));\n\n#Result\nprint(\"electrical resistivity in ohm metre is\",rho);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('electrical resistivity in ohm metre is', 4.526937967219795e-08)\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.2, Page number 231" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the band gap of semiconductor\n\n#importing modules\nimport math\n\n#Variable declaration\ns=3.75*10**3; #slope\nk=1.38*10**-23;\n\n#Calculation\nEg=2*k*s;\nEg=Eg/(1.6*10**-19); #converting J to eV\nEg=math.ceil(Eg*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"band gap of semiconductor in eV is\",Eg);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('band gap of semiconductor in eV is', 0.647)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.3, Page number 231" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the probability of occupation of electrons\n\n#importing modules\nimport math\n\n#Variable declaration\nT=989; #temperature in C\nk=1.38*10**-23;\n#let E-EF be E\nE=0.5; #occupied level of electron in eV\n\n#Calculation\nT=T+273; #temperature in K\nE=E*1.6*10**-19; #converting eV to J\n#let fermi=dirac distribution function f(E) be f\nf=1/(1+math.exp(E/(k*T)));\nf=math.ceil(f*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"probability of occupation of electrons is\",f);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('probability of occupation of electrons is', 0.011)\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.4, Page number 232" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the drift velocity of free electrons\n\n#Variable declaration\nmew_e=0.0035; #mobility of electrons in m^2/Vs\nE=0.5; #electric field strength in V/m\n\n#Calculation\nvd=mew_e*E;\nvd=vd*10**3;\n\n#Result\nprint(\"drift velocity of free electrons in m/sec is\",vd,\"*10**-3\");\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('drift velocity of free electrons in m/sec is', 1.75, '*10**-3')\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.5, Page number 232" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the mobility of electrons\n\n#importing modules\nimport math\n\n#Variable declaration\nA=6.022*10**23; #avagadro number\ne=1.6*10**-19;\nrho=1.73*10**-8; #resistivity of Cu in ohm metre\nw=63.5; #atomic weight \nd=8.92*10**3; #density in kg/m^3\n\n#Calculation\nd=d*10**3;\nsigma=1/rho;\nsigmaa=sigma/10**7;\nsigmaa=math.ceil(sigmaa*10**3)/10**3; #rounding off to 3 decimals\nn=(d*A)/w;\nmew=sigma/(n*e); #mobility of electrons\nmew=mew*10**3;\nmew=math.ceil(mew*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"electrical conductivity in ohm-1 m-1\",sigmaa,\"*10**7\");\nprint(\"concentration of carriers per m^3\",n);\nprint(\"mobility of electrons in m^2/Vsec is\",mew,\"*10**-3\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('electrical conductivity in ohm-1 m-1', 5.781, '*10**7')\n('concentration of carriers per m^3', 8.459250393700786e+28)\n('mobility of electrons in m^2/Vsec is', 4.2708, '*10**-3')\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.6, Page number 232" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the fermi energy\n\n#importing modules\nimport math\n\n#Variable declaration\nn=18.1*10**28; #concentration of electrons per m^3\nh=6.62*10**-34; #planck constant in Js\nme=9.1*10**-31; #mass of electron in kg\n\n#Calculation\nX=h**2/(8*me);\nE_F0=X*(((3*n)/math.pi)**(2/3));\nE_F0=E_F0/(1.6*10**-19); #converting J to eV\n\n#Result\nprint(\"Fermi energy in eV is\",E_F0);\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('Fermi energy in eV is', 3.762396978021977e-19)\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.7, Page number 233" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the concentration of free electrons\n\n#importing modules\nimport math\n\n#Variable declaration\nE_F0=5.5; #fermi energy in eV\nh=6.63*10**-34; #planck constant in Js\nme=9.1*10**-31; #mass of electron in kg\n\n#Calculation\nE_F0=E_F0*1.6*10**-19; #converting eV to J\nn=((2*me*E_F0)**(3/2))*((8*math.pi)/(3*h**3));\n\n#Result\nprint(\"concentration of free electrons per unit volume of silver per m^3 is\",n);\n\n#answer given in the book is wrong\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('concentration of free electrons per unit volume of silver per m^3 is', 4.603965704817037e+52)\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.8, Page number 233" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the probability of an electron\n\n#importing modules\nimport math\n\n#Variable declaration\nEg=1.07; #energy gap of silicon in eV\nk=1.38*10**-23;\nT=298; #temperature in K\n\n#Calculation\nEg=Eg*1.6*10**-19; #converting eV to J\n#let the probability of electron f(E) be X\n#X=1/(1+exp((E-Ef)/(k*T)))\n#but E=Ec and Ec-Ef=Eg/2\nX=1/(1+math.exp(Eg/(2*k*T)))\n\n#Result\nprint(\"probability of an electron thermally excited is\",X);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('probability of an electron thermally excited is', 9.122602463573379e-10)\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.9, Page number 234" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the fermi energy and temperature\n\n#importing modules\nimport math\n\n#Variable declaration\nk=1.38*10**-23;\nm=9.1*10**-31; #mass of the electron in kg\nvf=0.86*10**6; #fermi velocity in m/sec\n\n#Calculation\nEfj=(m*vf**2)/2;\nEf=Efj/(1.6*10**-19); #converting J to eV\nEf=math.ceil(Ef*10**3)/10**3; #rounding off to 3 decimals\nTf=Efj/k;\nTf=Tf/10**4;\nTf=math.ceil(Tf*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"fermi energy of metal in J is\",Efj);\nprint(\"fermi energy of metal in eV is\",Ef);\nprint(\"fermi temperature in K is\",Tf,\"*10**4\");\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('fermi energy of metal in J is', 3.3651800000000002e-19)\n('fermi energy of metal in eV is', 2.104)\n('fermi temperature in K is', 2.4386, '*10**4')\n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.10, Page number 234" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Lorentz number\n\n#Variable declaration\nsigma=5.82*10**7; #electrical conductivity in ohm^-1m^-1\nK=387; #thermal conductivity of Cu in W/mK\nT=27; #temperature in C\n\n#Calculation\nT=T+273; #temperature in K\nL=K/(sigma*T);\n\n#Result\nprint(\"lorentz number in W ohm/K^2 is\",L);\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('lorentz number in W ohm/K^2 is', 2.2164948453608246e-08)\n" + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 8.11, Page number 235" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the electrical conductivity, thermal conductivity and Lorentz number\n\n#importing modules\nimport math\n\n#Variable declaration\nm=9.1*10**-31; #mass of the electron in kg\ne=1.6*10**-19;\nk=1.38*10**-23;\nn=8.49*10**28; #concentration of electrons in Cu per m^3\ntow_r=2.44*10**-14; #relaxation time in sec\nT=20; #temperature in C\n\n#Calculation\nT=T+273; #temperature in K\nsigma=(n*(e**2)*tow_r)/m;\nsigmaa=sigma/10**7;\nsigmaa=math.ceil(sigmaa*10**4)/10**4; #rounding off to 4 decimals\nK=(n*(math.pi**2)*(k**2)*T*tow_r)/(3*m);\nK=math.ceil(K*100)/100; #rounding off to 2 decimals\nL=K/(sigma*T);\n\n#Result\nprint(\"electrical conductivity in ohm^-1 m^-1 is\",sigmaa,\"*10**7\");\nprint(\"thermal conductivity in W/mK is\",K);\nprint(\"Lorentz number in W ohm/K^2 is\",L);\n\n#answer for lorentz number given in the book is wrong\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('electrical conductivity in ohm^-1 m^-1 is', 5.8277, '*10**7')\n('thermal conductivity in W/mK is', 417.89)\n('Lorentz number in W ohm/K^2 is', 2.4473623172034308e-08)\n" + } + ], + "prompt_number": 29 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/Chapter_9.ipynb b/Engineering_Physics_Marikani/Chapter_9.ipynb new file mode 100644 index 00000000..4524cd7c --- /dev/null +++ b/Engineering_Physics_Marikani/Chapter_9.ipynb @@ -0,0 +1,259 @@ +{ + "metadata": { + "name": "Chapter 9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": "Semiconducting materials" + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.1, Page number 266" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the number of charge carriers\n\n#Variable declaration\nmew_e=0.36; #mobility of electrons in m^2/Vs\nmew_h=0.14; #mobility of holes in m^2/Vs\nsigma=2.2; #conductivity in ohm-1 m-1\nT=300; #temperature in K\ne=1.6*10**-19; #electron charge in C\n\n#Calculation\nni=sigma/(e*(mew_e+mew_h)); #carrier concentration per m^3\n\n#Result\nprint(\"carrier concentration of an intrinsic semiconductor per m^3 is\",ni);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('carrier concentration of an intrinsic semiconductor per m^3 is', 2.75e+19)\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.2, Page number 266" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the band gap\n\n#importing modules\nimport math\nimport numpy as np\nfrom __future__ import division\n\n#Variable declaration\nT1=20; #temperature in C\nT2=100; #temperature in C\nsigma_i20=250; #conductivity in ohm-1 m-1\nsigma_i100=1100; #conductivity in ohm-1 m-1\nk=1.38*10**-23;\n\n#Calculation\nT1K=T1+273; #temperature in K\nT2K=T2+273; #temperature in K\nT_1K=T1K**(-1);\nT_2K=T2K**(-1);\nT_1=T_2K-T_1K;\nT_2=T2K/T1K;\nTk=T_1**(-1);\nT_k=(T_2)**(3/2);\n#intrinsic carrier concentration at T1K is ni20 = 2*((2*math.pi*k*m*293)/h**2)**(3/2)*((me*mh)/m**2)**(3/4)*math.exp(-Eg/(2*k*293))\n#intrinsic carrier concentration at T2K is ni100 = 2*((2*math.pi*k*m*373)/h**2)**(3/2)*((me*mh)/m**2)**(3/4)*math.exp(-Eg/(2*k*373))\n#dividing ni20/ni100 = (293/373)**(3/2)*(math.exp(-Eg/(2*k*293))/math.exp(-Eg/(2*k*373)))\n#ni20/ni100 = (293/373)**(3/2)*math.exp((-Eg/(2*k))((1/293)-(1/373)))\n#sigma_i20/sigma_i100 = (ni20*e*(mew_e+mew_h))/(ni100*e*(mew_e+mew_h)) = ni20/ni100\n#therefore sigma_i20/sigma_i100 = ni20/ni100 = (293/373)**(3/2)*math.exp((-Eg/(2*k))((1/293)-(1/373)))\n#math.exp((-Eg/(2*k))*((1/293)-(1/373))) = (sigma_i20/sigma_i100)*(373/293)**(3/2)\n#by taking log on both sides we get (-Eg/(2*k))*((1/293)-(1/373)) = np.log((sigma_i20/sigma_i100)*(373/293)**(3/2))\n#Eg=2*k*(((1/373)-(1/293))**(-1))*np.log((sigma_i20/sigma_i100)*(373/293)**(3/2))\nEg=2*k*Tk*np.log((sigma_i20/sigma_i100)*T_k); #band gap in J\nEgeV=Eg*6.241*10**18; #converting J to eV\nEgeV=math.ceil(EgeV*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"band gap of the semiconductor in J is\",Eg);\nprint(\"band gap of the semiconductor in eV is\",EgeV);\n\n#answer for band gap in eV given in the book is wrong in the 4th decimal point", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('band gap of the semiconductor in J is', 4.2210259829756855e-20)\n('band gap of the semiconductor in eV is', 0.2635)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.3, Page number 267" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Hall voltage\n\n#Variable declaration\nI=10**-2; #current in Ampere\nl=100; #length in mm\nd=1; #thickness in mm\nw=10; #breadth in mm\nB=0.5; #magnetic field in Wb/m^2\nRH=3.66*10**-4; #hall coefficient in m^3/C\n\n#Calculation\nw=w*10**-3; #width in m\nVH=(B*I*RH)/w; #hall voltage\nVH=VH*10**4;\n\n#Result\nprint(\"Hall voltage in V is\",VH,\"*10**-4\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('Hall voltage in V is', 1.83, '*10**-4')\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.4, Page number 268" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the concentration of holes and electrons\n\n#importing modules\nimport math\n\n#Variable declaration\nsigma=300; #conductivity in S/cm\nT=300; #temperature in K\nni=1.5*10**10 #carrier concentration per cm^3\nmew_e=1300; #mobility of electrons in cm^2/Vs\nmew_h=500; #mobility of holes in cm^2/Vs\ne=1.6*10**-19; #electron charge in C\n\n#Calculation\nsigma=sigma*10**2; #sigma in S/m\nmew_e=mew_e*10**-4; #mobility of electrons in m^2/Vs\nND=sigma/(e*mew_e); #concentration of electron per m^3\nni=ni*10**6; #carrier concentration per m^3\np=ni**2/ND; #hole concentration per m^3\np=p/10**8;\np=math.ceil(p*10**3)/10**3; #rounding off to 3 decimals\nmew_h=mew_h*10**-4; #mobility of holes in m^2/Vs\nNA=sigma/(e*mew_h); #concentration of hole per m^3\nn=ni**2/NA; #electron concentration per m^3\nn=n/10**7;\n\n#Result\nprint(\"concentration of electron for N-type semiconductor per m^3\",ND);\nprint(\"hole concentration per m^3\",p,\"*10**8\");\nprint(\"concentration of hole for P-type semiconductor per m^3\",NA);\nprint(\"electron concentration per m^3\",int(n),\"*10**7\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('concentration of electron for N-type semiconductor per m^3', 1.4423076923076921e+24)\n('hole concentration per m^3', 1.561, '*10**8')\n('concentration of hole for P-type semiconductor per m^3', 3.7499999999999995e+24)\n('electron concentration per m^3', 6, '*10**7')\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.5, Page number 269" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To find the type of charge carriers and the carrier concentration\n\n#importing modules\nimport math\n\n#Variable declaration\nRH=-3.68*10**-5; #hall coefficient in m^3/C\ne=1.6*10**-19; #electron charge in C\n\n#Calculation\n#hall coefficient is negative implies charge carriers are electrons\nn=(3*math.pi)/(8*(-RH)*e); #carrier concentration\n\n#Result\nprint(\"charge carriers are electrons\");\nprint(\"carrier concentration per m^3 is\",n);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "charge carriers are electrons\n('carrier concentration per m^3 is', 2.000844505937792e+23)\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.6, Page number 269" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To compare the intrinsic carrier density\n\n#importing modules\nimport math\nfrom __future__ import division\n\n#Variable declaration\nEg1=0.36; #energy gap of 1st material in eV\nEg2=0.72; #energy gap of 2nd material in eV\nT=300; #temperature in K\nmh=9*10**-31;\nme=9*10**-31; \n#given that 2*k*T=0.052; \n#consider X=2*k*T\nX=0.052;\n\n#Calculation\n#intrinsic carrier concentration for A niA = 2*((2*math.pi*k*T*m)/h**2)**(3/2)*((me*mh)/m**2)**(3/4)*math.exp(-0.36/(2*k*T))\n#intrinsic carrier concentration for B niB = 2*((2*math.pi*k*T*m)/h**2)**(3/2)*((me*mh)/m**2)**(3/4)*math.exp(-0.72/(2*k*T))\n#dividing niA/niB = math.exp(-0.36/(2*k*T))*math.exp(0.72/(2*k*T))\n#let niA/niB be A\nA = math.exp(-0.36/X)*math.exp(0.72/X);\nA=A/10**3;\nA=math.ceil(A*10**5)/10**5; #rounding off to 5 decimals\n\n#Result\nprint(\"ratio of intrinsic carrier densities of A and B is\",A,\"*10**3\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('ratio of intrinsic carrier densities of A and B is', 1.01544, '*10**3')\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.7, Page number 270" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the mobility of electrons\n\n#importing modules\nimport math\n\n#Variable declaration\nND=2*10**22; #concentration of electron per m^3\nsigma=112; #conductivity in ohm-1 m-1\ne=1.6*10**-19; #electron charge in C\n\n#Calculation\nmew=sigma/(ND*e); #mobility of electrons \nmew=math.ceil(mew*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"mobility of electrons in m^2/Vs is\",mew);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('mobility of electrons in m^2/Vs is', 0.035)\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.8, Page number 270" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the Hall voltage\n\n#importing modules\nimport math\n\n#Variable declaration\nw=500; #thickness in micrometre\nA=2.5*10**-3; #area of cross section in cm^-2\nIx=1; #current in ampere\nBz=10; #magnetic field in Wb/cm^2\nn=10**16; #donor concentration in m^-3\ne=1.6*10**-19; #electron charge in C\n\n#Calculation\nBz=Bz*10**-4; #magnetic field in Wb/m^2\nw=w*10**-6; #thickness in m\nRH=(3*math.pi)/(8*n*e); #hall coefficient\nVH=(Bz*Ix*RH)/w; #hall voltage\nVH=VH/10**3;\nVH=math.ceil(VH*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"hall voltage in V is\",VH,\"*10**3\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('hall voltage in V is', 1.4727, '*10**3')\n" + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.9, Page number 271" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To find the ratio between conductivity of intrinsic semiconductor\n\n#importing modules\nimport math\nfrom __future__ import division\nimport numpy as np\n\n#Variable declaration\nEg=1.2; #energy gap in eV\nT1=300; #temperature in K\nT2=600; #temperature in K\nk=1.38*10**-23;\n\n#Calculation\nT_1=T1**(-1);\nT_2=T2**(-1);\nT=T_1-T_2;\nEg=Eg*1.602*10**-19; #Eg in J\n#sigma_300=ni300*e*(mew_e+mew_h)\n#sigma_600=ni600*e*(mew_e+mew_h)\n#sigma_600/sigma_300 = ni600/ni300\n#ni600/ni300 =((T2/T1)**(3/2))*math.exp(-Eg/(2*k*T2))*math.exp(Eg/(2*k*T1));\n#ni600/ni300 =((T2/T1)**(3/2))*math.exp((Eg/(2*k))*T;\n#let ni600/ni300 be X\nX=((T2/T1)**(3/2))*math.exp((Eg/(2*k))*T);\n\n\n#Result\nprint(\"ratio between the conductivity of material is\",int(X));\n\n#answer given in the book is wrong", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('ratio between the conductivity of material is', 311270)\n" + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.10, Page number 272" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the intrinsic carrier concentration\n\n#importing modules\nimport math\n\n#Variable declaration\nsigma=10**-6; #electrical conductivity in ohm-1 m-1\nmew_e=0.85; #electron mobility in m^2/Vs\nmew_h=0.04; #hole mobility in m^2/Vs\ne=1.6*10**-19; #electron charge in C\n\n#Calculation\nni=sigma/(e*(mew_e+mew_h)); #intrinsic carrier concentration\nni=ni/10**12;\nni=math.ceil(ni*10**4)/10**4; #rounding off to 4 decimals\n\n#Result\nprint(\"intrinsic carrier concentration per m^3 is\",ni,\"*10**12\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('intrinsic carrier concentration per m^3 is', 7.0225, '*10**12')\n" + } + ], + "prompt_number": 27 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": "Example number 9.11, Page number 272" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "#To calculate the density of impurity atoms and concentration of minority carriers\n\n#importing modules\nimport math\n\n#Variable declaration\nrho_p=10; #resistivity of p-type Si in ohm cm\nrho_n=10; #resistivity of n-type Si in ohm cm\nmew_e=1350; #electron mobility in cm^2/Vs\nmew_h=480; #hole mobility in cm^2/Vs\nni=1.5*10**10; #carrier concentration in cm^-3\ne=1.6*10**-19; #electron charge in C\n\n#Calculation\nrho_p=rho_p*10**-2;#resistivity of p-type Si in ohm m\nsigma_p=1/rho_p; #electrical conductivity\nmew_h=mew_h*10**-3;\nNA=sigma_p/(e*mew_h); #acceptor concentration\nni=ni*10**6; #carrier concentration in m^-3\nn=ni**2/NA; #concentration of minority carriers in m^-3\nn=n/10**12;\nn=math.ceil(n*10**4)/10**4; #rounding off to 4 decimals\nrho_n=rho_n*10**-2; #resistivity of n-type Si in ohm m\nsigma_n=1/rho_n; #electrical conductivity\nmew_e=mew_e*10**-3;\nND=sigma_n/(e*mew_e); #donor concentration\np=(ni**2)/ND; #concentration of minority carriers in m^-3\np=p/10**12;\np=math.ceil(p*10**3)/10**3; #rounding off to 3 decimals\n\n#Result\nprint(\"donor concentration per m^3 is\",ND);\nprint(\"concentration of minority carriers per m^3\",p,\"*10**12\");\nprint(\"acceptor concentration per m^3 is\",NA);\nprint(\"concentration of minority carriers per m^3 is\",n,\"*10**12\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "('donor concentration per m^3 is', 4.6296296296296284e+19)\n('concentration of minority carriers per m^3', 4.861, '*10**12')\n('acceptor concentration per m^3 is', 1.3020833333333331e+20)\n('concentration of minority carriers per m^3 is', 1.7281, '*10**12')\n" + } + ], + "prompt_number": 33 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/README.txt b/Engineering_Physics_Marikani/README.txt new file mode 100644 index 00000000..00ccdb95 --- /dev/null +++ b/Engineering_Physics_Marikani/README.txt @@ -0,0 +1,10 @@ +Contributed By: SINDHU ARROJU +Course: btech +College/Institute/Organization: JNTUH +Department/Designation: Computer Science +Book Title: Engineering Physics +Author: A. Marikani +Publisher: PHI Learning ( New Delhi ) +Year of publication: 2009 +Isbn: 9788120339392 +Edition: 1
\ No newline at end of file diff --git a/Engineering_Physics_Marikani/screenshots/screenshot1.PNG b/Engineering_Physics_Marikani/screenshots/screenshot1.PNG Binary files differnew file mode 100644 index 00000000..419ba452 --- /dev/null +++ b/Engineering_Physics_Marikani/screenshots/screenshot1.PNG diff --git a/Engineering_Physics_Marikani/screenshots/screenshot2.PNG b/Engineering_Physics_Marikani/screenshots/screenshot2.PNG Binary files differnew file mode 100644 index 00000000..bf169152 --- /dev/null +++ b/Engineering_Physics_Marikani/screenshots/screenshot2.PNG diff --git a/Engineering_Physics_Marikani/screenshots/screenshot3.PNG b/Engineering_Physics_Marikani/screenshots/screenshot3.PNG Binary files differnew file mode 100644 index 00000000..4062fb31 --- /dev/null +++ b/Engineering_Physics_Marikani/screenshots/screenshot3.PNG diff --git a/Schaum's_Outlines_-_Programming_with_C++/README.txt b/Schaum's_Outlines_-_Programming_with_C++/README.txt new file mode 100644 index 00000000..7c6dc038 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/README.txt @@ -0,0 +1,10 @@ +Contributed By: Hiren Shah +Course: mca +College/Institute/Organization: Financial Technologies +Department/Designation: Software Tester +Book Title: Schaum's Outlines - Programming with C++ +Author: John R. Hubbard +Publisher: McGraw Hill, USA +Year of publication: 2000 +Isbn: 0071353461 +Edition: 2nd
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch1.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch1.ipynb new file mode 100644 index 00000000..f6589ebe --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch1.ipynb @@ -0,0 +1,250 @@ +{ + "metadata": { + "name": "ch1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "\"\"\"\nExample 1.1\n\"\"\"\nprint \"Hello, World!\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nexample 1.2\nprints 'hello world' with comments.\n'''\n\n# prints \"Hello, World!\":\nprint \"Hello, World!\\n\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nexample 1.3\nanother version of hello world.\n'''\n\n# prints \"Hello, World!\":\nprint \"Hel\" + \"lo, Wo\" + \"rld!\" \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "\"\"\"\nexample 1.4\nanother version of hello world.\n\"\"\"\n\n# prints \"Hello, World!\":\nprint \"Hello, W\" + 'o' + \"rld\" + '!' ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Hello, World!\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.5 Inserting Numeric Literals into the Standard Output Stream\n'''\n\n# prints \"The Millennium ends Dec 31 2000.\":\nprint \"The Millennium ends Dec %d %d \" %(31,2000)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The Millennium ends Dec 31 2000 \n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.6 Using Integer Variables\nIn this example, the integer 44 is assigned to the variable m, and the value of the expression m + 33\nis assigned to the variable n:\n'''\n# prints \"m = 44 and n = 77\":\n\nm = 44 # assigns the value 44 to the variable m\nprint \"m = %d \" % m,\nn = m + 33 # assigns the value 77 to the variable n\nprint \"and n = %d \" % n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 44 and n = 77 \n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.7 A Program's Tokens\n'''\n\n# prints \"n = 44:\nn=44\nprint \"n = %d\" % n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.8 \n'''\n\n# Python does not have semicolons so wont give any errors.\nn=44\nprint \"n = %d\" % n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.9 Initializing Variables\nThis program contains one variable that is not initialized and one that is initialized.\nNote : This program would give you differ output then c gives.\n'''\n\n# prints \"m = ?? and n = 44\":\nm = 0 #In python we do not have declaration of variables, we just initialize it and use it.\nn=44\nprint \"m = %d and n = %d\" %(m,n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 0 and n = 44\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.10 The const Specifier\nThis program illustrates constant definitions:\n'''\n\n# defines constants; has no output:\nBEEP = '\\b'\nMAXINT = 2147483647\nN = MAXINT/2\nKM_PER_MI = 1.60934\nPI = 3.14159265358979323846\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 1.11 Using the Input Operator\n'''\n\n# tests the input of integers, floats, and characters:\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\nprint \"m = %d , n = %d \" %(m,n)\n\nprint \"Enter three decimal numbers: \"\nx = float(raw_input())\ny = float(raw_input())\nz = float(raw_input())\n\nprint \"x = %f , y = %f , z = %f\" %(x,y,z)\n\nprint \"Enter four characters: \";\nc1 = raw_input()\nc2 = raw_input()\nc3 = raw_input()\nc4 = raw_input()\nprint \"c1 = \" + c1 + \", c2 = \" + c2 + \", c3 = \" + c3 + \", c4 = \" + c4 ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 22 , n = 44 \nEnter three decimal numbers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2.2\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4.4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6.6\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 2.200000 , y = 4.400000 , z = 6.600000\nEnter four characters: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "A\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "B\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "C\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "D\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "c1 = A, c2 = B, c3 = C, c4 = D\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch10.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch10.ipynb new file mode 100644 index 00000000..4b6ac8a8 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch10.ipynb @@ -0,0 +1,286 @@ +{ + "metadata": { + "name": "ch10" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.1 Implementing the Ratio Class\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n \nx = Ratio()\nx.assign(22,7)\nprint \"x = \",\nx.print_()\nprint \" = \" , x.convert() \nx.invert()\nprint \"1/x = \",\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 22 / 7 = 3.14285714286\n1/x = 7 / 22\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.2 Class example\n'''\nclass Ratio:\n def __init__(self):\n self.num = 0\n self.den = 0\n def assign(self,n,d):\n self.num = n\n self.den = d\n def convert(self):\n return float(self.num)/self.den\n def invert(self):\n self.num,self.den = self.den,self.num\n def print_(self):\n print self.num , '/' , self.den ,\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.3\nA Constructor Function for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n,d):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio(-1,3)\ny = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \" and y = \",\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = -1 / 3 and y = 22 / 7\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.4 Constructors to the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)\nprint \"x = \",\nx.print_()\nprint \"\\ny = \",\ny.print_()\nprint \"\\nz = \",\nz.print_()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 0 / 1 \ny = 4 / 1 \nz = 22 / 7\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.5\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if n==None:\n self.num = 0\n self.den = 1\n elif d==None:\n self.num = n\n self.den = 1\n else:\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.6 Using Default Parameter Values in the Ratio Class Constructor\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio()\ny = Ratio(4)\nz = Ratio(22,7)", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.7 Access Functions in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n\nx = Ratio(22,7)\nprint x.numerator() , '/' , x.denominator() ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22 / 7\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,-360)\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "-5 / 18\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(100,360)\ny = Ratio(x)\nprint \"x = \",\nx.print_()\nprint \"y = \",\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 5 / 18 y = 5 / 18\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.10 Tracing Calls to the Copy Constructor\n'''\n'''\nEXAMPLE 10.9 Adding a Copy Constructor to the Ratio Class\n'''\n'''\nEXAMPLE 10.8\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\nclass Ratio:\n def __init__(self,n=0,d=None):\n if d == None:\n print \"COPY CONSTRUCTOR CALLED\"\n self.num = n.num\n self.den = n.den\n else: \n self.num = n\n self.den = d\n self.reduce()\n def numerator(self):\n return self.num\n def denominator(self):\n return self.den\n def print_(self):\n print self.num , '/' , self.den ,\n\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\ndef f(r):\n s = Ratio(r)\n\nx = Ratio(22,7)\ny = Ratio(x) #calls the copy constructor, copying x to y\nf(y)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "COPY CONSTRUCTOR CALLED\nCOPY CONSTRUCTOR CALLED\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.11 Including a Destructor in the Ratio Class\nNote : Python objects die when program gets exit.\n'''\n\nclass Ratio:\n def __init__(self):\n print \"OBJECT IS BORN.\"\n def __del__(self):\n print \"OBJECT DIES.\"\n\nx = Ratio()\nprint \"Now x is alive.\"\nprint \"Now between blocks.\"\ny = Ratio()\nprint \"Now y is alive.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "OBJECT IS BORN.\nNow x is alive.\nNow between blocks.\nOBJECT IS BORN.\nNow y is alive.\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.12\n'''\nclass X:\n def __init(self):\n data = 0\n\np = X()\np.data = 22\nprint \"p.data = \" , p.data , \" = \" , p.data\np.data = 44\nprint \" p.data = \" , p.data , \" = \" , p.data ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "p.data = 22 = 22\n p.data = 44 = 44\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.13 A Node Class for Linked Lists\n'''\nclass Node:\n def __init__(self,d,q=None):\n self.data = d\n self.next = q\n\nn = int(raw_input())\nq = Node(n)\nwhile True:\n n = int(raw_input())\n if n<=0:\n break\n p = Node(n, q)\n q = p\nk = p\nwhile k != None:\n print k.data , '->' , \n k = k.next\nprint '*'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "77 -> 66 -> 55 -> 44 -> 33 -> 22 -> *\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.14 \nPython does not support static data type.\nPython automatically handles local variable so we need not to delete it.\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , count , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , count , 'widgets' \nprint \"Now there are \" , count , 'widgets'\ny = Widget()\nprint \"Now there are \" , count , 'widgets'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "OBJECT DIES.\nNow there are 2 widgets\nOBJECT DIES.\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.15 \n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 10.16\n'''\ncount = 0\nclass Widget:\n def __init__(self):\n global count\n count += 1\n def numWidgets(self):\n global count\n return count\n \nw = Widget()\nx = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'\nif True:\n w = Widget()\n x = Widget()\n y = Widget()\n z = Widget()\n print \"Now there are\" , w.numWidgets() , 'widgets' \nprint \"Now there are \" , w.numWidgets() , 'widgets'\ny = Widget()\nprint \"Now there are \" , w.numWidgets() , 'widgets'", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Now there are 2 widgets\nNow there are 6 widgets\nNow there are 6 widgets\nNow there are 7 widgets\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch11.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch11.ipynb new file mode 100644 index 00000000..3ddc5480 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch11.ipynb @@ -0,0 +1,211 @@ +{ + "metadata": { + "name": "ch11" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.1\nAdding an Assignment Operator to the Ratio Class\nHere is a class interface for the Ratio class, showing the default constructor, the copy constructor,\nand the assignment operator:\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n def equals(self):\n return self # retuns calling object.", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.2 The Preferred Prototype for an Overloaded Assignment Operator\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n pass\n \n def equals(self):\n pass\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.3 Implementation of the Assignment Operator for the Ratio Class\n'''\nclass Ratio:\n def __init__(self,n=None,d=None):\n if d==None:\n self.num = n.num\n self.den = n.den\n elif n==None:\n self.num = 0\n self.den = 1\n else:\n self.num = n\n self.den = d\n \n\nz = Ratio(22,7)\ny = z\nx = z\n\nx = Ratio(22,7)\ny = Ratio(x)\nz = x\nw = x", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.4\nmultiplication operator overload\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __mul__(self,y):\n pass\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.5 The Ratio Class with Assignment and Multiplication Operators\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\nx = Ratio(22,7)\ny = Ratio(-3,8)\nz = x\nz.print_()\nx = y*z\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22 / 7\n-33 / 28\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.6 The Ratio Class with an Overloaded *= Operator\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\n\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def print_(self):\n print self.num , '/', self.den", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.7 Overloading the Equality Operator == in the Ratio Class\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n self.reduce()\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.8 \nPython does not use << operator for printing. So here we are just declaring function name as print_.\n'''\n\nclass Ratio:\n def __init__(self,n=None,d=None):\n self.num = n\n self.den = d\n def __imul__(self,y):\n self.num = self.num * y.num\n self.den = self.den * y.den\n def __eq__(self,y):\n return (x.num * y.den == y.num * x.den)\n def print_(self):\n print self.num , '/', self.den\n\n\nx = Ratio(22,7)\ny = Ratio(-3,8)\nx.print_() , y.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22 / 7\n-3 / 8\n" + }, + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 8, + "text": "(None, None)" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.9 \nPython does not have >> for input. so we will use input function.\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\n\nx = Ratio()\ny = Ratio()\nx.input()\ny.input()\nx.print_()\ny.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Numerator : -10\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Denominator : -24\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Numerator : 36\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Denominator : -20\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "5 / 12\n-9 / 5\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.10\nAdding a Conversion Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def input(self):\n self.num = int(raw_input('Numerator : '))\n self.den = int(raw_input('Denominator : '))\n self.reduce()\n def __float__(self):\n return float(self.num)/self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 15.py\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(-5,8)\nprint \"x = \" , \nx.print_() \nprint \", float(x) = \" , float(x) \nP = Ratio(22,7)\nPI = float(P)\nprint \"P = \" ,\nP.print_() \nprint \", PI = \" , PI\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = -5 / 8\n, float(x) = -0.625\nP = 22 / 7\n, PI = 3.14285714286\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.11 Adding a Pre-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nx += 1\ny = x\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "y = 29 / 7\n, x = 29 / 7\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.12 Adding a Post-Increment Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __iadd__(self,n):\n self.num += self.den\n return self\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\ny = Ratio(x.num,x.den)\nx += 1\nprint \"y = \" ,\ny.print_()\nprint \", x = \",\nx.print_()\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "y = 22 / 7\n, x = 29 / 7\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 11.13\nAdding a Subscript Operator to the Ratio Class\n'''\ndef gcd(m,n):\n # returns the greatest common divisor of m and n:\n if (m<n):\n m,n = n,m\n while (n>0):\n r = m % n\n m = n\n n = r\n return m\nclass Ratio:\n def __init__(self,n=0,d=1):\n self.num = n\n self.den = d\n self.reduce()\n def __mul__(self,y):\n z = Ratio(self.num * y.num, self.den * y.den)\n return z\n def print_(self):\n print self.num , '/', self.den\n \n def __getitem__(self,k):\n if k == 1:\n return self.num\n else:\n return self.den\n def reduce(self):\n # enforce invariant(den > 0):\n if (self.num == 0 or self.den == 0):\n self.num = 0\n self.den = 1\n return\n if (self.den < 0):\n self.den *= -1\n self.num *= -1\n # enforce invariant(gcd(num,den) == 1):\n if (self.den == 1):\n return\n # it's already reduced\n sgn = 0\n if self.num < 0:\n sgn = -1\n else:\n sgn = 1\n g = gcd(sgn*self.num,self.den)\n self.num /= g\n self.den /= g\n\nx = Ratio(22,7)\nprint \"x = \" ,\nx.print_()\nprint \"x[1] = \" , x[1] , \", x[2] = \" , x[2]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 22 / 7\nx[1] = 22 , x[2] = 7\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch12.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch12.ipynb new file mode 100644 index 00000000..304a3ccb --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch12.ipynb @@ -0,0 +1,228 @@ +{ + "metadata": { + "name": "ch12" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.1 A Person Class\nHere is a simple definition for a class to represent people.\n'''\nclass Person:\n def __init__(self,n=\"\",nat=\"U.S.A.\",s=1):\n self.name = n\n self.nationality = nat\n self.sex = s\n\n def printName(self):\n print self.name,\n \n def printNationality(self):\n print self.nationality,\n\ncreator = Person(\"Bjarne Stroustrup\", \"Denmark\")\nprint \"The creator of C++ was \" ,\ncreator.printName() \nprint \", who was born in \" ,\ncreator.printNationality() \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The creator of C++ was Bjarne Stroustrup , who was born in Denmark\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.2 A Date Class\n'''\n\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\npeace = Date(11,11,1918)\nprint \"World War I ended on \" ,\npeace.print_()\npeace.setDate(8,14,1945)\nprint \"World War II ended on \" ,\npeace.print_()\nprint \"Enter month, day, and year: \"\ndate = Date()\ndate.input()\nprint \"The date is \" , \ndate.print_()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "World War I ended on November 11 , 1918\nWorld War II ended on August 14 , 1945\nEnter month, day, and year: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1976\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The date is July 4 , 1976\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.3 Composing the Date Class with the Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nauthor = Person(\"Thomas Jefferson\", 1)\nauthor.setDOB(4,13,1743)\nauthor.setDOD(7,4,1826)\nprint \"The author of the Declaration of Independence was \",\nauthor.printName()\nprint \".\\nHe was born on \",\nauthor.printDOB()\nprint \" and died on \",\nauthor.printDOD()\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The author of the Declaration of Independence was Thomas Jefferson .\nHe was born on April 13 , 1743\n and died on July 4 , 1826\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.4 Deriving a Student Class from the Person Class\nStudents are people. So it is natural to use the Person class to derive a Student class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.printName()\nprint \"\\n\\t Born: \" ,\nx.printDOB()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\tMatriculated: August 29 , 1995\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.5 The Person Class\n'''\nclass Date:\n def __init__(self,m=0,d=0,y=0):\n self.month = m\n self.day = d\n self.year = y\n \n def setDate(self,m,d,y):\n self.month = m\n self.day = d\n self.year = y\n # Python doesn't have >> operator for input so we are just using input function\n def input(self):\n self.month = int(raw_input()) \n self.day = int(raw_input())\n self.year = int(raw_input()) \n \n # Python doesn't have << operator for output so we are just using print function\n def print_(self):\n monthName = [\"\", \"January\",\"February\",\"March\", \"April\", \"May\", \"June\",\\\n \"July\", \"August\",\"September\", \"October\", \"November\",\\\n \"December\"]\n print monthName[self.month] , self.day , \",\" , self.year\n\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n\nclass Student(Person):\n def __init__(self,n,s=0,i=\"\"):\n Person.__init__(self,n,s)\n self.id = i\n self.credits = 0\n self.gpa = 0\n self.dom = Date()\n\n def setDOM(self,m,d,y):\n self.dom.setDate(m, d, y)\n def printDOM(self):\n self.dom.print_()\n def printSex(self):\n if self.sex == 1:\n print \"male\"\n else:\n print 'female'\n\nx = Student(\"Ann Jones\", 0, \"219360061\")\nx.setDOB(5, 13, 1977)\nx.setDOM(8, 29, 1995)\nx.setDOD(7,4,1826)\nx.printName()\nprint \"\\n\\t Born: \" , \nx.printDOB()\nprint \"\\n\\t Sex: \" ,\nx.printSex()\nprint \"\\n\\tMatriculated: \",\nx.printDOM()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Ann Jones \n\t Born: May 13 , 1977\n\n\t Sex: female\n\n\tMatriculated: August 29 , 1995\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.6 Dominating a Data Member and Overriding a Member Function\nHere are two classes, X and Y, with Y inheriting from X.\n'''\nclass X:\n def __init__(self):\n self.a = 0\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def __init__(self):\n self.a = 0\n def f(self):\n print \"Y::f() executing\"\nx = X()\nx.a = 22\nx.f()\nprint \"x.a = \" , x.a\ny = Y()\ny.a = 44\n# assigns 44 to the a defined in Y\ny._X__a = 66\n# assigns 66 to the a defined in X\ny.f()\n# invokes the f() defined in Y\nX.f(x)\n# invokes the f() defined in X\nprint \"y.a = \" , y.a \nprint \"y._X__a = \" , y._X__a \nz = y\nprint \"z.a = \" , z._X__a \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::f() executing\nx.a = 22\nY::f() executing\nX::f() executing\ny.a = 44\ny._X__a = 66\nz.a = 66\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.7 Parent Constructors and Destructors\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass X:\n def __init__(self):\n print \"X::X() constructor executing \"\n def __del__(self):\n print \"X::X() destructor executing \"\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n print \"Y::Y() constructor executing \"\n def __del__(self):\n print \"Y::Y() destructor executing \"\n\nclass Z(Y):\n def __init__(self,i):\n Y.__init__(self)\n print \"Z::Z(\" , i , \") constructor executing \"\n def __del__(self):\n print \"Z::Z() destructor executing \"\n \n\nZ = Z(44)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::X() constructor executing \nY::Y() constructor executing \nZ::Z( 44 ) constructor executing \n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.8 Parent Constructors and Destructors\nHere is a demo program that uses a base class Person and a derived class Student:\nNote : Python destuctor is called when program goes exit. So output may be differ than c.\n'''\nclass Person:\n def __init__(self,s):\n self.name = s\n def __del__(self):\n pass\n\nclass Student(Person):\n def __init__(self,s,m):\n Person.__init__(self,s)\n self.major = m\n def __del__(self):\n pass\nx = Person(\"Bob\")\ny = Student(\"Sarah\", \"Biology\")\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.9\n'''\nclass Person:\n def __init__(self,n=\"\",s=0,nat=\"U.S.A.\"):\n self.name = n\n self.nationality = nat\n self.sex = s\n self.dob = Date()\n self.dod = Date()\n def setDOB(self,m,d,y):\n self.dob.setDate(m,d,y)\n def setDOD(self,m,d,y):\n self.dod.setDate(m,d,y)\n def printName(self):\n print self.name,\n def printNationality(self):\n print self.nationality,\n def printDOB(self):\n self.dob.print_()\n def printDOD(self):\n self.dod.print_()\n def setHSgraduate(self,g):\n self.hs = g\n def isHSgraduate(self):\n return hs\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.10 Using virtual Functions\nThis demo program declares p to be a pointer to objects of the base class X. First it assigns p to\npoint to an instance x of class X. Then it assigns p to point to an instance y of the derived class Y.\nNote : By default all methods in python are virtual. so output would be differ than c.\n'''\nclass X:\n def f(self):\n print \"X::f() executing\"\nclass Y(X):\n def f(self):\n print \"Y::f() executing\"\nx = X()\ny = Y()\np = x\np.f()\np = y\np.f()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X::f() executing\nY::f() executing\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.11 Polymorphism through virtual Functions\nHere is a Person class with a Student subclass and a Professor subclass:\n'''\nclass Person:\n def __init__(self,n):\n self.name = n\n def print_(self):\n print 'My name is' , self.name\n\nclass Student(Person):\n def __init__(self,s,g):\n Person.__init__(self,s)\n self.gpa = g\n def print_(self):\n print 'My name is ',self.name, ' and my G.P.A. is', self.gpa\n\nclass Professor(Person):\n def __init__(self,s,n):\n Person.__init__(self,s)\n self.publs = n\n def print_(self):\n print 'My name is ', self.name,' and i have ' , self.publs,' publications.'\n\nx = Person(\"Bob\")\np = x\np.print_()\ny = Student(\"Tom\", 3.47)\np = y\np.print_()\nz = Professor(\"Ann\", 7)\np = z\np.print_()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "My name is Bob\nMy name is Tom and my G.P.A. is 3.47\nMy name is Ann and i have 7 publications.\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.12 Memory Leaks\nThis program is similar to Example 12.6:\n'''\nclass X:\n def __init__(self):\n self.p = [0,0]\n print 'X().',\n def __del__(self):\n print '~X().'\n\nclass Y(X):\n def __init__(self):\n X.__init__(self)\n self.q = []\n for i in range(1023):\n self.q.append(0)\n print 'Y() : Y::q = ', hex(id(self.q)) ,'.',\n def __del__(self):\n print '~Y().'\n\nfor i in range(8):\n r = Y()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "X(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\nX(). Y() : Y::q = 0x90d91cc . ~Y().\nX(). Y() : Y::q = 0x90c944c . ~Y().\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 12.13 A Hierarchy of Media Classes\nHere is a hierarchy of classes to represent various media objects:\nMedia Audio Book Periodical CD Tape Record Magazine Newspaper Journal Newsletter\nThe primary ABC is the Media class:\n'''\nclass Media:\n def __init__(self):\n self.title = ''\n def print_(self):\n pass\n def id(self):\n pass\n\nclass Book(Media):\n def __init__(self,a='',t=\"\",p=\"\",i=\"\"):\n self.author = a\n self.publisher = p\n self.isbn = i\n self.title = t\n def print_(self):\n print self.title , \" by \" , self.author\n def id(self):\n return self.isbn\n\nclass CD(Media):\n def __init__(self,t=\"\",c=\"\",m=\"\",n=\"\"):\n self.composer = c\n self.make = m\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \", \" , self.composer\n def id(self):\n s = str(self.make) + ' ' + str(self.number)\n return s\n\nclass Magazine(Media):\n def __init__(self,t=\"\",i=\"\",v=0, n=0):\n self.issn = i\n self.volume = v\n self.number = n\n self.title = t\n def print_(self):\n print self.title , \" Magazine, Vol. \", self.volume , \", No.\" , self.number\n def id(self):\n return self.issn\n\nbook = Book(\"Bjarne Stroustrup\", \"The C++ Programming Language\",\"Addison-Wesley\", \"0-201-53992-6\")\nmagazine = Magazine(\"TIME\", \"0040-781X\", 145, 23)\ncd = CD(\"BACH CANTATAS\", \"Johann Sebastian Bach\",\"ARCHIV\", \"D120541\")\nbook.print_()\nprint \"\\tid: \" , book.id() \nmagazine.print_()\nprint \"\\tid: \" , magazine.id() \ncd.print_()\nprint \"\\tid: \" , cd.id()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The C++ Programming Language by Bjarne Stroustrup\n\tid: 0-201-53992-6\nTIME Magazine, Vol. 145 , No. 23\n\tid: 0040-781X\nBACH CANTATAS , Johann Sebastian Bach\n\tid: ARCHIV D120541\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb new file mode 100644 index 00000000..827142fe --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch13.ipynb @@ -0,0 +1,136 @@ +{ + "metadata": { + "name": "ch13" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.1 The swap Function Template\n'''\ndef swap(x,y):\n x[0],y[0] = y[0],x[0]\n\nm = [22]\nn = [66]\nswap(m, n)\ns1 = [\"John Adams\"]\ns2 = [\"James Madison\"]\nswap(s1, s2)\nx = [22/7]\ny = [-3]\nswap(x, y)\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.2 The Bubble Sort Template\n'''\n\ndef sort(v,n):\n for i in range(1,n):\n for j in range(n-i):\n if v[j] > v[j+1]:\n v[j],v[j+1] = v[j+1],v[j]\n\ndef print_( v,n):\n for i in range(n):\n print v[i],\n print \"\"\n \na = [55, 33, 88, 11, 44, 99, 77, 22, 66]\nprint_(a,9);\nsort(a,9)\nprint_(a,9)\ns = [\"Tom\", \"Hal\", \"Dan\", \"Bob\", \"Sue\", \"Ann\", \"Gus\"]\nprint_(s,7)\nsort(s,7)\nprint_(s,7)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "55 33 88 11 44 99 77 22 66 \n11 22 33 44 55 66 77 88 99 \nTom Hal Dan Bob Sue Ann Gus \nAnn Bob Dan Gus Hal Sue Tom \n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.3 A Stack Class Template\n'''\nclass Stack:\n def __init__(self,s=100):\n self.size = s\n self.top = -1\n self.data = []\n def push(self,x):\n self.data.append( x)\n self.top += 1\n def pop(self):\n d = self.data[self.top]\n self.data.pop(self.top)\n self.top -= 1\n return d \n def isEmpty(self):\n return self.top == -1\n def isFull(self):\n return self.top==self.size-1\n \nintStack1 = Stack(5)\nintStack2 = Stack(10)\ncharStack = Stack(8)\nintStack1.push(77)\ncharStack.push('A')\nintStack2.push(22)\ncharStack.push('E')\ncharStack.push('K')\nintStack2.push(44)\nprint intStack2.pop() \nprint intStack2.pop() \nif (intStack2.isEmpty()):\n print \"intStack2 is empty.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "44\n22\nintStack2 is empty.\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.4 A Vector Class Template\n= operator does work in python by defaultly.\n'''\nclass Vector:\n def __init__(self,n=None):\n if type(n) == int :\n self.size = n\n self.data = []\n else:\n self.size = 8\n self.data = []\n for i in range(self.size):\n self.data.append(0)\nv = Vector()\nv.data[5] = 127\nw = v\nx = Vector(3)\nprint w.size\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.5 A Subclass Template for Vectors\n'''\nclass Vector:\n def __init__(self,n=None):\n if type(n) == int :\n self.size = n\n self.data = []\n else:\n self.size = 8\n self.data = []\n for i in range(self.size):\n self.data.append(0)\n\nclass Array(Vector):\n def __init__(self,i,j):\n Vector.__init__(self,j-i+1)\n self.i0= i\n def __setitem__(self,k,v):\n self.data[k-self.i0] = v\n def __getitem__(self,i):\n return self.data[self.i0-i]\n def firstSubscript(self):\n return self.i0\n def lastSubscript(self):\n return self.i0+self.size-1\n\nx = Array(1,3)\nx.data[0] = 3.14159\nx.data[1] = 0.08516\nx.data[2] = 5041.92\nprint \"x.size() = \" , x.size \nprint \"x.firstSubscript() = \" , x.firstSubscript() \nprint \"x.lastSubscript() = \" , x.lastSubscript()\nfor i in range(0,3):\n print \"x[\" , i + 1 , \"] = \" , x.data[i]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x.size() = 3\nx.firstSubscript() = 1\nx.lastSubscript() = 3\nx[ 1 ] = 3.14159\nx[ 2 ] = 0.08516\nx[ 3 ] = 5041.92\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.6 A Matrix Class Template\n'''\n\nclass Matrix:\n def __init__(self,r=1,c=1):\n self.rows = r\n self.columns = c\n self.vector = []\n for i in range(r):\n a = []\n for j in range(c):\n a.append(0)\n self.vector.append(a)\n\na = Matrix(2,3)\na.vector[0][0] = 0.0\na.vector[0][1] = 0.1\na.vector[0][2] = 0.2\na.vector[1][0] = 1.0\na.vector[1][1] = 1.1\na.vector[1][2] = 1.2\n\nprint \"The matrix a has \" , a.rows , \" rows and \", a.columns , \" columns:\"\nfor i in range(2):\n for j in range(3):\n print a.vector[i][j] ,\n print \"\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The matrix a has 2 rows and 3 columns:\n0.0 0.1 0.2 \n1.0 1.1 1.2 \n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.7 A List Class Template\nA list consists of a linked sequence of nodes. Each node contains one data item and a link to the next\nnode. So we begin by defining a ListNode class template:\n'''\n\nfriends = []\n\n\nfriends.insert(0,\"Bowen, Van\")\nfriends.insert(0,\"Dixon, Tom\")\nfriends.insert(0,\"Mason, Joe\")\nfriends.insert(0,\"White, Ann\")\n\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\nfriends.remove('White, Ann')\nprint \"Removed: \" , 'White, Ann'\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "White, Ann -> Mason, Joe -> Dixon, Tom -> Bowen, Van -> *\nRemoved: White, Ann\nMason, Joe -> Dixon, Tom -> Bowen, Van -> *\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 13.8\n'''\nfriends = []\nfriends.append(\"Bowen, Van\")\nfriends.append(\"Dixon, Tom\")\nfriends.append(\"Mason, Joe\")\nfriends.append(\"White, Ann\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.remove(\"Mason, Joe\")\nfriends[1] = \"Davis, Jim\"\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfriends.insert(2,\"Morse, Sam\")\nfor i in range(len(friends)):\n print friends[i], '->' ,\nprint '*'\n\nfor i in range(len(friends)):\n print \"[\" ,friends[i] , \"]\" , '->' ,\nprint '*'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Bowen, Van -> Dixon, Tom -> Mason, Joe -> White, Ann -> *\nBowen, Van -> Davis, Jim -> White, Ann -> *\nBowen, Van -> Davis, Jim -> Morse, Sam -> White, Ann -> *\n[ Bowen, Van ] -> [ Davis, Jim ] -> [ Morse, Sam ] -> [ White, Ann ] -> *\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb new file mode 100644 index 00000000..1618010d --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch14.ipynb @@ -0,0 +1,172 @@ +{ + "metadata": { + "name": "ch14" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.1 Using a vector of strings\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.2 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.3 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in range(len(v)):\n print v[i]\n\nv = []\nload(v)\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.4 Using the Generic sort() Algorithm\n'''\n\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.5 Using the Assignment Operator to Duplicate a vector\n'''\n#This program demonstrates that one vector can be assigned to another.\n\n\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nw = v\nprint_(v)\nprint_(w)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\nJapan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.6 \n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\nprint \"v.front() = \" + v[0]\nprint \"v.back() = \" + v.pop(-1) \nprint \"v.back() = \" + v.pop(-1) \nprint \"v.back() = \" + v[-1] \nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nv.front() = Chile\nv.back() = Zaire\nv.back() = Spain\nv.back() = Nepal\nChile\nEgypt\nItaly\nJapan\nKenya\nNepal\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.7 Using the erase() Function\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nv.sort()\nprint_(v)\nv.pop(2) # removes Italy\nv.pop(-2) # removes Spain\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nChile\nEgypt\nJapan\nKenya\nNepal\nZaire\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.8 Using the insert() Function\n'''\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n\ndef print_(v):\n for i in v:\n print i\nv = []\nload(v)\nv.sort()\nprint_(v)\nr = []\nfor i in range(2,len(v)-2):\n r.append(v[i]) #removes the segment Italy..Nepal\n \nfor i in r:\n v.remove(i)\nprint_(v)\nv.insert(2,\"India\")\nprint_(v)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Chile\nEgypt\nItaly\nJapan\nKenya\nNepal\nSpain\nZaire\nChile\nEgypt\nSpain\nZaire\nChile\nEgypt\nIndia\nSpain\nZaire\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.9 Using the find() Function\n'''\n\ndef load(v):\n v.append(\"Japan\")\n v.append(\"Italy\")\n v.append(\"Spain\")\n v.append(\"Egypt\")\n v.append(\"Chile\")\n v.append(\"Zaire\")\n v.append(\"Nepal\")\n v.append(\"Kenya\")\n v.append(\"India\")\n v.append(\"China\")\n v.append(\"Malta\")\n v.append(\"Syria\")\n\ndef print_(v):\n for i in v:\n print i\n\nv = []\nload(v)\nprint_(v)\negypt = v.index('Egypt')\nmalta = v.index('Malta')\nw = v[egypt:malta+1]\nw.sort()\nprint_(w)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Japan\nItaly\nSpain\nEgypt\nChile\nZaire\nNepal\nKenya\nIndia\nChina\nMalta\nSyria\nChile\nChina\nEgypt\nIndia\nKenya\nMalta\nNepal\nZaire\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 14.10\n'''\ndef copy(v,x,n):\n for i in x:\n v.append(i)\n\ndef projection(v,b):\n v_size = len(v)\n w = []\n for i in range(0,v_size):\n if b[i]:\n w.append(v[i])\n return w\n\ndef print_(v):\n for i in v:\n print i,\n print ''\n\nx = [ 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 ]\nv = []\ncopy(v, x, 8)\ny = [ False, True, False, True, True, True, False, True ]\nb = []\ncopy(b, y, 8)\nw = projection(v, b)\nprint_(v)\nprint_(w)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n33.3 55.5 66.6 77.7 99.9 \n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb new file mode 100644 index 00000000..606044d0 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch2.ipynb @@ -0,0 +1,310 @@ +{ + "metadata": { + "name": "CH2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.1 Boolean Variables\n'''\n\n# prints the value of a boolean variable:\nflag=False\nprint \"flag = %r\" % flag\nflag = True\nprint \"flag = %r\" % flag", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "flag = False\nflag = True\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.2 Character Variables\n'''\n\n# prints the character and its internally stored\nc='A'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='t'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='\\t' # the tab character\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\nc='!'\nprint \"c = \" + c + \", int(c) = %d\" % ord(c)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "c = A, int(c) = 65\nc = t, int(c) = 116\nc = \t, int(c) = 9\nc = !, int(c) = 33\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.3 Integer Type Ranges\nThis program prints the numeric ranges of the Python\n'''\nimport sys\n# defines the constants SHRT_MIN, etc.\nprint 'maximum limit int : ',\nprint sys.maxint\nprint 'float info'\nprint sys.float_info", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "maximum limit int : 2147483647\nfloat info\nsys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.4 Integer Arithmetic\nThis example illustrates how the arithmetic operators work.\n'''\n\n# tests operators +, -, *, /, and %:\nm=54\nn=20\nprint \"m = %d and n = %d\" %(m,n)\nprint \"m+n = %d\" % (m+n) # 54+20 = 74\nprint \"m-n = %d\" % (m-n) # 54-20 = 34\nprint \"m*n = %d\" % (m*n)# 54*20 = 1080\nprint \"m/n = %d\" % (m/n) # 54/20 = 2\nprint \"m modulo by n = %d\" % (m%n) # 54%20 = 14", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 54 and n = 20\nm+n = 74\nm-n = 34\nm*n = 1080\nm/n = 2\nm modulo by n = 14\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.5 Applying the Pre-increment and Post-increment Operators\n'''\n\n# shows the difference between m++ and ++m:\nm = 44\nm += 1\nn = m\nprint \"m = %d , n = %d\" %(m,n)\nm = 44\nn = m # the post-increment operator is applied to m\nm += 1\nprint \"m = %d , n = %d\" %(m,n)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "m = 45 , n = 45\nm = 45 , n = 44\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.6 Applying Composite Arithmetic Assignment Operators\n'''\n\n# tests arithmetic assignment operators:\nn=22\nprint \"n = %d\" % n\nn += 9 # adds 9 to n\nprint \"After n += 9, n = %d\" % n\nn -= 5 # subtracts 5 from n\nprint \"After n -= 5, n = %d\" % n\nn *= 2 # multiplies n by 3\nprint \"After n *= 2, n = %d\" % n \nn /= 3 # divides n by 9\nprint \"After n /= 3, n = %d\" % n \nn %= 7 # reduces n to the remainder from dividing by 4\nprint 'After n modulo by 7 n = %d' %n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 22\nAfter n += 9, n = 31\nAfter n -= 5, n = 26\nAfter n *= 2, n = 52\nAfter n /= 3, n = 17\nAfter n modulo by 7 n = 3\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.7 Floating-Point Arithmetic\nThis program is nearly the same as the one in Example 2.4. The important difference is that these\nvariables are declared to have the floating-point type double instead of the integer type int.\n'''\n\n# tests the floating-point operators +, -, *, and /:\nx=54.0\ny=20.0\nprint \"x = %f and y = %f\" %(x,y)\nprint \"x+y = %f\" %(x+y) # 54.0+20.0 = 74.0\nprint \"x-y = %f\" % (x-y) # 54.0-20.0 = 34.0\nprint \"x*y = %f\" %( x*y) # 54.0*20.0 = 1080.0\nprint \"x/y = %f\" % (x/y) # 54.0/20.0 = 2.7\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 54.000000 and y = 20.000000\nx+y = 74.000000\nx-y = 34.000000\nx*y = 1080.000000\nx/y = 2.700000\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.8 Using the sizeof Operator\nThis program tells you how much space each of the undamental types uses:\nNote : Python has few number of data types so output would be differ.\n'''\nimport sys\n# prints the storage sizes of the fundamental types:\nprint \"Number of bytes used:\\n\"\n\nprint \" char: %d \" % sys.getsizeof('a')\nprint \" int : %d \" % sys.getsizeof(int(1))\nprint \" string : %d \" % sys.getsizeof(str('hellololdei'))\nprint \"float : %d\" % sys.getsizeof(float(1.1))", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Number of bytes used:\n\n char: 22 \n int : 12 \n string : 32 \nfloat : 16\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.9 Reading from the <cfloat> Header File\nThis program tells you the precision and magnitude range that the float type has on your system:\n'''\nimport sys\n# prints the storage sizes of the fundamental types:\nfbits = 8*sys.getsizeof(float(123))\n\n# each byte contains 8 bits\nprint \"float uses : %d bits:\\n\\t\" % fbits \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "float uses : 128 bits:\n\t\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nThis program casts a double value into int value:\n'''\n\n# casts a double value as an int:\nv = 1234.56789\nn = int(v);\nprint \"v = %f, n = %d\" %(v,n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "v = 1234.567890, n = 1234\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.11 Promotion of Types\nThis program promotes a char to a short to an int to a float to a double:\nNote : Python is type independent. So would give output differ.\n'''\n# prints promoted vales of 65 from char to double:\nc='A'\nprint \"char c = \" + c\nk=c;\nprint \"k = \" + k \nm=k;\nprint \"m = \" + m \nn=m\nprint \"n = \" + n\nx=m\nprint \"x = \" + x \ny=x\nprint \"y = \" + y", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "char c = A\nk = A\nm = A\nn = A\nx = A\ny = A\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.12 Integer Overflow\nThis program repeatedly multiplies n by 1000 until it overflows.\n'''\n# prints n until it overflows:\nn=1000\nprint \"n = %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n = %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n = %d\" % n\nn *= 1000 # multiplies n by 1000\nprint \"n = %d\" % n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 1000\nn = 1000000\nn = 1000000000\nn = 1000000000000\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.13 Floating-point Overflow\nThis program is similar to the one in Example 2.12. It repeatedly squares x until it overflows.\n'''\n\n# prints x until it overflows:\nx=1000.0\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\nx *= x # multiplies n by itself; i.e., it squares x\nprint \"x = %f\" % x\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 1000.000000\nx = 1000000.000000\nx = 1000000000000.000000\nx = 999999999999999983222784.000000\nx = 1000000000000000043845843045076197354634047651840.000000\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.14 Round-off Error\nThis program does some simple arithmetic to illustrate roundoff error:\n'''\n\n# illustrates round-off error::\nx = 1000/3.0\nprint \"x = %f\" %x # x = 1000/3\ny = x - 333.0\nprint \"y = %f\" % y # y = 1/3\nz = 3*y - 1.0\nprint \"z = %f\" %z # z = 3(1/3) - 1\nif (z == 0):\n print \"z == 0.\\n\"\nelse:\n print \"z does not equal 0.\\n\" # z != 0\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 333.333333\ny = 0.333333\nz = -0.000000\nz does not equal 0.\n\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.15 Hidden Round-off Error\nThis program implements the quadratic formula to solve quadratic equations.\n'''\nimport math\n\n# implements the quadratic formula\na = float(raw_input(\"Enter the coefficients of a quadratic equation:\\n a : \"))\nb = float(raw_input('b : '))\nc = float(raw_input('c : '))\n\nprint \"The equation is: \",\nprint a,\nprint \"*x*x + \",\nprint b,\nprint \"*x + \" ,\nprint c,\nprint \" = 0\" \n\nd = b*b - 4*a*c # discriminant\nsqrtd = math.sqrt(d)\nx1 = (-b + sqrtd)/(2*a)\nx2 = (-b - sqrtd)/(2*a)\nprint \"The solutions are:\"\nprint \"\\tx1 = %f\" % x1\nprint \"\\tx2 = %f\" % x2\nprint \"Check:\" \nprint \"\\ta*x1*x1 + b*x1 + c = %f\" %( a*x1*x1 + b*x1 + c)\nprint \"\\ta*x2*x2 + b*x2 + c = %f\" %( a*x2*x2 + b*x2 + c)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter the coefficients of a quadratic equation:\n a : 2\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "b : 1\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "c : -3\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The equation is: 2.0 *x*x + 1.0 *x + -3.0 = 0\nThe solutions are:\n\tx1 = 1.000000\n\tx2 = -1.500000\nCheck:\n\ta*x1*x1 + b*x1 + c = 0.000000\n\ta*x2*x2 + b*x2 + c = 0.000000\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.17 Scientific Format\nThis program shows how floating-point values may be input in scientific format:\n'''\n\n# prints double values in scientific e-format:\nx = float(raw_input(\"Enter float: \"))\nprint \"Its reciprocal is: \",\nprint 1/x ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter float: 234.567e89\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Its reciprocal is: 4.2631742743e-92\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.18 Scope of Variables\nNOte : Python does not have variable scopes. Once variable is declared then it stays live until program gets exit.\nso it wont give any errors.\n\n'''\n# illustrates the scope of variables:\nx = 11\n# ERROR: this is not in the scope of x\nif True:\n x = 22 # OK: this is in the scope of x\n y = 33 # ERROR: this is not in the scope of y\n x = 44 # OK: this is in the scope of x\n y = 55 # OK: this is in the scope of y\nx = 66 # OK: this is in the scope of x\ny = 77 # ERROR: this is not in the scope of y\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 2.19 Nested and Parallel Scopes\nNOte : Python does not have variable scopes. Once variable is declared then it stays live until program gets exit.\nso output would be differ.\n'''\n# this x is global\nx = 11\n\nif True:\n # illustrates the nested and parallel scopes:\n x = 22\n # begin scope of internal block\n if True:\n x = 33\n print \"In block inside main(): x = %d \" % x\n # end scope of internal block\nprint \"In main(): x = %d\" %x \nprint \"In main(): x = %d \"% x", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "In block inside main(): x = 33 \nIn main(): x = 33\nIn main(): x = 33 \n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb new file mode 100644 index 00000000..8a41802e --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch3.ipynb @@ -0,0 +1,606 @@ +{ + "metadata": { + "name": "ch3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.1 Testing for Divisibility\nThis program tests if one positive integer is not divisible by another:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "66 is not divisible by 7\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.2 Testing for Divisibility Again\nThis program is the same as the program in Example 3.1 except that the if statement has been replaced\nby an if..else statement:\n'''\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\nif (n%d):\n print \"%d is not divisible by %d\" %(n,d)\nelse:\n print \"%d is divisible by %d\" %(n,d)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "56\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "56 is divisible by 7\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.3 The Minimum of Two Integers\nThis program prints the minimum of the two integers entered:\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\n\nif (m < n):\n print \"%d is the minimum.\" %m\nelse:\n print \"%d is the minimum.\" %n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "55 is the minimum.\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.4 A Common Programming Error\nThis program is erroneous:\n'''\nprint \"Enter an integer: \"\nn = int(raw_input())\nif (n = 22):\n print \"%d = 22\" %n\nelse: \n print \"%d != 22\" %n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (<ipython-input-4-d2cee3e182b1>, line 7)", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"<ipython-input-4-d2cee3e182b1>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m if (n = 22):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.5 The Minimum of Three Integers\nThis program is similar to the one in Example 3.3 except that it applies to three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nm=n1\n# now min <= n1\nif (n2 < m):\n m = n2 # now min <= n1 and min <= n2\nif (n3 < m):\n m = n3 # now min <= n1, min <= n2, and min <= n3\nprint \"Their minimum is %d\" % m", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Their minimum is 33\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.6 A Statement Block within an if Statement\nThis program inputs two integers and then outputs them in increasing order:\n'''\n\nprint \"Enter two integers: \"\nx = int(raw_input())\ny = int(raw_input())\n\nif (x > y):\n temp=x\n x = y\n y = temp\n \n\nprint \"%d <= %d\" %(x,y)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "44 <= 66\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.7 Using Blocks to Limit Scope\nThis program uses the same name n for three different variables:\n'''\nn=44\nprint \"n = %d\" % n \nif True:\n # scope extends over 4 lines\n print \"Enter an integer: \"\n n = int(raw_input())\n print \"n = %d\" % n \n\nif True:\n print \"n = %d\" % n \n # the n that was declared first\nif True:\n print \"n = %d\" % n \n\nprint \"n = %d\" % n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\nEnter an integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 77\nn = 77\nn = 77\nn = 77\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.8 Using Compound Conditions\nThis program has the same effect as the one in Example 3.5 on page 39. This version uses compound\nconditions to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 <= n2 and n1 <= n3):\n print \"Their minimum is %d\" % n1\n \nif (n2 <= n1 and n2 <= n3):\n print \"Their minimum is %d \" % n2 \nif (n3 <= n1 and n3 <= n2):\n print \"Their minimum is %d\" % n3 \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Their minimum is 33 \n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.9 User-Friendly Input\nThis program allows the user to input either a 'Y' or a 'y' for 'yes':\n'''\n\nprint \"Are you enrolled (y/n): \"\nans = raw_input()\nif (ans == 'Y' or ans == 'y'):\n print \"You are enrolled.\\n\"\nelse: \n print \"You are not enrolled.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Are you enrolled (y/n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "y\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "You are enrolled.\n\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.10 Short-Circuiting\nThis program tests integer divisibility:\n'''\n\nprint \"Enter two positive integers: \";\nn = int(raw_input())\nd = int(raw_input())\n\nif (d != 0 and n%d == 0): \n print \"%d divides %d\" %(d,n)\nelse:\n print \"%d does not divide %d\"% (d,n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "6 does not divide 33\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.11 Another Logical Error\nThis program is erroneous:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\n\nif (n1 >= n2 >= n3):\n print \"max = x\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.12 Nesting Selection Statements\nThis program has the same effect as the one in Example 3.10 on page 42:\n'''\nprint \"Enter two positive integers: \"\nn = int(raw_input())\nd = int(raw_input())\n\nif (d != 0):\n if (n%d == 0):\n print d,\n print \" divides %d\" % n \n else:\n print \"%d does not divide %d\" %(d,n)\nelse:\n print '%d does not divide %d '%(d,n)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two positive integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "44 does not divide 55\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.13 Using Nested Selection Statements\nThis program has the same effect as those in Example 3.5 on page 39 and Example 3.8 on page 41.\nThis version uses nested if..else statements to find the minimum of three integers:\n'''\nprint \"Enter three integers: \"\nn1 = int(raw_input())\nn2 = int(raw_input())\nn3 = int(raw_input())\nif (n1 < n2):\n if (n1 < n3):\n print \"Their minimum is : %d\" % n1\n else:\n print \"Their minimum is : %d\" % n3\nelse: # n1 >= n2\n if (n2 < n3):\n print \"Their minimum is : %d\" % n2\n else:\n print \"Their minimum is %d\" % n3", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter three integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Their minimum is : 33\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.14 A Guessing Game\nThis program finds a number that the user selects from 1 to 8:\n'''\n\nprint \"Pick a number from 1 to 8.\" \nanswer = int(raw_input())\nprint \"Is it less than 5? (y|n): \"\nanswer = raw_input()\nif (answer == 'y'): # 1 <= n <= 4\n print \"Is it less than 3? (y|n): \"\n answer = raw_input() \n if (answer == 'y'): # 1 <= n <= 2\n print \"Is it less than 2? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 1.\"\n else:\n print \"Your number is 2.\"\n else: # 3 <= n <= 4\n print \"Is it less than 4? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 3.\"\n else:\n print \"Your number is 4.\"\nelse: # 5 <= n <= 8\n print \"Is it less than 7? (y|n): \"\n answer = raw_input()\n if (answer == 'y'): # 5 <= n <= 6\n print \"Is it less than 6? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 5.\"\n else:\n print \"Your number is 6.\" \n else: # 7 <= n <= 8\n print \"Is it less than 8? (y|n): \"\n answer = raw_input()\n if (answer == 'y'):\n print \"Your number is 7.\" \n else:\n print \"Your number is 8.\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Pick a number from 1 to 8.\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Is it less than 5? (y|n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "n\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Is it less than 7? (y|n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "y\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Is it less than 6? (y|n): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "n\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your number is 6.\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.15 Using the else if Construct for Parallel Alternatives\nThis program requests the users language and then prints a greeting in that language:\n'''\nlanguage = raw_input(\"Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): \")\n\nif (language == 'e'): \n print \"Welcome to ProjectEuclid.\"\nelif (language == 'f'):\n print \"Bon jour, ProjectEuclid.\"\nelif (language == 'g'):\n print \"Guten tag, ProjectEuclid.\"\nelif (language == 'i'):\n print \"Bon giorno, ProjectEuclid.\"\nelif (language == 'r'):\n print \"Dobre utre, ProjectEuclid.\"\nelse:\n print \"Sorry; we don't speak your language.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Engl., Fren., Ger., Ital., or Rus.? (e|f|g|i|r): i\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Bon giorno, ProjectEuclid.\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.16 Using the else if Construct to Select a Range of Scores\nThis program converts a test score into its equivalent letter grade:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter your test score: 83\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your grade is a B.\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.17 Using a switch Statement to Select a Range of Scores\nThis program has the same effect as the one in Example 3.16:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter your test score: 83\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your grade is a B.\nGoodbye.\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.18 An Erroneous Fall-through in a switch Statement\nThis program was intended to have the same effect as the one in Example 3.17. But with\nstatements, the program execution falls through all the case statements it encounters:\n'''\nscore = int(raw_input(\"Enter your test score: \"))\na = int(score/10)\nif a == 10 or a == 9:\n print \"Your grade is an A.\"\nelif a == 8:\n print \"Your grade is a B.\" \nelif a == 7:\n print \"Your grade is a C.\" \nelif a == 6:\n print \"Your grade is a D.\"\nelif a==5 or a==4 or a==3 or a==2 or a==1 or a==0:\n print \"Your grade is an F.\" \nelse:\n print \"Error: score is out of range.\\n\"\n\nprint \"Goodbye.\"", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter your test score: 83\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Your grade is a B.\nGoodbye.\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 3.19 Finding the Minimum Again\n'''\n\nprint \"Enter two integers: \"\nm = int(raw_input())\nn = int(raw_input())\nprint min(m,n),\nprint 'is the minimum'", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "33 is the minimum\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch4.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch4.ipynb new file mode 100644 index 00000000..d137f53e --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch4.ipynb @@ -0,0 +1,839 @@ +{ + "metadata": { + "name": "ch4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.1 Using a while Loop to Compute a Sum of Consecutive Integers\nThis program computes the sum 1 + 2 + 3 + + n for an input integer n:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (i <= n):\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(i,s)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 6 integers is 15\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.2 Using a while Loop to Compute a Sum of Reciprocals\nThis program computes the sum of reciprocals s = 1 + 1/2 + 1/3 + 1/n where n is\ninteger for which n is greater than s\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\ns=0.0\ni=0\nwhile (s < bound):\n i += 1\n s += 1.0/i\n\nprint \"The sum of the first %d reciprocals is %f\" %(i,s)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 83 reciprocals is 5.002068\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.3 Using a while Loop to Repeat a Computation\nThis program prints the square root of each number input by the user. It uses a while loop to allow any\nnumber of computations in a single run of the program:\n'''\nimport math\nprint \"Enter a positive number: \"\nx = float(raw_input())\nwhile (x > 0):\n print \"sqrt(%d) = %f \"%(x,math.sqrt(x))\n print \"Enter another positive number (or 0 to quit): \"\n x = float(raw_input())\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive number: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "sqrt(5) = 2.236068 \nEnter another positive number (or 0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "sqrt(3) = 1.732051 \nEnter another positive number (or 0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.4 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=1\nprint \"Enter a positive integer: \";\nn = int(raw_input())\ns=0\nwhile(True):\n if (i > n):\n break # terminates the loop immediately\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 5 integers is 15\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.5 The Fibonacci Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.6 Using the exit(0) Function\nThe exit() function provides another way to terminate a loop. When it executes, it terminates the\nprogram itself:\n'''\nimport sys\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n sys.exit(0)\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "ename": "SystemExit", + "evalue": "0", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8" + }, + { + "output_type": "stream", + "stream": "stderr", + "text": "To exit: use 'exit', 'quit', or Ctrl-D.\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \nFibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.8 Using a do..while Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=0\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile i<=n:\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 10 integers is 55\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.9 The Factorial Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Factorial numbers < %d:\\n1, 1\" %bound,\nf=1\ni=1\nwhile f < bound:\n i += 1\n f *= i\n print \", %d\" %f,\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Factorial numbers < 10:\n1, 1 , 2 , 6 , 24\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.10 Using a for Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0;\nfor i in range(0,n+1):\n s += i\nprint \"The sum of the first %d integers is %d\" %(n,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 10 integers is 55\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.11 Reusing for Loop Control Variable Names\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nfor i in range(1,n/2): # the scope of this i is this loop\n s += i\n\nfor i in range(n/2,n+1): # the scope of this i is this loop\n s += i\nprint \"The sum of the first %d integers is %d\" % (n,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 10 integers is 55\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.12 The Factorial Numbers Again\nThis program has the same effect as the one in Example 4.9 on page 65:\n'''\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\n\nprint \"Factorial numbers that are <= %d:\\n1, 1\" %bound,\nf=1\nfor i in range(2,bound+1):\n f *= i\n print \", %d\" % f,\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Factorial numbers that are <= 10:\n1, 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 , 3628800\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.13 Using a Descending for Loop\nThis program prints the first ten positive integers in reverse order:\n'''\n\nfor i in range(10,0,-1):\n print i,\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "10 9 8 7 6 5 4 3 2 1\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.14 Using a for Loop with a Step Greater than One\nThis program determines whether an input number is prime:\n'''\nprime = True\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nif (n < 2):\n print \"%d is not prime.\" %n\n prime = False\nelif (n < 4):\n print \"%d is prime.\" %n\n prime = False\nelif (n%2 == 0):\n print \"%d = 2* %d\" %(n,n/2)\n prime = False\nelse:\n for d in range(3,n/2+1):\n if (n%d == 0):\n print \"%d = %d * %d\" %(n,d,n/d)\n prime = False\nif prime: \n print \"%d is prime.\"%n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "11\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "11 is prime.\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.15 Using a Sentinel to Control a for Loop\nThis program finds the maximum of a sequence of input numbers:\n'''\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0:\n n = int(raw_input())\n if n > m :\n m = n\n\nprint \"max = %d\" % m", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter positive integers (0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "19\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "42\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "max = 42\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.16 Using a Loop Invariant to Prove that a for Loop is Correct\nThis program finds the minimum of a sequence of input numbers. It is similar to the program in\nExample 4.15:\n'''\n\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0: \n if n < m :\n m = n\n n = int(raw_input())\n\nprint \"min = %d\" % m\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter positive integers (0 to quit): \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "19\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "42\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "min = 1\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.17\n'''\n\nm = 95\nn = 11\nwhile m%n > 0:\n print \"%d modulo %d = %d\" %(m,n,m%n)\n m -= 3\n n += 1\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "95 modulo 11 = 7\n92 modulo 12 = 8\n89 modulo 13 = 11\n86 modulo 14 = 2\n83 modulo 15 = 8\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.18 Nesting for Loops\nThis program prints a multiplication table:\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n print \"%4d\" % (x*y),\n print \"\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " 1 2 3 4 5 6 7 8 9 10 11 12 \n 2 4 6 8 10 12 14 16 18 20 22 24 \n 3 6 9 12 15 18 21 24 27 30 33 36 \n 4 8 12 16 20 24 28 32 36 40 44 48 \n 5 10 15 20 25 30 35 40 45 50 55 60 \n 6 12 18 24 30 36 42 48 54 60 66 72 \n 7 14 21 28 35 42 49 56 63 70 77 84 \n 8 16 24 32 40 48 56 64 72 80 88 96 \n 9 18 27 36 45 54 63 72 81 90 99 108 \n 10 20 30 40 50 60 70 80 90 100 110 120 \n 11 22 33 44 55 66 77 88 99 110 121 132 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.19 Testing a Loop Invariant\n'''\nimport math\n# defines pow() and log()\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nd=0 # the discrete binary logarithm of n\np2d=1 # = 2^d\ni = n\nwhile i > 1:\n # INVARIANT: 2^d <= n/i < 2*2^d\n p2d=math.pow(2,d) # = 2^d\n print \"%2d <= %2d\" %(p2d,2*p2d)\n i /= 2\n d += 1\n\np2d=math.pow(2,d) # = 2^d\nprint \"%2d <= %2d < %2d\" %(p2d,n,2*p2d)\nprint \" The discrete binary logarithm of is %d\" % d \nlgn = math.log(n)/math.log(2) # base 2 logarithm\nprint \"The continuous binary logarithm of is %f\" % lgn", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "17\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 1 <= 2\n 2 <= 4\n 4 <= 8\n 8 <= 16\n16 <= 17 < 32\n The discrete binary logarithm of is 4\nThe continuous binary logarithm of is 4.087463\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.20 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60. It uses a break statement to\ncontrol the loop:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (True):\n if (i > n):\n break\n s += i\n i += 1\n\nprint \"The sum of the first %d integers is %d\" %(i,s)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter a positive integer: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "10\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The sum of the first 11 integers is 55\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.21 Controlling Input with a Sentinel\nThis program reads a sequence of positive integers, terminated by 0, and prints their average:\n'''\ncount=0\ns=0\nprint \"Enter positive integers (0 to quit):\" \nwhile True: # \"forever\"\n print \"\\t %d :\" %(count + 1),\n n = int(raw_input())\n if (n <= 0):\n break\n count += 1\n s += n\n\nprint \"The average of those %d positive numbers is \" %count,\nprint float(s)/count\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter positive integers (0 to quit):\n\t 1 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "12\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t 2 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "32\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t 3 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "11\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t 4 :" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " The average of those 3 positive numbers is 18.3333333333\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.22 Using a break Statement with Nested Loops\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n if y>x:\n break\n else:\n print '%4d' %(x*y),\n print ''\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " 1 \n 2 4 \n 3 6 9 \n 4 8 12 16 \n 5 10 15 20 25 \n 6 12 18 24 30 36 \n 7 14 21 28 35 42 49 \n 8 16 24 32 40 48 56 64 \n 9 18 27 36 45 54 63 72 81 \n 10 20 30 40 50 60 70 80 90 100 \n 11 22 33 44 55 66 77 88 99 110 121 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.23 Using continue and break Statements\nThis little program illustrates the continue and break statements:\n'''\nwhile True:\n n = int(raw_input('Enter int : '))\n if (n%2 == 0):\n continue\n if (n%3 == 0):\n break\n print \"\\tBottom of loop.\\n\"\nprint \"\\tOutside of loop.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tBottom of loop.\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 6\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Enter int : 9\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tOutside of loop.\n\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.24 Using a goto Statement to Break Out of a Nest of Loops\nNote : Python has no goto facility.\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.25 Using a Flag to Break Out of a Nest of Loops\nThis program has the same output as that in Example 4.24:\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.26 Generating Pseudo-Random Numbers\nThis program uses the rand() function to generate pseudo-random numbers:\n'''\nimport random\n\n# prints pseudo-random numbers:\n\nfor i in range(0,8):\n print random.random()\n\n ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0.702115758628\n0.969460447904\n0.409934401112\n0.700339443791\n0.093528851602\n0.132172955687\n0.0162887279366\n0.943010713478\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.27 Setting the Seed Interactively\nThis program is the same as the one in Example 4.26 except that it allows the pseudo-random number\ngenerator's seed to be set interactively:\n'''\nimport random\n# prints pseudo-random numbers:\nprint \"Enter seed: \"\nseed = int(raw_input())\nrandom.seed(seed);\nfor i in range(0,8):\n print random.random()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter seed: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "0.62290169489\n0.741786989261\n0.795193565566\n0.942450283777\n0.73989857474\n0.922324996665\n0.0290052282836\n0.465622654378\n" + } + ], + "prompt_number": 22 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.28 Setting the Seed from the System Clock\nThis program is the same as the one in Example 4.27 except that it sets the pseudo-random number\ngenerator's seed from the system clock.\n'''\nimport random\nfor i in range(0,8):\n print random.random()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0.943356716998\n0.648974553137\n0.900900491751\n0.113205964653\n0.469069047782\n0.24657283262\n0.543760859236\n0.573941187928\n" + } + ], + "prompt_number": 23 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 4.29 Generating Pseudo-Random Numbers in Given Range\nThis program is the same as the one in Example 4.28 except that the pseudo-random numbers that it\ngenerates are restricted to given range:\n'''\nimport random\nprint \"Enter minimum and maximum: \"\nm = int(raw_input())\nn = int(raw_input())\n# lowest and highest numbers\nr = n - m + 1\n# number of numbers in range\nfor i in range(0,20):\n j = int(random.random()*100 % r + m)\n print j,\n print \" \",\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter minimum and maximum: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "15\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "6 15 10 8 15 9 7 7 11 6 5 15 14 15 15 15 11 13 14 6 \n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb new file mode 100644 index 00000000..6ef37c88 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch5.ipynb @@ -0,0 +1,694 @@ +{ + "metadata": { + "name": "ch5" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.1 The Square Root Function sqrt()\n'''\nimport math\n\n# tests the sqrt() function:\nfor i in range(0,6):\n print \"\\t %d \\t %f\" %(i,math.sqrt(i))", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\t 0 \t 0.000000\n\t 1 \t 1.000000\n\t 2 \t 1.414214\n\t 3 \t 1.732051\n\t 4 \t 2.000000\n\t 5 \t 2.236068\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.2 Testing a Trigonometry Identity\n'''\nimport math\n# tests the identity sin 2x = 2 sin x cos x:\nx = 0\nwhile x < 2:\n print \"%f \\t\\t %f \\t %f\" %(x,math.sin(2*x),2*math.sin(x)*math.cos(x))\n x += 0.2\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0.000000 \t\t 0.000000 \t 0.000000\n0.200000 \t\t 0.389418 \t 0.389418\n0.400000 \t\t 0.717356 \t 0.717356\n0.600000 \t\t 0.932039 \t 0.932039\n0.800000 \t\t 0.999574 \t 0.999574\n1.000000 \t\t 0.909297 \t 0.909297\n1.200000 \t\t 0.675463 \t 0.675463\n1.400000 \t\t 0.334988 \t 0.334988\n1.600000 \t\t -0.058374 \t -0.058374\n1.800000 \t\t -0.442520 \t -0.442520\n2.000000 \t\t -0.756802 \t -0.756802\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.3 A cube() Function\nHere is a simple example of a user-defined function:\n'''\n\ndef cube(x):\n # returns cube of x:\n return x*x*x\n\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.4 A Test Driver for the cube() Function\nHere is a complete program that includes the definition of the cube() function from Example 5.4\ntogether with a test driver for it:\n'''\n\ndef cube(x):\n # returns cube of x:\n return x*x*x\n\n# tests the cube() function:\nn=1\nwhile (n != 0):\n n = int(raw_input())\n print \"\\tcube( %d ) = %d\" %(n,cube(n))", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tcube( 4 ) = 64\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tcube( 2 ) = 8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tcube( 9 ) = 729\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tcube( 0 ) = 0\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.5 A Test Driver for the max() Function\nHere is a function with two parameters. It returns the larger of the two values passed to it.\n'''\n\ndef maximum(x,y):\n # returns larger of the two given integers:\n if (x < y):\n return y\n else:\n return x\n\n# tests the max() function:\nm = 1\nn = 1\nwhile m != 0: \n m = int(raw_input())\n n = int(raw_input())\n print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax( 5 , 2 ) = 5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax( 0 , 3 ) = 3\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.6 The max() Function with Declaration Separate from Definition\n'''\n\ndef maximum(x,y):\n # returns larger of the two given integers:\n if (x < y):\n return y\n else:\n return x\n\n# tests the max() function:\nm = 1\nn = 1\nwhile m != 0: \n m = int(raw_input())\n n = int(raw_input())\n print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax( 5 , 2 ) = 5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax( 0 , 3 ) = 3\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.8 The max() Function Compiled Separately\n'''\n\n\n# returns larger of the two given integers:\n\nm = 1\nn = 1\nwhile m!=0:\n m = int(raw_input())\n n = int(raw_input())\n print \"\\tmax(%d,%d) = %d\" %(m,n, max(m,n))\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax(5,4) = 5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax(4,3) = 4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax(8,0) = 8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\tmax(0,5) = 5\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.9 The Factorial Function\n'''\n\ndef fact(n):\n if (n < 0):\n return 0\n f = 1\n while (n > 1):\n f *= n\n n -= 1\n return f\n\nfor i in range(-1,6):\n print fact(i),\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0 1 1 2 6 24 120\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.10 The Permutation Function\n'''\ndef fact(n):\n if (n < 0):\n return 0\n f = 1\n while (n > 1):\n f *= n\n n -= 1\n return f\n\n\ndef perm(n,k):\n # returns P(n,k), the number of permutations of k from n:\n if (n < 0 or k < 0 or k > n):\n return 0\n return fact(n)/fact(n-k)\n\nfor i in range(-1,8):\n for j in range(-1,i+2):\n print perm(i,j),\n print ''\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "0 0 \n0 1 0 \n0 1 1 0 \n0 1 2 2 0 \n0 1 3 6 6 0 \n0 1 4 12 24 24 0 \n0 1 5 20 60 120 120 0 \n0 1 6 30 120 360 720 720 0 \n0 1 7 42 210 840 2520 5040 5040 0 \n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.11 A Function that Prints Dates\n'''\n\ndef printDate(m,d,y):\n # prints the given date in literal form:\n if (m < 1 or m > 12 or d < 1 or d > 31 or y < 0):\n print \"Error: parameter out of range.\\n\"\n return\n if m == 1:\n print \"January \",\n elif m ==2:\n print \"February \",\n elif m==3 :\n print \"March \",\n elif m==4:\n print \"April \",\n elif m==5:\n print \"May \",\n elif m==6:\n print \"June \",\n elif m==7:\n print \"July \",\n elif m==8:\n print \"August \",\n elif m==9:\n print \"September \",\n elif m==10:\n print \"October \",\n elif m==1:\n print \"November \",\n else:\n print \"December \",\n print d , \", \", y \n\n# tests the printDate() function:\nmonth = 1\nwhile month > 0:\n month = int(raw_input())\n day = int(raw_input())\n year = int(raw_input())\n printDate(month,day,year)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "12\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1989\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "September 12 , 1989\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2001\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Error: parameter out of range.\n\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.12 Classifying Characters\n'''\nimport string\ndef ispunct(s):\n return all(c in string.punctuation for c in s)\ndef printCharCategory(c):\n # prints the category to which the given character belongs:\n print \"The character [\" + c + \"] is a \",\n if(c.isdigit()):\n print \"digit.\\n\"\n elif (c.islower()):\n print \"lower-case letter.\\n\"\n elif (c.isupper()): \n print \"capital letter.\\n\"\n elif (c.isspace()):\n print \"white space character.\\n\"\n elif (ord(c) >= 10 and ord(c) <= 15 or ord(c) == 0):\n print \"control character.\\n\"\n elif (ispunct(c)):\n print \"punctuation mark.\\n\"\n else:\n print \"Error.\\n\"\n\n# prints the category to which the given character belongs;\n# tests the printCharCategory() function:\nfor c in range(128):\n printCharCategory(chr(c))\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " The character [\u0000] is a control character.\n\nThe character [\u0001] is a Error.\n\nThe character [\u0002] is a Error.\n\nThe character [\u0003] is a Error.\n\nThe character [\u0004] is a Error.\n\nThe character [\u0005] is a Error.\n\nThe character [\u0006] is a Error.\n\nThe character [\u0007] is a Error.\n\nThe character [\b] is a Error.\n\nThe character [\t] is a white space character.\n\nThe character [\n] is a white space character.\n\nThe character [\u000b] is a white space character.\n\nThe character [\f] is a white space character.\n\nThe character [\r] is a white space character.\n\nThe character [\u000e] is a control character.\n\nThe character [\u000f] is a control character.\n\nThe character [\u0010] is a Error.\n\nThe character [\u0011] is a Error.\n\nThe character [\u0012] is a Error.\n\nThe character [\u0013] is a Error.\n\nThe character [\u0014] is a Error.\n\nThe character [\u0015] is a Error.\n\nThe character [\u0016] is a Error.\n\nThe character [\u0017] is a Error.\n\nThe character [\u0018] is a Error.\n\nThe character [\u0019] is a Error.\n\nThe character [\u001a] is a Error.\n\nThe character [\u001b] is a Error.\n\nThe character [\u001c] is a Error.\n\nThe character [\u001d] is a Error.\n\nThe character [\u001e] is a Error.\n\nThe character [\u001f] is a Error.\n\nThe character [ ] is a white space character.\n\nThe character [!] is a punctuation mark.\n\nThe character [\"] is a punctuation mark.\n\nThe character [#] is a punctuation mark.\n\nThe character [$] is a punctuation mark.\n\nThe character [%] is a punctuation mark.\n\nThe character [&] is a punctuation mark.\n\nThe character ['] is a punctuation mark.\n\nThe character [(] is a punctuation mark.\n\nThe character [)] is a punctuation mark.\n\nThe character [*] is a punctuation mark.\n\nThe character [+] is a punctuation mark.\n\nThe character [,] is a punctuation mark.\n\nThe character [-] is a punctuation mark.\n\nThe character [.] is a punctuation mark.\n\nThe character [/] is a punctuation mark.\n\nThe character [0] is a digit.\n\nThe character [1] is a digit.\n\nThe character [2] is a digit.\n\nThe character [3] is a digit.\n\nThe character [4] is a digit.\n\nThe character [5] is a digit.\n\nThe character [6] is a digit.\n\nThe character [7] is a digit.\n\nThe character [8] is a digit.\n\nThe character [9] is a digit.\n\nThe character [:] is a punctuation mark.\n\nThe character [;] is a punctuation mark.\n\nThe character [<] is a punctuation mark.\n\nThe character [=] is a punctuation mark.\n\nThe character [>] is a punctuation mark.\n\nThe character [?] is a punctuation mark.\n\nThe character [@] is a punctuation mark.\n\nThe character [A] is a capital letter.\n\nThe character [B] is a capital letter.\n\nThe character [C] is a capital letter.\n\nThe character [D] is a capital letter.\n\nThe character [E] is a capital letter.\n\nThe character [F] is a capital letter.\n\nThe character [G] is a capital letter.\n\nThe character [H] is a capital letter.\n\nThe character [I] is a capital letter.\n\nThe character [J] is a capital letter.\n\nThe character [K] is a capital letter.\n\nThe character [L] is a capital letter.\n\nThe character [M] is a capital letter.\n\nThe character [N] is a capital letter.\n\nThe character [O] is a capital letter.\n\nThe character [P] is a capital letter.\n\nThe character [Q] is a capital letter.\n\nThe character [R] is a capital letter.\n\nThe character [S] is a capital letter.\n\nThe character [T] is a capital letter.\n\nThe character [U] is a capital letter.\n\nThe character [V] is a capital letter.\n\nThe character [W] is a capital letter.\n\nThe character [X] is a capital letter.\n\nThe character [Y] is a capital letter.\n\nThe character [Z] is a capital letter.\n\nThe character [[] is a punctuation mark.\n\nThe character [\\] is a punctuation mark.\n\nThe character []] is a punctuation mark.\n\nThe character [^] is a punctuation mark.\n\nThe character [_] is a punctuation mark.\n\nThe character [`] is a punctuation mark.\n\nThe character [a] is a lower-case letter.\n\nThe character [b] is a lower-case letter.\n\nThe character [c] is a lower-case letter.\n\nThe character [d] is a lower-case letter.\n\nThe character [e] is a lower-case letter.\n\nThe character [f] is a lower-case letter.\n\nThe character [g] is a lower-case letter.\n\nThe character [h] is a lower-case letter.\n\nThe character [i] is a lower-case letter.\n\nThe character [j] is a lower-case letter.\n\nThe character [k] is a lower-case letter.\n\nThe character [l] is a lower-case letter.\n\nThe character [m] is a lower-case letter.\n\nThe character [n] is a lower-case letter.\n\nThe character [o] is a lower-case letter.\n\nThe character [p] is a lower-case letter.\n\nThe character [q] is a lower-case letter.\n\nThe character [r] is a lower-case letter.\n\nThe character [s] is a lower-case letter.\n\nThe character [t] is a lower-case letter.\n\nThe character [u] is a lower-case letter.\n\nThe character [v] is a lower-case letter.\n\nThe character [w] is a lower-case letter.\n\nThe character [x] is a lower-case letter.\n\nThe character [y] is a lower-case letter.\n\nThe character [z] is a lower-case letter.\n\nThe character [{] is a punctuation mark.\n\nThe character [|] is a punctuation mark.\n\nThe character [}] is a punctuation mark.\n\nThe character [~] is a punctuation mark.\n\nThe character [\u007f] is a Error.\n\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.13 A Function that Tests Primality\n'''\nimport math\ndef isPrime(n):\n # returns True if n is prime, False otherwise:\n sqrtn = math.sqrt(n)\n if (n < 2):\n return False\n # 0 and 1 are not primes\n if (n < 4):\n return True\n # 2 and 3 are the first primes\n if (n%2 == 0):\n return False\n # 2 is the only even prime\n for d in range(3,int(sqrtn+1),2):\n if (n%d == 0):\n return False\n # n has a nontrivial divisor\n return True;\n\nfor n in range(0,80):\n if (isPrime(n)):\n print n,\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.14 A Leap Year Function\n'''\ndef isLeapYear(y):\n # returns true iff y is a leap year:\n return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)\n\n# tests the isLeapYear() function:\nn = 2\nwhile n > 1:\n n = int(raw_input())\n if (isLeapYear(n)):\n print \"%d is a leap year.\" % n\n else:\n print \"%d is not a leap year.\" %n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2004\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "2004 is a leap year.\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2006\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "2006 is not a leap year.\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2013\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "2013 is not a leap year.\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "0 is a leap year.\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.15 A Function for Reading the User's Age\n'''\n\ndef age():\n # prompts the user to input his/her age, and returns that value:\n while (True):\n print \"How old are you: \"\n n = int(raw_input())\n if (n < 0):\n print \"\\a\\tYour age could not be negative.\"\n elif (n > 120):\n print \"\\a\\tYou could not be over 120.\"\n else:\n return n\n print \"\\n\\tTry again.\\n\"\n\na = age();\nprint \"\\nYou are %d years old.\" %a\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "How old are you: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "-12\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\u0007\tYour age could not be negative.\n\n\tTry again.\n\nHow old are you: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "125\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\u0007\tYou could not be over 120.\n\n\tTry again.\n\nHow old are you: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "24\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\nYou are 24 years old.\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.16 The swap() Function\n'''\n\ndef swap(x,y):\n # exchanges the values of x and y:\n x[0],y[0] = y[0],x[0]\n\na = [22.2]\nb = [44.4]\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\nswap(a,b)\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 22.20 , b = 44.40 \na = 44.40 , b = 22.20 \n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.17 \nNote : Python doesn't support pass value by reference. but can be done by passing list.\n'''\n\ndef f(x,y):\n x[0]= 88\n y[0] = 99\n\n# tests the f() function:\na = [22]\nb = [44]\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\nf(a,b)\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\nf(2*a,b)\nprint \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 22.00 , b = 44.00 \na = 88.00 , b = 99.00 \na = 88.00 , b = 99.00 \n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.18 Returning More than One Value\n'''\n\ndef computeCircle(r):\n # returns the area and circumference of a circle with radius r:\n PI = 3.141592653589793\n area = PI*r*r\n circumference = 2*PI*r\n return area,circumference\n\n# tests the computeCircle() function:\nprint \"Enter radius: \"\nr = int(raw_input())\na,c = computeCircle(r)\nprint \"area = %.2f , circumference = %.2f\" %(a,c)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter radius: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "area = 78.54 , circumference = 31.42\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.19\nNote : Python passes variable by value and not by reference. So output would be differ.\n'''\n\n\ndef f(x,y,z):\n x[0] += z[0]\n y[0] += z[0]\n print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n\nx = [22]\ny = [33]\nz = [44]\n\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\nf(x,y,z)\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\nx[0] = 2*x[0] - 3\nf(x,y,z)\nprint \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 22 , y = 33 , z = 44\nx = 66 , y = 77 , z = 44\nx = 66 , y = 77 , z = 44\nx = 173 , y = 121 , z = 44\nx = 173 , y = 121 , z = 44\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.20\n'''\ndef cube(x):\n # returns cube of x:\n return x*x*x\n\n# tests the cube() function:\nprint cube(4)\nx = int(raw_input())\ny = cube(2*x-3)\nprint y\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "64\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "343\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.21 Nested and Parallel Scopes\nPython has it's own scope so output would be differ.\n'''\nx = 11\n\ndef f():\n x = 44\n print \"In f(): x = %d\" % x \n\ndef g():\n print \"In g(): x = %d\" % x \n\nx = 22\nx = 33\nprint \"In block inside main(): x = %d\" % x\n\n\nprint \"In main(): x = %d\" % x \nprint \"In main(): ::x = %d\" % x \nf()\ng()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "In block inside main(): x = 33\nIn main(): x = 33\nIn main(): ::x = 33\nIn f(): x = 44\nIn g(): x = 33\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.22 \n'''\n\ndef max_(x, y,z=0):\n if x > y and x > y:\n return x\n elif y > x and y > z:\n return y\n else:\n return z\n \n \nprint max(99,77), \" \" , max(55,66,33)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "99 66\n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.23 Using the return Statement to Terminate a Program\n'''\n\n# prints the quotient of two input integers:\nprint \"Enter two integers: \"\nn = int(raw_input())\nd = int(raw_input())\nif (d == 0):\n import sys\n sys.exit(0)\nprint n , \"/\" , d , \" = \" , n/d \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter two integers: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "8 / 2 = 4\n" + } + ], + "prompt_number": 22 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.24 Using the exit() Function to Terminate a Program\n'''\n\ndef reciprocal(x):\n #returns the reciprocal of x:\n if (x == 0):\n import sys\n sys.exit(1); # terminate the program\n return 1.0/x\n\nx = float(raw_input())\nprint reciprocal(x)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "25\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "0.04\n" + } + ], + "prompt_number": 23 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 5.25 Default Parameters\nThis function evaluates the third degree polynomial a0 + a1x + a2x2 + a3x3. \n'''\ndef p(x,a0,a1=0,a2=0,a3=0):\n # returns a0 + a1*x + a2*x^2 + a3*x^3:\n return (a0 + (a1 + (a2 + a3*x)*x)*x)\n\n\n# tests the p() function:\nx = 2.0003\nprint \"p(x,7) = %f\" % p(x,7)\nprint \"p(x,7,6) = %f\" % p(x,7,6)\nprint \"p(x,7,6,5) = %f\" % p(x,7,6,5)\nprint \"p(x,7,6,5,4) = %f\" % p(x,7,6,5,4)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "p(x,7) = 7.000000\np(x,7,6) = 19.001800\np(x,7,6,5) = 39.007800\np(x,7,6,5,4) = 71.022203\n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb new file mode 100644 index 00000000..a355a8d2 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch6.ipynb @@ -0,0 +1,648 @@ +{ + "metadata": { + "name": "ch6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.1 Using Direct Access on Arrays\n'''\n\na = [0, 0, 0]\na[2] = 55.55\na[0] = 11.11\na[1] = 33.33\nprint \"a[0] = \" , a[0] \nprint \"a[1] = \" , a[1] \nprint \"a[2] = \" , a[2] \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a[0] = 11.11\na[1] = 33.33\na[2] = 55.55\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.2 Printing a Sequence in Order\n'''\n\nSIZE=5 # defines the size N for 5 elements\na = []\n# declares the array's elements as type double\nprint \"Enter \" , SIZE , \" numbers:\\t\"\nfor i in range(SIZE):\n a.append(float(raw_input()))\n \nprint \"In reverse order: \"\nfor i in range(SIZE-1,-1,-1):\n print \"\\t\" , a[i]\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 5 numbers:\t\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "1\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3.3\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4.4\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5.5\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "In reverse order: \n\t5.5\n\t4.4\n\t3.3\n\t2.0\n\t1.0\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.3 Initializing an Array\nThis program initializes the array a and then prints its values:\n'''\n\na = [ 22.2, 44.4, 66.6 ]\n\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i]\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 22.2\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 66.6\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.4 Initializing an Array with Trailing Zeros\n'''\n\na = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]\nsize = len(a)\nfor i in range(size):\n print \"\\ta[\" , i , \"] = \" , a[i] \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 22.2\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 66.6\n\ta[ 3 ] = 0\n\ta[ 4 ] = 0\n\ta[ 5 ] = 0\n\ta[ 6 ] = 0\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.5 An Uninitialized Array\nNOte : In PYthon , we have to initialize array. Without initializing it, it doesn't work.\nArray size is automatically incremented when we push data into it.\n'''\nimport numpy\nSIZE = 4\na = numpy.zeros(4)\n# declares the array's elements as type float\nfor i in range(SIZE):\n print \"\\ta[\" , i , \"] = \" , a[i]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 0.0\n\ta[ 1 ] = 0.0\n\ta[ 2 ] = 0.0\n\ta[ 3 ] = 0.0\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.6 Allowing an Array Index to Exceed its Bounds\nNOte : Python gives error when this kind of error occurs. so It wont print garbage values after bound exceeds.\n'''\nSIZE=4\na = [ 33.3, 44.4, 55.5, 66.6 ]\nfor i in range(7): # ERROR: index is out of bounds!\n print \"\\ta[\" , i , \"] = \" , a[i] \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-6-248fbb50a5b0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[1;36m33.3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m44.4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m55.5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m66.6\u001b[0m \u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# ERROR: index is out of bounds!\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\ta[\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;34m\"] = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIndexError\u001b[0m: list index out of range" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\ta[ 0 ] = 33.3\n\ta[ 1 ] = 44.4\n\ta[ 2 ] = 55.5\n\ta[ 3 ] = 66.6\n\ta[ 4 ] = " + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.7 Causing Side Effects\nThis program inadvertently changes the value of a variable when it accesses a nonexistent element of\nan array:\nNote : Python automatically increase size of array/list. so it wont give error and output would be differ than c gives.\n'''\n\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na.append(88.8) # ERROR: index is out of bounds!\nprint \"x = \" , x \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " x = 11.1\nx = 11.1\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.8 Causing Unhandled Exceptions\nThis program crashes because the array index gets too big:\n'''\na = [ 22.2, 44.4, 66.6 ]\nx=11.1\nprint \"x = \" , x \na[3333] = 88.8 # ERROR: index is out of bounds!\nprint \"x = \" , x \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list assignment index out of range", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-8-63b6c8e60294>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m11.1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3333\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m88.8\u001b[0m \u001b[1;31m# ERROR: index is out of bounds!\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIndexError\u001b[0m: list assignment index out of range" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "x = 11.1\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.9 Passing an Array to a Function that Returns its Sum\n'''\n\ndef sum_(a):\n s = 0\n for i in a:\n s += i\n return s\n \na = [ 11, 33, 55, 77 ]\nprint \"sum(a) = \" , sum_(a) \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "sum(a) = 176\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.10 Input and Output Functions for an Array\nThis program uses a read() function to input values into the array a interactively. Then it uses a\nprint() function to print the array:\n'''\ndef read(a):\n print \"Enter integers. Terminate with 0:\\n\"\n n = 1\n while True:\n n = int(raw_input(\"a[\" + str(len(a)) + \"]: \"))\n if n == 0:\n break\n a.append(n)\n \n\ndef print_(a):\n for i in a:\n print i ,\n\n\na = []\nread(a)\nprint \"The array has \" , len(a) , \" elements: \"\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter integers. Terminate with 0:\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[0]: 11\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[1]: 22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[2]: 33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[3]: 44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "a[4]: 0\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The array has 4 elements: \n11 22 33 44\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.11 Printing the Memory Location of an Array\nThis program prints the value of the address stored in an array name.\n'''\nimport sys\na = [ 22, 44, 66, 88 ]\nprint \"a = \" , id(a) # the address of a[0]\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 169156908\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.12 The Linear Search\nThis program tests a function that implements the Linear Search algorithm:\n'''\ndef index(x,a,n):\n for i in range(len(a)):\n if (a[i] == x):\n return i\n return n # x not found\n\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(50,a,7) = \" , index(50,a,7) \n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "index(44,a,7) = 1\nindex(50,a,7) = 7\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.13 The Bubble Sort\nThis program tests a function that implements the Bubble Sort algorithm.\n'''\ndef sort(a,n):\n # bubble sort:\n n = len(a)\n for i in range(n):\n # bubble up max{a[0..n-i]}:\n for j in range(n-i-1):\n if (a[j] > a[j+1]):\n a[j],a[j+1] = a[j+1],a[j]\n\ndef print_(a):\n for i in range(len(a)):\n print a[i],\n print ''\n \na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\n\nprint_(a)\nsort(a,8)\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.14 The Binary Search Algorithm\n'''\n\ndef index(x,a,n):\n # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n # binary search:\n lo=0\n hi=n-1\n while (lo <= hi):\n i = (lo + hi)/2 # the average of lo and hi\n if (a[i] == x):\n return i\n if (a[i] < x):\n lo = i+1 # continue search in a[i+1..hi]\n else:\n hi = i-1 # continue search in a[lo..i-1]\n return n # x was not found in a[0..n-1]\n\na = [ 22, 33, 44, 55, 66, 77, 88 ]\nprint \"index(44,a,7) = \" , index(44,a,7)\nprint \"index(60,a,7) = \" , index(60,a,7) \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "index(44,a,7) = 2\nindex(60,a,7) = 7\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.15 Determining whether an Array is Sorted\nThis program tests a boolean function that determines whether a given array is nondecreasing.\n'''\n\ndef isNondecreasing(a,n):\n # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n for i in range(1,n):\n if (a[i]<a[i-1]):\n return False\n return True\n\na = [ 22, 44, 66, 88, 44, 66, 55 ]\nprint \"isNondecreasing(a,4) = \" , isNondecreasing(a,4)\nprint \"isNondecreasing(a,7) = \" , isNondecreasing(a,7)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "isNondecreasing(a,4) = True\nisNondecreasing(a,7) = False\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.16 Using the assert() Function to Enforce a Precondition\nThis program tests an improved version of the search() function from Example 6.14. This version\nuses the isNondecreasing() function from Example 6.15 to determine whether the array is sorted. It\npasses the resulting boolean return value to the assert() function so that the search will not be carried\nout if the array is not sorted:\n'''\n\ndef isNondecreasing(a,n):\n # returns true iff a[0] <= a[1] <= ... <= a[n-1]:\n for i in range(1,n):\n if (a[i]<a[i-1]):\n return False\n return True\n\n\ndef index(x,a,n):\n # PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\n # binary search:\n assert(isNondecreasing(a,n)) \n \n lo=0\n hi=n-1\n while (lo <= hi):\n i = (lo + hi)/2\n if (a[i] == x):\n return i\n if (a[i] < x):\n lo = i+1 # continue search in a[i+1..hi]\n else:\n hi = i-1 # continue search in a[lo..i-1]\n return n # x was not found in a[0..n-1]\n\na = [ 22, 33, 44, 55, 66, 77, 88, 60 ]\nprint \"index(44,a,7) = \" , index(44,a,7) \nprint \"index(44,a,8.py) = \" , index(44,a,8) \nprint \"index(60,a,7) = \" , index(60,a,8)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-18-3a784dd6b25e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 34\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[1;36m22\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m33\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m55\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m66\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m77\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m88\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m60\u001b[0m \u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 35\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(44,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 36\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(44,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m44\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 37\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"index(60,a,7) = \"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m60\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m<ipython-input-18-3a784dd6b25e>\u001b[0m in \u001b[0;36mindex\u001b[1;34m(x, a, n)\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;31m# PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;31m# binary search:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0misNondecreasing\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 21\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[0mlo\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAssertionError\u001b[0m: " + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "index(44,a,7) = 2\nindex(44,a,7) = " + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.17 Enumerating the Days of the Week\nThis program defines an array high[] of seven floats, representing the high temperatures for\nthe seven days of a week: 138\n'''\nDay = [ 0, 1, 2, 3, 4, 5, 6 ]\nhigh = [ 88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7]\n\nfor i in Day:\n print \"The high temperature for day \" , i , \" was \" , high[i] \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": " The high temperature for day 0 was 88.3\nThe high temperature for day 1 was 95.0\nThe high temperature for day 2 was 91.2\nThe high temperature for day 3 was 89.9\nThe high temperature for day 4 was 91.4\nThe high temperature for day 5 was 92.5\nThe high temperature for day 6 was 86.7\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.18 The Bubble Sort Again\n'''\n\ndef sort(a,n):\n a.sort()\n\ndef print_(a,n):\n for i in a:\n print i,\n print ''\na = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]\nprint_(a,8);\nsort(a,8)\nprint_(a,8)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7 \n22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9 \n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.19 Reading and Printing a Two-Dimensional Array\nThis program shows how a two-dimensional array can be processed:\n'''\n\ndef read(a):\n print \"Enter 15 integers, 5 per row:\\n\"\n for i in range(3):\n ar = []\n print \"Row \" , i , \": \",\n for j in range(5):\n ar.append(int(raw_input()))\n a.append(ar)\n\ndef print_(a):\n for i in range(3):\n for j in range(5):\n print a[i][j],\n print ''\na = []\nread(a)\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 15 integers, 5 per row:\n\nRow 0 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "11\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Row 1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "60\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "50\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "30\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "90\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "70\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Row 2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "85\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "25\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "45\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "45\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "55\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 44 77 33 11 44 \n60 50 30 90 70 \n85 25 45 45 55 \n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.20 Processing a Two-Dimensional Array of Quiz Scores\n'''\ndef read(score):\n for s in range(3):\n print \"Student \" , s , \": \",\n st = []\n for q in range(5):\n st.append(int(raw_input()))\n score.append(st)\n\ndef printQuizAverages(score):\n for s in range(3):\n sm = 0\n for q in range(5):\n sm += score[s][q]\n print \"\\tStudent \" , s , \": \" , sm/5.0\n\ndef printClassAverages(score):\n for q in range(5):\n sm = 0\n for s in range(3):\n sm += score[s][q]\n print \"\\tQuiz \" , q , \": \" , sm/3.0\n\n\n\nNUM_STUDENTS = 3\nNUM_QUIZZES = 5\n\n\nscore = []\nprint \"Enter \" , NUM_QUIZZES , \" scores for each student: \"\nread(score)\nprint \"The quiz averages are:\"\nprintQuizAverages(score)\nprint \"The class averages are: \"\nprintClassAverages(score)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 5 scores for each student: \nStudent 0 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Student 1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " Student 2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "5\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "6\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "7\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "8\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " The quiz averages are:\n\tStudent 0 : 8.2\n\tStudent 1 : 8.8\n\tStudent 2 : 7.0\nThe class averages are: \n\tQuiz 0 : 7.33333333333\n\tQuiz 1 : 7.33333333333\n\tQuiz 2 : 8.33333333333\n\tQuiz 3 : 8.33333333333\n\tQuiz 4 : 8.66666666667\n" + } + ], + "prompt_number": 24 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 6.21 Processing a Three-Dimensional Array\nThis program simply counts the number of zeros in a three-dimensional array:\n'''\n\ndef numZeros(a,n1,n2,n3):\n count = 0\n for i in range(n1):\n for j in range(n2):\n for k in range(n3):\n if (a[i][j][k] == 0):\n count += 1\n return count\n\n\na = [ [ [5,0,2], [0,0,9], [4,1,0], [7,7,7] ],[ [3,0,0], [8,5,0], [0,0,0], [2,0,9] ]]\nprint \"This array has \" , numZeros(a,2,4,3) , \" zeros\"\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "This array has 11 zeros\n" + } + ], + "prompt_number": 25 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch7.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch7.ipynb new file mode 100644 index 00000000..f480fc86 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch7.ipynb @@ -0,0 +1,329 @@ +{ + "metadata": { + "name": "ch7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.1 Printing Pointer Values\n'''\n\nn=44\nprint \"n = \" , n \n# prints the value of n\nprint \"&n = \" , hex(id(n)) # prints the address of n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n&n = 0x8fc0eec\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.2\nNote : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"n = \" , n , \", rn = \" , rn \nn[0] -= 1\nprint \"n = \" , n , \", rn = \" , rn \nrn[0] *= 2\nprint \"n = \" , n , \", rn = \" , rn \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , rn = [44]\nn = [43] , rn = [43]\nn = [86] , rn = [86]\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.3 References Are Not Separate Variables\n'''\n\n\nn = [44]\nrn=n # r is a synonym for n\nprint \"&n = \" , hex(id(n)) , \", rn = \" , hex(id(rn ))\nrn2 = n\nrn3 = rn\nprint \"&rn2 = \" , hex(id(rn2)) , \", rn = \" , hex(id(rn ))\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "&n = 0x9c6228c , rn = 0x9c6228c\n&rn2 = 0x9c6228c , rn = 0x9c6228c\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.4 Using Pointer Variables\n\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"pn = \" , hex(id(pn)) , \", &pn = \" , hex(id(hex(id(pn))))\n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9c624ec\npn = 0x9c624ec , &pn = 0x9c6aa60\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.5\n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9c58d6c\n\tpn = 0x9c58d6c ,\n &pn = 0x9c6ab20\n*pn = [44]\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.6 \n'''\n\nn = [44]\nprint \"n = \" , n , \", &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nppn = pn\n\nprint \" ppn = \" , hex(id(hex(id(ppn)))) \nprint \" &ppn = \" , hex(id(hex(id(hex(id(ppn))))))\nprint \" *ppn = \" , hex(id(ppn)) \nprint \"**ppn = \" , ppn \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] , &n = 0x9bf05ac\n\tpn = 0x9bf05ac ,\n &pn = 0x9c58160\n*pn = [44]\n ppn = 0x9c58680\n &ppn = 0x9c58160\n *ppn = 0x9bf05ac\n**ppn = [44]\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.7\n'''\n\nn = [44]\nprint \"n = \" , n , \"\\n &n = \" , hex(id(n))\npn = n\nprint \"\\tpn = \" , hex(id(pn)) , \",\\n &pn = \" , hex(id(hex(id(pn))))\nprint \"*pn = \" , pn\nnn = pn\nprint \" ppn = \" , hex(id(nn))\nprint \" &ppn = \" , hex(id(hex(id(nn))))\nrpn = pn\nprint \" ppn = \" , hex(id(rpn))\nprint \" &ppn = \" , hex(id(hex(id(rpn))))\n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "n = [44] \n &n = 0x9bf60ec\n\tpn = 0x9bf60ec ,\n &pn = 0x9bf0e40\n*pn = [44]\n ppn = 0x9bf60ec\n &ppn = 0x9bf0e40\n ppn = 0x9bf60ec\n &ppn = 0x9bf0f20\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.8\n'''\n\ndef max_(m,n):\n if m>n:\n return m\n else:\n return n\n\nm = 44\nn = 22\nprint m , \", \" , n , \", \" , max_(m,n)\nm = max_(m,n) \nm = 55\n# changes the value of m from 44 to 55\nprint m , \", \" , n , \", \" , max_(m,n) \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "44 , 22 , 44\n55 , 22 , 55\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.9 \n'''\n\nv = []\nfor k in range(1,5):\n v.append(1.0/k)\n\nfor i in range(4):\n print \"v[\" , i , \"] = \" , v[i]\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "v[ 0 ] = 1.0\nv[ 1 ] = 0.5\nv[ 2 ] = 0.333333333333\nv[ 3 ] = 0.25\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.10 \n'''\nimport sys\na = [22, 33, 44]\n\nprint \"a = \" , hex(id(a))\nprint \"sizeof(int) = \" , sys.getsizeof(1) \ns = 0\nfor i in a:\n s += i\n print \"\\t i = \" , hex(id(i)),\n print \"\\t *i = \" , i,\n print \"\\t sum = \" , s\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 0x9bf688c\nsizeof(int) = 12\n\t i = 0x8fc0ff4 \t *i = 22 \t sum = 22\n\t i = 0x8fc0f70 \t *i = 33 \t sum = 55\n\t i = 0x8fc0eec \t *i = 44 \t sum = 99\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.11 Examining the Addresses of Array Elements\n'''\n\na = [22, 33, 44, 55, 66]\nprint \"a = \" , hex(id(a)) , \", *a = \" , a[0] \nfor p in a:\n print \"p = \" , hex(id(p)) , \", *p = \" , p \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "a = 0x9c6526c , *a = 22\np = 0x8fc0ff4 , *p = 22\np = 0x8fc0f70 , *p = 33\np = 0x8fc0eec , *p = 44\np = 0x8fc0e68 , *p = 55\np = 0x8fc0de4 , *p = 66\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.12 Pattern Matching\n'''\n\ndef loc(a1,a2,n1,n2):\n p = []\n for element in a2:\n if element in a1:\n p.append(element)\n return p\n\na1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]\na2 = [11, 11, 11, 22, 33]\nprint \"Array a1 begins at location\\t\" , hex(id(a1 ))\nprint \"Array a2 begins at location\\t\" , hex(id(a2)) \np = loc(a1, a2, 9, 5)\nif (p):\n print \"Array a2 found at location\\t\" , hex(id(p))\n for i in range(len(p)):\n print \"\\t\" , hex(id(p[i])) , \": \" , p[i], \"\\t\" , hex(id(a2[i])) , \": \" , a2[i] \nelse:\n print \"Not found.\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Array a1 begins at location\t0x9bea56c\nArray a2 begins at location\t0x9bea62c\nArray a2 found at location\t0x9bea6cc\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc1078 : 11 \t0x8fc1078 : 11\n\t0x8fc0ff4 : 22 \t0x8fc0ff4 : 22\n\t0x8fc0f70 : 33 \t0x8fc0f70 : 33\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.15 Using Dynamic Arrays\n'''\n\ndef get(a):\n print \"Enter number of items: \"\n n = int(raw_input())\n print \"Enter \" , n , \" items, one per line:\"\n for i in range(n):\n print \"\\t\" , i+1 , \": \",\n a.append(float(raw_input()))\n\ndef print_(a):\n for i in range(len(a)):\n print a[i] ,\n print ''\n\na = []\nget(a)\nprint_(a)\na = []\nget(a)\nprint_(a)\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter number of items: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 4 items, one per line:\n\t1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44.4\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "77.7\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t3 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22.2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t4 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "88.8\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 44.4 77.7 22.2 88.8 \nEnter number of items: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "2\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter 2 items, one per line:\n\t1 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "3.33\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " \t2 : " + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "9.99\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": " 3.33 9.99 \n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.17 Indirect Bubble Sort\n'''\n\ndef sort(p, n):\n for i in range(1,n):\n for j in range(n-i):\n if (p[j] > p[j+1]):\n p[j],p[j+1] = p[j+1],p[j]\n", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 7.18 The Sum of a Function\n'''\n\ndef sum_(k,n):\n # returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):\n s = 0\n for i in range(1,n+1):\n s += k(i)\n return s\n\ndef square(k):\n return k*k\n\ndef cube(k):\n return k*k*k\n\n\nprint sum_(square,4) # 1 + 4 + 9 + 16\nprint sum_(cube,4) # 1 + 8 + 27 + 64\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "30\n100\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch8.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch8.ipynb new file mode 100644 index 00000000..033f6e3f --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch8.ipynb @@ -0,0 +1,492 @@ +{ + "metadata": { + "name": "ch8" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "\"\"\"\nEXAMPLE 8.1\n\"\"\"\n\nn= [44] # n holds the int 44\nprint \"int n=44; // n holds the int 44:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\npn = n \nprint \"int* pn=&n; // pn holds the address of n:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\n\npn[0] = 77 # changes the value of n to 77\nprint \"*pn = 77; // changes the value of n to 77:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\n\nq = n \nprint \"int* q=&n; // q also holds the address of n:\\n\";\nprint \"\\t\\t n = \" , n \nprint \"\\t\\t &n = \" , hex(id(n))\nprint \"\\t\\t pn = \" , hex(id(pn)) \nprint \"\\t\\t &pn = \" , hex(id(hex(id(pn))))\nprint \"\\t\\t *pn = \" , pn\nprint \"\\t\\t q = \" , hex(id(q))\nprint \"\\t\\t &q = \" , hex(id(hex(id(hex(id(pn))))))\nprint \"\\t\\t *q = \" , q \n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "int n=44; // n holds the int 44:\n\n\t\t n = [44]\n\t\t &n = 0x9bfb92c\nint* pn=&n; // pn holds the address of n:\n\n\t\t n = [44]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9bf5aa0\n\t\t *pn = [44]\n*pn = 77; // changes the value of n to 77:\n\n\t\t n = [77]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9c6a760\n\t\t *pn = [77]\nint* q=&n; // q also holds the address of n:\n\n\t\t n = [77]\n\t\t &n = 0x9bfb92c\n\t\t pn = 0x9bfb92c\n\t\t &pn = 0x9bf5c80\n\t\t *pn = [77]\n\t\t q = 0x9bfb92c\n\t\t &q = 0x9c6a760\n\t\t *q = [77]\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.2\nNote : Python does not append '\\0' at the end of string.\n'''\n\ns = \"ABCD\"\nfor i in range(4):\n print \"s[\" , i , \"] = '\" , s[i] , \"'\\n\";\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s[ 0 ] = ' A '\n\ns[ 1 ] = ' B '\n\ns[ 2 ] = ' C '\n\ns[ 3 ] = ' D '\n\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.3\n'''\n\nwhile True:\n word = raw_input()\n if len(word) < 2:\n break\n l = word.split(' ')\n for i in l:\n print '\\t\"' , i , '\"'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Today's date is March 12, 2000.\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t\" Today's \"\n\t\" date \"\n\t\" is \"\n\t\" March \"\n\t\" 12, \"\n\t\" 2000. \"\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Tomorrow is Monday.\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t\" Tomorrow \"\n\t\" is \"\n\t\" Monday. \"\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.4\nThis program echoes the input, line by line:\n'''\n\nwhile True:\n line = raw_input()\n if len(line) < 2:\n break\n print \"\\t[\" , line , \"]\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Once upon a midnight dreary, while I pondered, weak and weary, ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Over a many quaint and curious volume of forgotten lore,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Over a many quaint and curious volume of forgotten lore, ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.5\n'''\n\n\nwhile True:\n word = raw_input()\n if len(word) < 2:\n break\n l = word.split(',')\n for i in range(len(l)-1):\n print '\\t[' , l[i] , ']'\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Once upon a midnight dreary ]\n\t[ while I pondered ]\n\t[ weak and weary ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Over a many quaint and curious volume of forgotten lore,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t[ Over a many quaint and curious volume of forgotten lore ]\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.6 \nThis program counts the number of occurrences of the letter 'e' in the input stream. \n'''\n\ncount = 0\nwhile True:\n a = raw_input()\n if len(a) < 1:\n break\n for ch in a:\n if (ch == 'e'): count+=1\n \nprint count , \" e's were counted.\\n\"\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Once upon a midnight dreary, while I pondered, weak and weary,\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Over many a quaint and curious volume of forgotten lore,\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "11 e's were counted.\n\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.7 \nThis program echoes the input stream, capitalizing each word:\n'''\n\nwhile True:\n a = raw_input()\n if len(a) < 1:\n break\n print a.title()\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Fourscore and seven years ago our fathers\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Fourscore And Seven Years Ago Our Fathers\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "brought forth upon this continent a new nation,\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "Brought Forth Upon This Continent A New Nation,\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.8\nThis tests a function that extracts the integers from the input stream:\n'''\n\na = raw_input()\nl = a.split(' ')\nnos = []\nfor i in l:\n try:\n i = int(i)\n nos.append(i)\n except:\n continue\nm = nos[0]\nn = nos[1] \nprint m , \" + \" , n , \" = \" , m+n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "what is 305 plus 9416 ?\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "305 + 9416 = 9721\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.10 An Array of Strings\n'''\n\nname = []\ncount=0\n\nprint \"Enter at most 4 names with at most 19 characters:\\n\";\nwhile (True):\n n = raw_input()\n if len(n) < 1:\n break\n name.append(n)\n count += 1\n \nprint \"The names are:\\n\"\nfor i in range(count):\n print \"\\t\" , i , \". [\" , name[i] , \"]\" \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter at most 4 names with at most 19 characters:\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "George Washington\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "John Adams\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Thomas Jefferson\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.11 A String Array\n'''\n\nname = []\ncount=0\n\nprint \"Enter at most 4 names with at most 19 characters:\\n\";\nwhile (True):\n n = raw_input()\n if len(n) < 1:\n break\n name.append(n)\n count += 1\n \nprint \"The names are:\\n\"\nfor i in range(count):\n print \"\\t\" , i , \". [\" , name[i] , \"]\" ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter at most 4 names with at most 19 characters:\n\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "George Washington\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "John Adams\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Thomas Jefferson\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.12 Initializing a String Array\nThis program is nearly equivalent to those in the previous two examples. It initializes the array/list\nname and then prints its contents:\n'''\n\nname = [ \"George Washington\", \"John Adams\", \"Thomas Jefferson\"]\nprint \"The names are:\\n\"\nfor i in range(3):\n print \"\\t\" , i , \". [\" , name[i] , \"]\"\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The names are:\n\n\t0 . [ George Washington ]\n\t1 . [ John Adams ]\n\t2 . [ Thomas Jefferson ]\n" + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.13 The len() Function\n'''\n\ns = \"ABCDEFG\"\nprint \"len(\" , s , \") = \" , len(s) \nprint \"len(\\\"\\\") = \" , len(\"\")\nprint \"Enter string: \"\nb = raw_input()\nprint \"len(\" , b , \") = \" , len(b)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "len( ABCDEFG ) = 7\nlen(\"\") = 0\nEnter string: \n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "hello how are you !!!\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "len( hello how are you !!! ) = 21\n" + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.14 \ngives index of substring.\n'''\ns = \"The Mississippi is a long river.\"\nprint 's = \"' , s , '\"'\np = s.find(' ')\nprint \"find(s, ' ') points to s[\" , p , \"].\"\np = s.find('s')\nprint \"find(s, 's') points to s[\" , p , \"].\"\np = s.rfind('s')\nprint \"reverse find(s, 's') points to s[\" , p , \"].\"\np = s.find(\"is\")\nprint \"strstr(s, \\\"is\\\") points to s[\" , p , \"].\"\np = s.find(\"isi\")\nif p== -1:\n print 's.find(\"isi\") returns NULL'\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s = \" The Mississippi is a long river. \"\nfind(s, ' ') points to s[ 3 ].\nfind(s, 's') points to s[ 6 ].\nreverse find(s, 's') points to s[ 17 ].\nstrstr(s, \"is\") points to s[ 5 ].\ns.find(\"isi\") returns NULL\n" + } + ], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.15 string copy\n'''\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before strcpy(s1,s2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 = s2\nprint \"After strcpy(s1,s2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before strcpy(s1,s2):\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter strcpy(s1,s2):\n\n\ts1 = [ XYZ ], length = 3\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.16 string copy\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before strcpy(s1,s2,2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 = s2[:2] + s1[2:]\nprint \"After strcpy(s1,s2,2):\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \n\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before strcpy(s1,s2,2):\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter strcpy(s1,s2,2):\n\n\ts1 = [ XYCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.17 The String Concatenation\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before string concatination :\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 += s2\nprint \"After string concatination :\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before string concatination :\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter string concatination :\n\ts1 = [ ABCDEFGXYZ ], length = 10\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 19 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.18 The Second String Concatenation (no. of characters)\n'''\n\ns1 = \"ABCDEFG\"\ns2 = \"XYZ\" \nprint \"Before string concatination :\\n\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) \ns1 += s2[:2]\nprint \"After string concatination :\" \nprint \"\\ts1 = [\" , s1 , \"], length = \" , len(s1) \nprint \"\\ts2 = [\" , s2 , \"], length = \" , len(s2) ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Before string concatination :\n\n\ts1 = [ ABCDEFG ], length = 7\n\ts2 = [ XYZ ], length = 3\nAfter string concatination :\n\ts1 = [ ABCDEFGXY ], length = 9\n\ts2 = [ XYZ ], length = 3\n" + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.19 The String Tokenize \n'''\ns = \"Today's date is March 12, 2000.\"\n\nprint \"The string is: [\" , s , \"] \\nIts tokens are: \"\np = s.split(\" \")\n\nfor i in p:\n print \"\\t[\" , i , \"] \"\n\nprint \"Now the string is: [\" , p[0] , \"] \";\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "The string is: [ Today's date is March 12, 2000. ] \nIts tokens are: \n\t[ Today's ] \n\t[ date ] \n\t[ is ] \n\t[ March ] \n\t[ 12, ] \n\t[ 2000. ] \nNow the string is: [ Today's ] \n" + } + ], + "prompt_number": 21 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 8.20 The strpbrk() Function\nC provides inbuilt strpbrk(), Here i have written mine.\n'''\n\ndef strpbrk(s,s1):\n found = []\n for i in range(len(s1)):\n if s1[i] in s:\n index = s.find(s1[i])\n found.append(index)\n if found:\n return min(found)\n return None\n \n\ns = \"The Mississippi is a long river.\"\nprint 's = \"' , s , '\"'\np = strpbrk(s, \"nopqr\")\nprint 'strpbrk(s, \"nopqr\") points to s[' , p , \"].\"\np = strpbrk(s, \"NOPQR\")\nif (p == None):\n print 'strpbrk(s, \"NOPQR\") returns NULL.\\n'", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s = \" The Mississippi is a long river. \"\nstrpbrk(s, \"nopqr\") points to s[ 12 ].\nstrpbrk(s, \"NOPQR\") returns NULL.\n\n" + } + ], + "prompt_number": 22 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/ch9.ipynb b/Schaum's_Outlines_-_Programming_with_C++/ch9.ipynb new file mode 100644 index 00000000..fabb9a56 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/ch9.ipynb @@ -0,0 +1,232 @@ +{ + "metadata": { + "name": "ch9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.2 Using the Extraction Operation to Control a Loop\n'''\n\n\nwhile True:\n try:\n n = int(raw_input())\n print \"n = \" , n \n except:\n break", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "46\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 46\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "22\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 22\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "44\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 44\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "66\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 66\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "88\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "n = 88\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "33,\n" + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.4 Inputting Strings with the raw_input() Function\nThis program shows how to read text data line-by-line into an array \n'''\n\nking = [] # defines king to be an array \nn=0\nwhile True:\n name = raw_input()\n if len(name) < 1:\n break\n king.append(name)\n n += 1\n# now n == the number of names read\nfor i in range(n):\n print '\\t' , i+1 , \". \" , king[i] ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Kenneth II (971-995)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Constantine III (995-997)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Kenneth III (997-1005)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Malcolm II (1005-1034)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Duncan I (1034-1040)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Macbeth (1040-1057)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Lulach (1057-1058)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "Malcolm III (1058-1093)\n" + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": "\n" + }, + { + "output_type": "stream", + "stream": "stdout", + "text": "\t1 . Kenneth II (971-995)\n\t2 . Constantine III (995-997)\n\t3 . Kenneth III (997-1005)\n\t4 . Malcolm II (1005-1034)\n\t5 . Duncan I (1034-1040)\n\t6 . Macbeth (1040-1057)\n\t7 . Lulach (1057-1058)\n\t8 . Malcolm III (1058-1093)\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.6 Capitalizing All the Words in a Text File\nHere is a complete program that reads words from the external file named input.txt, capitalizes\nthem, and then writes them to the external file named output.txt:\nNote : Working good , you need input.txt in same directory.\n'''\n\ninfile = open(\"input.txt\",\"r\")\noutfile = open(\"output.txt\",\"w\")\n\nfor i in infile:\n s = i.title()\n outfile.write(s)\n\ninfile.close()\noutfile.close()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IOError", + "evalue": "[Errno 2] No such file or directory: 'input.txt'", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-3-60ee428ecf4c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m '''\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0minfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"input.txt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[0moutfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"output.txt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"w\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'input.txt'" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.7 Merging Two Sorted Data Files\n'''\n\nfin1 = open(\"north.dat\",\"r\")\nfin2 = open(\"south.dat\",\"r\")\nfout = open(\"combined.dat\",\"w\")\n\nfile1 = []\nfile2 = []\nfor i in fin1:\n try:\n s = i.split(\" \")\n for j in s:\n file1.append(int(j))\n except:\n continue\n \nfor i in fin2:\n try:\n s = i.split(\" \")\n for j in s:\n file2.append(int(j))\n except:\n continue\n\n\nfor i in sorted(file1 + file2):\n fout.write(str(i) + \" \")\n\nfin1.close()\nfin2.close()\nfout.close()", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "IOError", + "evalue": "[Errno 2] No such file or directory: 'north.dat'", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-4-e14718f30215>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m '''\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mfin1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"north.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mfin2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"south.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mfout\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"combined.dat\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"w\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'north.dat'" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.8 Using an Output String Stream\nThis program creates four objects: a character string s,\nan integer n, a floating-point number x, and an output string stream oss:\n'''\n\ndef print_(oss):\n print 'oss.str() = \"' , str(oss) , '\"'\n\ns=\"ABCDEFG\"\nn=33\nx=2.718\nl = ''\nprint_(l)\nl += s\nprint_(l)\nl += ( \" \" + str(n) )\nprint_(l)\nl += ( \" \" + str(x) )\nprint_(l)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "oss.str() = \" \"\noss.str() = \" ABCDEFG \"\noss.str() = \" ABCDEFG 33 \"\noss.str() = \" ABCDEFG 33 2.718 \"\n" + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nEXAMPLE 9.9 Using an Input String Stream iss ABCDEFG 44 3.14 istringstream\nThis program is similar to the one in Example 9.8 \nexcept that it reads from an input string stream iss\ninstead of writing to an output string stream.:\n'''\n'''\nEXAMPLE 9.8 Using an Output String Stream\nThis program creates four objects: a character string s,\nan integer n, a floating-point number x, and an output string stream oss:\n'''\n\ndef print_(iss,s='',n=0,x=0.0):\n print 's = \"' , s , '\", n = ' , n , \", x = \" , x, ', iss.str() = \"' \\\n , iss , '\"' \n\ns=\"\"\nn=0\nx=0.0\nl = ''\niss = \"ABCDEFG 44 3.14\"\nprint_(iss)\ns = \"ABCDEFG\"\nprint_(iss,s)\nn = 44\nprint_(iss,s,n)\nx = 3.14\nprint_(iss,s,n,x)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "s = \" \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 0 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 44 , x = 0.0 , iss.str() = \" ABCDEFG 44 3.14 \"\ns = \" ABCDEFG \", n = 44 , x = 3.14 , iss.str() = \" ABCDEFG 44 3.14 \"\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Schaum's_Outlines_-_Programming_with_C++/screenshots/ratio.png b/Schaum's_Outlines_-_Programming_with_C++/screenshots/ratio.png Binary files differnew file mode 100644 index 00000000..68bb8cd0 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/screenshots/ratio.png diff --git a/Schaum's_Outlines_-_Programming_with_C++/screenshots/swap.png b/Schaum's_Outlines_-_Programming_with_C++/screenshots/swap.png Binary files differnew file mode 100644 index 00000000..7d707e37 --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/screenshots/swap.png diff --git a/Schaum's_Outlines_-_Programming_with_C++/screenshots/vector.png b/Schaum's_Outlines_-_Programming_with_C++/screenshots/vector.png Binary files differnew file mode 100644 index 00000000..b51554ce --- /dev/null +++ b/Schaum's_Outlines_-_Programming_with_C++/screenshots/vector.png |