Академический Документы
Профессиональный Документы
Культура Документы
Python 3
Trusted
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
Run
In [1]:
s = "this is good"
t = True
a=7
b=6
f = 9.7
print("hello sum", a+b)
hello sum 13
In [3]:
s = "this is good"
t = True
a=7
b=6
f = 9.7
print("hello sum", a+b)
d=4
hello sum 13
In [ ]:
In [4]:
s = "this is good"
t = True
a=7
b=6
f = 9.7
#print("hello sum", a+b)
In [5]:
if a > 10:
print("ok")
else:
print("not ok")
not ok
In [6]:
for i in range(1,4):
for j in range(1,4):
print(i)
1
1
1
2
2
2
3
3
3
In [11]:
for i in range(1,4):
for j in range(1,4):
print(i, end = "i=2")
1i=21i=21i=22i=22i=22i=23i=23i=23i=2
In [12]:
for i in range(1,4):
for j in range(1,4):
print(i, end = "")
111222333
In [13]:
for i in range(1,4):
for j in range(1,4):
print(i, end = "")
print("\n")
111
222
333
In [14]:
for i in range(1,4):
print("\n")
for j in range(1,4):
print(i, end = "")
111
222
333
In [15]:
s = lambda x: x*x
print(s(5))
25
In [16]:
s = lambda x,y: x+y
print(s(2,3))
5
In [17]:
try:
a=9
b=0
print("the div is", a/b)
except Exception as e:
print("Error!", e)
Error! division by zero
In [18]:
try:
a=9
b=0
print("the div is", b/a)
except Exception as e:
print("Error!", e)
the div is 0.0
In [9]:
import numpy as np
In [2]:
l = [2,3,5,6]
np.array(l)
Out[2]:
array([2, 3, 5, 6])
In [3]:
np.array(my_matrix)
Out[4]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [9]:
np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
Out[9]:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
In [12]:
np.arange(0,10)
Out[12]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [7]:
np.zeros(4)
Out[7]:
array([0., 0., 0., 0.])
In [8]:
np.ones(3)
Out[8]:
array([1., 1., 1.])
In [10]:
np.zeros(5,5)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-0be2ed1d2c69> in <module>
----> 1 np.zeros(5,5)
In [11]:
np.zeros((5,5))
Out[11]:
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
In [12]:
np.linspace(0,10,4)
Out[12]:
array([ 0. , 3.33333333, 6.66666667, 10. ])
In [13]:
np.linspace(0.10,6)
Out[13]:
array([0.1 , 0.22040816, 0.34081633, 0.46122449, 0.58163265,
0.70204082, 0.82244898, 0.94285714, 1.06326531, 1.18367347,
1.30408163, 1.4244898 , 1.54489796, 1.66530612, 1.78571429,
1.90612245, 2.02653061, 2.14693878, 2.26734694, 2.3877551 ,
2.50816327, 2.62857143, 2.74897959, 2.86938776, 2.98979592,
3.11020408, 3.23061224, 3.35102041, 3.47142857, 3.59183673,
3.7122449 , 3.83265306, 3.95306122, 4.07346939, 4.19387755,
4.31428571, 4.43469388, 4.55510204, 4.6755102 , 4.79591837,
4.91632653, 5.03673469, 5.15714286, 5.27755102, 5.39795918,
5.51836735, 5.63877551, 5.75918367, 5.87959184, 6. ])
In [14]:
np.linspace(0,10,6)
Out[14]:
array([ 0., 2., 4., 6., 8., 10.])
In [3]:
import random
In [7]:
random.randint(1,10)
Out[7]:
4
In [8]:
np.random.rand(4)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-63d0acad6bb4> in <module>
----> 1 np.random.rand(4)
In [10]:
np.random.rand(7)
Out[10]:
array([0.84026051, 0.68889086, 0.52492321, 0.43262344, 0.06019164,
0.22666551, 0.75850704])
In [23]:
np.random.rand(5,5)
Out[23]:
array([[0.63319009, 0.05468459, 0.86719802, 0.57467522, 0.79109165],
[0.73134838, 0.0800012 , 0.48351881, 0.90437749, 0.83405371],
[0.35944379, 0.19996895, 0.49880596, 0.54937713, 0.33440026],
[0.6628148 , 0.1358355 , 0.02842624, 0.20352366, 0.0729804 ],
[0.97083834, 0.62168946, 0.86759173, 0.23519234, 0.58733608]])
In [24]:
np.random.randn(5,5)
Out[24]:
array([[ 1.04926831, 1.39542519, 0.86472576, 0.06007758, -0.54405796],
[-0.45113187, -0.79282517, -0.14487855, -0.81817126, -0.57137778],
[-0.25792585, 0.03132514, 0.83200862, 0.53485948, -2.15863243],
[ 1.93795039, 1.00533897, -0.77763965, 1.69860581, -1.17202323],
[-0.23895415, 1.07441575, 0.17345031, 1.62337034, 0.63651691]])
In [29]:
np.random.randint(1,1999)
Out[29]:
1255
In [30]:
np.arange(12).reshape(3,4)
Out[30]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [31]:
In [32]:
In [33]:
d=np.arange(12).reshape(3,4)
In [34]:
d
Out[34]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [35]:
d.reshape(12)
Out[35]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
In [ ]:
In [37]:
arr = np.arange(0,11)
In [39]:
arr
Out[39]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
In [40]:
arr[6]
Out[40]:
6
In [41]:
arr[3:7]
Out[41]:
array([3, 4, 5, 6])
In [42]:
arr[:7]
Out[42]:
array([0, 1, 2, 3, 4, 5, 6])
In [43]:
arr[7:]
Out[43]:
array([ 7, 8, 9, 10])
In [46]:
import numpy as np
In [4]:
t = np.array([[3,4,5],[3,5,6],[3,5,67]])
In [23]:
t
Out[23]:
array([[ 3, 4, 5],
[ 3, 5, 6],
[ 3, 5, 67]])
In [25]:
t[1:2,1:3] # row 1 too 2 excluding 2, column 1 to 3 exclusing 3;(start counting from 0 --> [0,1,2,3) last point is
excluded)
Out[26]:
array([[5, 6]])
In [4]:
a
Out[17]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [18]:
a+a
Out[18]:
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18])
In [19]:
b=np.arange(1,6)
In [20]:
ValueError: operands could not be broadcast together with shapes (9,) (5,)
In [27]:
import pandas as pd
In [38]:
my_list = [10,20,30]
In [30]:
arr=np.array([10,20,30])
In [32]:
pd.Series(data=my_list)
Out[32]:
0 10
1 20
2 30
dtype: int64
In [33]:
pd.Series(data=my_list,index = [1,2,3])
Out[33]:
1 10
2 20
3 30
dtype: int64
In [39]:
pd.Series(data=labels)
Out[39]:
0 a
1 b
2 c
dtype: object
In [40]:
pd.Series(data=arr)
Out[40]:
0 10
1 20
2 30
dtype: int32
In [36]:
d={'a':10,'b':20,'c':30}
In [41]:
pd.Series(data=d)
Out[41]:
a 10
b 20
c 30
dtype: int64
In [42]:
pd.Series(d)
Out[42]:
a 10
b 20
c 30
dtype: int64
In [43]:
pd.Series(arr,labels)
Out[43]:
a 10
b 20
c 30
dtype: int32
In [44]:
df
Out[49]:
W X Y Z
In [50]:
W X Y Z
In [54]:
df['W']
Out[54]:
A -0.482512
B 0.526326
C -0.573692
D -2.121619
E -1.287729
Name: W, dtype: float64
In [56]:
df[['W','Z']]
Out[56]:
W Z
A -0.482512 -0.071771
B 0.526326 -0.723881
C -0.573692 1.457442
D -2.121619 -0.340125
E -1.287729 1.417590
In [58]:
df
Out[60]:
W X Y Z new
In [61]:
df.drop('new', axis=1) #removing a column. axis = 1 means delete a column. axis = 0 means delete a row
Out[61]:
W X Y Z
In [63]:
In [64]:
df # see in original no change. Hence use 'inplace = True' for permanent change
Out[64]:
W X Y Z new
In [65]:
W X Y Z new
In [67]:
df.loc['B', 'Y']
Out[69]:
0.10989162475388488
In [72]:
In [ ]: