0% found this document useful (0 votes)
7 views19 pages

2021-civ-73-part-2 python

...

Uploaded by

Nouman Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views19 pages

2021-civ-73-part-2 python

...

Uploaded by

Nouman Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

2021-civ-73-part-2

November 21, 2024

[4]: for i in range (1,101):


if i % 2 == 0:
print(i,end=' ')

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56
58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

[2]: for i in range (1,101):


if i % 2 != 0:
print(i,end=' ')

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55
57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

[3]: for i in range (1,1000):


if i%3==0 and i%5==0:
print (i,end=' ')

15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 240 255 270 285 300 315
330 345 360 375 390 405 420 435 450 465 480 495 510 525 540 555 570 585 600 615
630 645 660 675 690 705 720 735 750 765 780 795 810 825 840 855 870 885 900 915
930 945 960 975 990

[10]: for i in range (1,101):


for j in range (1,101):
if i == j**2:
print (i, end=' ')

1 4 9 16 25 36 49 64 81 100

[11]: for i in range (1,101):


for j in range (1,101):
if i == j**3:
print (i, end=' ')

1 8 27 64

1
[9]: #Printing Prime No.s from 1 to 100
for i in range (1,101):
x = 0
for j in range (1,101):
if i%j == 0:
x = x+1
if x == 2:
print (i,end=' ')

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

[23]: #Evaluating the no. is prime or Not


i = int(input('Enter a No: '))
x = 0
for j in range (1,i+1):
if i%j == 0:
x = x+1

if x == 2:
print ('Entered No. is a Prime No.')
else:
print ('Entered No. is Not a Prime No.')

Enter a No: 4
Entered No. is Not a Prime No.

[24]: #Python library numpy using


import numpy as np
x = np.array([4,5,6])
y = np.array([1,2,3])
z1 = x + y
z2 = np.add(x,y)
print('usingg sign:',z1)
print('using function:',z2)

usingg sign: [5 7 9]
using function: [5 7 9]

[37]: #Calculating trigonometry values


import numpy as np
x = np.array([0, 45, 90])
y = np.radians(x)
z1 = np.round(np.sin (y), 2)
z2 = np.round(np.cos (y), 2)
z3 = np.round(np.tan (y), 2)
print ('sin: ', z1)
print ('cos: ', z2)

2
print ('tan: ', z3)

sin: [0. 0.71 1. ]


cos: [1. 0.71 0. ]
tan: [0.00000000e+00 1.00000000e+00 1.63312394e+16]

[5]: import numpy as np


x = np.array([1.245, 9.3399, 10.1122])
y = np.round(x,2)
z = np.ceil(x)
w = np.floor(x)
print (x)
print (y)
print (z)
print (w)

[ 1.245 9.3399 10.1122]


[ 1.25 9.34 10.11]
[ 2. 10. 11.]
[ 1. 9. 10.]

[42]: #marking 2x2 matrix in array


import numpy as np
x = np.array([[1,2],[3,4]])
print(x)

[[1 2]
[3 4]]

[47]: #marking 2x2 matrix in 1-Dimensional array


import numpy as np
x = np.array([[1,2],[3,4]])
y = np.flaten(x)
print(y)

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[47], line 4
2 import numpy as np
3 x = np.array([[1,2],[3,4]])
----> 4 y = np.flaten(x)
5 print(y)

File D:\Anaconda\Lib\site-packages\numpy\__init__.py:333, in __getattr__(attr)


330 "Removed in NumPy 1.25.0"
331 raise RuntimeError("Tester was removed in NumPy 1.25.")
--> 333 raise AttributeError("module {!r} has no attribute "
334 "{!r}".format(__name__, attr))

3
AttributeError: module 'numpy' has no attribute 'flaten'

[49]: import numpy as np


x = np.array([[1,2,3],[4,5,6],[7,8,9]])
y = np.array([[2],[4],[6]])
u = np.transpose(x)
v = np.linalg.inv(x)
w = np.linalg.det(x)
print(u)
print(v)
print(w)
l = np.dot(x,y)
print (l)
#solving for deflection calculation
k = np.linalg.solve(x,y)
print (k)

[[1 4 7]
[2 5 8]
[3 6 9]]
[[ 3.15251974e+15 -6.30503948e+15 3.15251974e+15]
[-6.30503948e+15 1.26100790e+16 -6.30503948e+15]
[ 3.15251974e+15 -6.30503948e+15 3.15251974e+15]]
-9.51619735392994e-16
[[ 28]
[ 64]
[100]]
[[-0.46666667]
[ 0.93333333]
[ 0.2 ]]

[1]: #Array indexing


import numpy as np
x = np.array([7,8,9,10,20])
print (x[2])

[2]: #Array indexing


import numpy as np
x = np.array([7,8,9,10,20])
print (x[2]+x[3])

19

4
[4]: #Array slicing
#lower limit will be included
print (x[1:])

[ 8 9 10 20]

[5]: #upper limit will be exceluded


print (x[:2])

[7 8]

[6]: print (x[1:4])

[ 8 9 10]

[12]: import numpy as np


