Python Notes
Python Notes
[2]: 2+3
[2]: 5
[3]: 5*6
[3]: 30
[5]: 3/2
[5]: 1.5
[6]: 3//2
[6]: 1
[7]: 5-10
[7]: -5
[8]: 3%2
[8]: 1
Q1) Give the tax is 0.125 and the intial salary is 42,000. Calculate the final tax ?
[19]: tax=0.125
salary = 42000
tax_amt=42000*tax
total_amt=salary-tax_amt
print("The tax is ",tax_amt,"\nThe final salary is",total_amt)
[20]: 2**5
[20]: 32
[24]: print('Cannot\nCan\'t')
1
Cannot
Can't
[33]: a="Python"
print(len(a))
[66]: a=[1,2,3,4,5,6,7]
s=[]
for i in a[:
i*i
s.append(i)
print(s)
File <string>:6
print(s)
^
IndentationError: unindent does not match any outer indentation level
Using while loop print Fibonnacci series first ten numbers Syntax: While condition : statements
[29]: a=0
b=1
n=0
I=int(input("Enter the number of terms till u want"))
print(a,"\n",b)
while n < I:
a,b=b,a+b
print(b)
n=n+1
2
55
89
144
233
377
610
987
If else statement in python if condition 1: statements else : statement
if elif statement if condition 2: statement 1: elif condition 3: statements else statement
1
4
9
64
125
216
343
2.8284271247461903
3.0
3.1622776601683795
3
a, b = b, a + b
print(b, end="\n")
i += 1
fib(143)
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
4
165580141
267914296
433494437
701408733
1134903170
1836311903
2971215073
4807526976
7778742049
12586269025
20365011074
32951280099
53316291173
86267571272
139583862445
225851433717
365435296162
591286729879
956722026041
1548008755920
2504730781961
4052739537881
6557470319842
10610209857723
17167680177565
27777890035288
44945570212853
72723460248141
117669030460994
190392490709135
308061521170129
498454011879264
806515533049393
1304969544928657
2111485077978050
3416454622906707
5527939700884757
8944394323791464
14472334024676221
23416728348467685
37889062373143906
61305790721611591
99194853094755497
160500643816367088
259695496911122585
420196140727489673
679891637638612258
1100087778366101931
5
1779979416004714189
2880067194370816120
4660046610375530309
7540113804746346429
12200160415121876738
19740274219868223167
31940434634990099905
51680708854858323072
83621143489848422977
135301852344706746049
218922995834555169026
354224848179261915075
573147844013817084101
927372692193078999176
1500520536206896083277
2427893228399975082453
3928413764606871165730
6356306993006846248183
10284720757613717413913
16641027750620563662096
26925748508234281076009
43566776258854844738105
70492524767089125814114
114059301025943970552219
184551825793033096366333
298611126818977066918552
483162952612010163284885
781774079430987230203437
1264937032042997393488322
2046711111473984623691759
3311648143516982017180081
5358359254990966640871840
8670007398507948658051921
14028366653498915298923761
22698374052006863956975682
36726740705505779255899443
59425114757512643212875125
96151855463018422468774568
155576970220531065681649693
251728825683549488150424261
407305795904080553832073954
659034621587630041982498215
1066340417491710595814572169
1725375039079340637797070384
2791715456571051233611642553
4517090495650391871408712937
7308805952221443105020355490
11825896447871834976429068427
6
19134702400093278081449423917
30960598847965113057878492344
50095301248058391139327916261
81055900096023504197206408605
131151201344081895336534324866
212207101440105399533740733471
print(factorial(10))
3628800
It is odd
7
5 Create a function with parameter a and return a+2
[18]: def fun(a):
return f"{a+2}"
fun(4)
[18]: '6'
[7]: l=['a','b','a','c','c','d']
u=set(l)
print (u)
2440121 Durgadutt S B
print(max(t),"\n",min(t))
8
Second
x=np.linspace(0,100,20)
# x will be equal to x=[0,5,10,...,95,100]
y= np.sin (x)
# single line code for scatter plot
# plt.plot(x,y,color = 'green', marker ='o')
# dual line
plt.plot(x,y,color= 'violet')
plt.scatter(x,y,color='red')
1
2 Draw a Scatter for the following funet
y =f(x) ={ 9 ,x=0 11 ,x=2 -7 ,x=5 }
x=[0,2,5]
y=[9,11,-7]
# x will be equal to x=[0,5,10,...,95,100]
y= np.sin (x)
# single line code for scatter plot
# plt.plot(x,y,color = 'green', marker ='o')
# dual line
plt.plot(x,y,color= 'violet')
plt.scatter(x,y,linestyle='dotted')
2
[17]: import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(10,50)
y=np.sin(x)
y_2=np.cos(x)
plt.plot(x,y, label= 'sinx')
plt.plot(x,y_2, label = 'cosx')
plt.legend()
plt.show()
3
PLOT E^X AND X^2 IN THE SAME DIAGRAM AND SHOW E^X INCREASES MORE
RAPIDLY THAN x^2
plt.subplot(1,2,1)
x = 4
y1=np.sin(x)
plt.plot(x,y1)
plt.subplot(1,2,2)
x=3
y2=np.cos(x)
plt.plot(x,y2)
plt.show()
4
3 Line Plots
[10]: from mpl_toolkits import mplot3d #allows plt to plot 3D figures
from matplotlib.pyplot import *
from numpy import *
ax=axes(projection = "3d")
ax.set_xlabel("X - axis")
ax.set_ylabel("Y - axis", size = 10, color = "blue")
ax.set_zlabel("Z - axis", fontsize=10)
5
[44]: # draw spiral inside the box
from mpl_toolkits import mplot3d #allows plt to plot 3D figures
from matplotlib.pyplot import *
from numpy import *
ax = axes(projection="3d")
z = linspace(0,15,15)
x = sin(z)
y = cos(z)
#ax.plot3D(x,y,z) # for ploting as a spiral
ax.scatter3D(x,y,z) # for ploting with scatter points
show()
6
[50]: ax = axes(projection = "3d")
t = linspace(-10,10,100)
xval = sin(t)
yval = cos(t)
zval = t
ax.plot3D(xval,yval,zval,"red")
title("Helix")
xlabel("X")
ylabel("Y")
show()
7
4 2.Surface plots
[11]: import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
8
5 3. Surface Plot
[9]: from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(projection="3d") # Correct way to create 3D axes
ax.plot_surface(XX, Y, ZZ)
plt.title("Plane")
ax.set_xlabel("$X$")
ax.set_ylabel("$Y$")
ax.set_zlabel("$Z$")
plt.show()
9
6 Matched Sub Figure
[88]: import numpy as np
import matplotlib.pyplot as plt
plt.subplot(2,2,(1,2))
x=np.linspace(0,10)
y=np.sin(x)
plt.plot(x,y, color = "red" )
plt.grid()
plt.xlabel('x')
plt.ylabel('sinx')
plt.subplot(2,2,3)
x=np.linspace(0,10)
plt.plot(x,np.cos(x),label = 'cos x')
plt.grid()
plt.xlabel('x')
plt.ylabel('cos x')
plt.subplot(2,2,4)
10
x=np.linspace(0,10)
plt.plot(x,np.tan(x),color = "green",label ='tanx')
plt.grid()
plt.xlabel('x')
plt.ylabel('tan x')
plt.ylim(-5,5)
plt.subplots_adjust(wspace=0.5)
plt.show()
11
7 Create a 2*2 grid of 3D plots
12
ax3.set_zlabel("Z-axis")
plt.tight_layout()
plt.show()
[ ]:
13