Welcome to Canopy's interactive data-analysis environment!

Python 2.7.13 |Enthought, Inc. (x86_64)| (default, Mar 2 2017, 16:05:12) [MSC v.1500 64 bit (AMD64)]

IPython 5.3.0 -- An enhanced Interactive Python.

? -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help -> Python's own help system.

object? -> Details about 'object', use 'object??' for extra details.

In [1]: ?

# Dividing two integers:

In [2]: 5/2

Out[2]: 2

In [3]: x = 5

In [4]: type(x)

Out[4]: int

In [5]: 5.0/2

Out[5]: 2.5

In [6]: 5/2.0

Out[6]: 2.5

In [8]: float(5)/2

Out[8]: 2.5

# Other aritmetic:

In [9]: x

Out[9]: 5

In [10]: x**2

Out[10]: 25

In [11]: pow(x, 3)

Out[11]: 125

In [12]: sin(0)

Out[12]: 0.0

# Defining anonymus functions (lambda expressions):

In [13]: f = lambda x: x*x + 2*x + 1

In [14]: f(-1)

Out[14]: 0

In [15]: f(4)

Out[15]: 25

In [16]: f = lambda x,y: x**2 + y**2

In [17]: f(1,1)

Out[17]: 2

# Defining ordinary multi-line functions:

In [18]: def fun1(x, y):

...: z = x**2 + y**2

...: print z

...: return float(z)/3

...:

In [19]: fun1(3,4)

25

Out[19]: 8.333333333333334

# Using result from previous step:

In [20]: _

Out[20]: 8.333333333333334

In [21]: _*3

Out[21]: 25.0

# Checking variable type:

In [22]: type(fun1)

Out[22]: function

In [23]: isinstance(fun1, int)

Out[23]: False

In [26]: isinstance(1, int)

Out[26]: True

# Floating point numbers:

In [27]: 35.0

Out[27]: 35.0

In [29]: float(46)

Out[29]: 46.0

In [30]: x=1

In [31]: type(x)

Out[31]: int

In [32]: y = 2.3

In [33]: type(y)

Out[33]: float

# Complex numbers:

In [34]: z1=2+3j

In [35]: type(z1)

Out[35]: complex

In [36]: z1

Out[36]: (2+3j)

In [37]: z2=1-2j

In [38]: z1+z2

Out[38]: (3+1j)

In [39]: z1-z2

Out[39]: (1+5j)

In [40]: z1**2

Out[40]: (-5+12j)

In [41]: z1.real

Out[41]: 2.0

In [42]: z1.imag

Out[42]: 3.0

In [43]: complex(5, 8)

Out[43]: (5+8j)

In [44]: abs(z1)

Out[44]: 3.605551275463989

# Boolean variables:

In [45]: b1 = 1 > 0

In [46]: b1

Out[46]: True

In [47]: type(b1)

Out[47]: bool

In [48]: b2 = False

In [49]: b3 = True

In [50]: b2

Out[50]: False

In [51]: b3

Out[51]: True

In [1]: b1 = True

In [2]: b2 = bool(0)

In [3]: b2

Out[3]: False

In [4]: b1 and b2

Out[4]: False

In [5]: b1 or b2

Out[5]: True

In [6]: (1 > 0) and (1 < 2)

Out[6]: True

In [7]: not (b1 or b2)

Out[7]: False

In [8]: b1 or b2

Out[8]: True

Out[62]: True

# Strings:

In [63]: s1 = "Hello world!"

In [64]: s2 = 'Hello world!'

In [65]: s1==s2

Out[65]: True

In [66]: type(s1)

Out[66]: str

In [67]: s1[5]

Out[67]: ' '

In [68]: s1[0]

Out[68]: 'H'

In [69]: s1+s2

Out[69]: 'Hello world!Hello world!'

In [70]: s3 = '''First line

...: second line

...: thir line '''

In [71]: s3

Out[71]: 'First line\nsecond line\nthir line '

In [72]: print s3

First line

second line

thir line

# Types conversion :

In [73]: pi

Out[73]: 3.141592653589793

In [74]: t = int(pi)

In [75]: type(t)

Out[75]: int

In [76]: t

Out[76]: 3

In [77]: t1 = float(t)

In [78]: type(t1)

Out[78]: float

In [79]: t1

Out[79]: 3.0

In [80]: str(t1)

Out[80]: '3.0'

In [81]: float('4672356783.35')

Out[81]: 4672356783.35