x = np.array([[1,2,3],[4,5,6]])
print (x[1,1])
print (x[0:2,1])
print (x[0:2,1:3])

5
[2 5]
[[2 3]
[5 6]]

[16]: #Array shape


import numpy as np
x = np.array([[1,2,3],[4,5,6]])
print (x.shape)
print (x.reshape (1,6))
print (x.reshape(3,2))

(2, 3)
[[1 2 3 4 5 6]]
[[1 2]
[3 4]
[5 6]]

[6]: #Array joining on right side (h for horizontally)


import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([[10,20],[30,40]])
z1 = np.hstack((x,y))
print (z1)

[[ 1 2 10 20]
[ 3 4 30 40]]

5
[7]: #Array joining on bottom (v for vertically)
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([[10,20],[30,40]])
z2 = np.vstack((x,y))
print (z2)

[[ 1 2]
[ 3 4]
[10 20]
[30 40]]

[23]: #Matplotlib.pyplot
# Matplotlib is the plotting library for python and numpy
# Pyplot is a module of matplotlib that provides MATLAB like interface
# plot a graph for x = [1,2,3,4], y = [1,4,9,16]
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,2,3,4],[1,4,9,16],'ro')

[23]: [<matplotlib.lines.Line2D at 0x1630580aab0>]

6
[25]: plt.plot([1,2,3,4],[1,4,9,16],'r-')

[25]: [<matplotlib.lines.Line2D at 0x163058ee480>]

[28]: plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()
plt.plot([1,2,3,4],[1,4,9,16],'b-')

7
[28]: [<matplotlib.lines.Line2D at 0x16307159370>]

8
[29]: plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.plot([1,2,3,4],[1,4,9,16],'b-')

[29]: [<matplotlib.lines.Line2D at 0x1630718eed0>]

9
[30]: plt.plot([1,2,2,1,1],[1,1,4,4,1],'g-')

[30]: [<matplotlib.lines.Line2D at 0x163071c3620>]

10
[56]: #Functions used to space intervals
# Linspace
# x = np.linspace(3,18,6)
# 3 is lower limit, 18 is upper limit, 6 equally spaced No.s
# Space = (UL-LL)/(6-1)
x = np.linspace (3,27,7)
print (x)

[ 3. 7. 11. 15. 19. 23. 27.]

[37]: #Arrange
import numpy as np
x = np.arange(0,12,2)
print (x)
x = np.arange(5,20,3)
print (x)

[ 0 2 4 6 8 10]
[ 5 8 11 14 17]

[38]: #Zeros-Like function


# same shape array of y as that of x

11
x = np.array([1,2,3,4])
y = np.zeros_like (x)
print (y)

[0 0 0 0]

[46]: #plot a graph of quadratic Eq.


a = 2
b = 5
c = 1
x = np.arange(-100,100,5)
y = a*x**2 + b*x + c
plt.plot(y,'ro')
plt.plot(y,'g-')
plt.title('Graph')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)

12
[57]: #plot a graph of quadratic Eq.
x = np.arange(0,360+0.1,0.1)
pi = 3.1415
y = np.sin(x*pi/180)
plt.plot(y,'ro')
plt.plot(y,'g-')
plt.title('Sin Curve')
plt.xlabel('@')
plt.ylabel('y')
plt.grid(True)

[64]: #Make a SFD of beam having length 6m, Point load = 2KN at center (a=3)
import numpy as np
import matplotlib.pyplot as plt
L = 6
p = 2
a = 3
x = np.arange(0,L+0.001,0.001)
#zeros like is used to have a variable shape as that of x so that we can stores␣
↪value answer in y

13
y = np.zeros_like(x)
z = np.zeros_like(x)
for i in range (len(x)):
if x[i]==0:
y[i]=0
elif x[i]< a:
y[i] = P/2
elif x[i] >= a and x[i]<L:
y[i] = p/2 - p
elif x[i] ==L:
y[i] = 0
for i in range (len(x)):
z[i] = 0
plt.plot(x,y,'g-')
plt.plot(x,z,'b--')

[64]: [<matplotlib.lines.Line2D at 0x16307a3f260>]

[68]: #Make a BMD of beam having length 6m, Point load = 2KN at center (a=3)
import numpy as np
import matplotlib.pyplot as plt
L = 6

14
p = 2
a = 3
x = np.arange(0,L+0.001,0.001)
#zeros like is used to have a variable shape as that of x so that we can stores␣
↪value answer in y

y = np.zeros_like(x)
z = np.zeros_like(x)
for i in range (len(x)):
if x[i]==0:
y[i]=0
elif x[i] <= a:
y[i] = (P/2)*x[i]
elif x[i] > a:
y[i] = (p/2)*x[i] - p*(x[i]-a)
elif x[i] ==L:
y[i] = 0
for i in range (len(x)):
z[i] = 0
plt.plot(x,y,'g-')
plt.plot(x,z,'b--')

[68]: [<matplotlib.lines.Line2D at 0x16307dfd550>]

15
[ ]: #SFD & BMD for UDL on simply supported beam
#User input the Load and location and we draw SFD& BMD

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

16
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]: print

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

17
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

18
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

19

You might also like