In [82]: s10 = float('4672356783.35')

In [83]: type(s10)

Out[83]: float

# Pyhton's list structure:

In [85]: ar = [1,2,3,4]

In [86]: type(ar)

Out[86]: list

In [87]: strar=["a", "b", "c"]

In [88]: strar

Out[88]: ['a', 'b', 'c']

In [89]: ar1 = [1,2,3,4]

In [90]: ar2 = [5,7,8]

In [91]: ar1 + ar2

Out[91]: [1, 2, 3, 4, 5, 7, 8]

In [92]: 3*ar

Out[92]: [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

In [93]: ar[6]

IndexErrorTraceback (most recent call last)
<ipython-input-93-eaa96c4760c6> in <module>()
----> 1 ar[6]

IndexError: list index out of range

In [94]: ar[2]

Out[94]: 3

# NumPy arrays:

# One dimension and elementwise operations:

In [96]: a1 = np.array([1, 2, 3, 4])

In [97]: type(a1)

Out[97]: numpy.ndarray

In [98]: a1 = np.array([-1, -2, -3, -4])

In [99]: a1 = np.array([1, 2, 3, 4])

In [100]: a2 = np.array([-1, -2, -3, -4])

In [101]: a1+a2

Out[101]: array([0, 0, 0, 0])

In [102]: a1*a2

Out[102]: array([ -1, -4, -9, -16])

In [103]: a1*2

Out[103]: array([2, 4, 6, 8])

# Two dimensions:

In [104]: array([ [1, 2, 3], [4, 5, 6] ])

Out[104]:

array([[1, 2, 3],

[4, 5, 6]])

In [105]: a = array([ [1, 2, 3], [4, 5, 6] ])

In [106]: a1.ndim

Out[106]: 1

In [107]: a.ndim

Out[107]: 2

In [108]: a.shape

Out[108]: (2L, 3L)

In [109]: a.shape = (3, 2)

In [110]: _

Out[110]: (2L, 3L)

In [111]: a

Out[111]:

array([[1, 2],

[3, 4],

[5, 6]])

In [112]: len(a)

Out[112]: 3

In [114]: range(10)

Out[114]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [115]: arange(10)

Out[115]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [116]: arange(10, 2, 3)

Out[116]: array([], dtype=int32)

In [117]: x = np.arange(1, 9, 2)

In [118]: x

Out[118]: array([1, 3, 5, 7])

In [119]: y = np.linspace(0, 1, 6)

In [120]: y

Out[120]: array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. ])

In [121]: a1

Out[121]: array([1, 2, 3, 4])

In [122]: a2

Out[122]: array([-1, -2, -3, -4])

In [123]: dot(a1, a2)

Out[123]: -30

# Matrices:

In [126]: m1 = matrix([ [1, 2, 3], [4, 5, 6]])

In [127]: m1

Out[127]:

matrix([[1, 2, 3],

[4, 5, 6]])

In [128]: type(m1)

Out[128]: numpy.matrixlib.defmatrix.matrix

In [129]: m2 = matrix([ [1, 2], [3, 4], [5, 6]])

In [130]: m1*m2

Out[130]:

matrix([[22, 28],

[49, 64]])

In [131]: m1.T

Out[131]:

matrix([[1, 4],

[2, 5],

[3, 6]])

In [132]: m1.I

Out[132]:

matrix([[-0.94444444, 0.44444444],

[-0.11111111, 0.11111111],

[ 0.72222222, -0.22222222]])

In [133]: m1.T*5

Out[133]:

matrix([[ 5, 20],

[10, 25],

[15, 30]])

In [135]: m1*m1.I

Out[135]:

matrix([[ 1.00000000e+00, 3.33066907e-16],

[ 0.00000000e+00, 1.00000000e+00]])

In [137]: v1 = zeros(2)

In [138]: v1

Out[138]: array([ 0., 0.])

In [142]: v2 = zeros((2,3))

In [143]: v2

Out[143]:

array([[ 0., 0., 0.],

[ 0., 0., 0.]])

In [144]: eye(5)

Out[144]:

array([[ 1., 0., 0., 0., 0.],

[ 0., 1., 0., 0., 0.],

[ 0., 0., 1., 0., 0.],

[ 0., 0., 0., 1., 0.],

[ 0., 0., 0., 0., 1.]])

In [145]: random.rand(4)

Out[145]: array([ 0.522425 , 0.12708431, 0.67319322, 0.3070614 ])