Welcome to Canopy's interactive data-analysis environment!

Type '?' for more information.

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

Type "copyright", "credits" or "license" for more information.


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.

# Booleans again:


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


# More arrays:

In [9]: v1 = array([1, 2,3])


In [10]: m1 = array([ [1, 2], [2, 3] ])


In [11]: m1

Out[11]:

array([[1, 2],

[2, 3]])


In [12]: v1

Out[12]: array([1, 2, 3])


In [13]: v2 = array([-1, 0, -3])


In [14]: v2

Out[14]: array([-1, 0, -3])


In [15]: dot(v1, v2)

Out[15]: -10


In [16]: cross(v1, v2)

Out[16]: array([-6, 0, 2])


In [17]: zeros(10)

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


In [18]: zeros((3, 2))

Out[18]:

array([[ 0., 0.],

[ 0., 0.],

[ 0., 0.]])


In [19]: ones(10)

Out[19]: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])


In [20]: ones((3,2))

Out[20]:

array([[ 1., 1.],

[ 1., 1.],

[ 1., 1.]])


In [21]: eye(5)

Out[21]:

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 [25]: v1 = arange(0, 5, 0.5)


In [26]: v1

Out[26]: array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])


In [27]: v1.reshape(5, 2)

Out[27]:

array([[ 0. , 0.5],

[ 1. , 1.5],

[ 2. , 2.5],

[ 3. , 3.5],

[ 4. , 4.5]])


In [31]: v1

Out[31]: array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])


In [32]: v1.reshape(2, 5)

Out[32]:

array([[ 0. , 0.5, 1. , 1.5, 2. ],

[ 2.5, 3. , 3.5, 4. , 4.5]])


In [34]: v2 = random.rand(5)


In [35]: v2

Out[35]: array([ 0.42688657, 0.75595512, 0.49228696, 0.5644005 , 0.95769253])


In [36]: v2 = random.rand(5, 5)


In [37]: v2

Out[37]:

array([[ 0.1726089 , 0.32138997, 0.04819101, 0.43818724, 0.49740441],

[ 0.28443194, 0.09906931, 0.05265563, 0.71285561, 0.71925883],

[ 0.96066373, 0.84804012, 0.48517694, 0.81344891, 0.51913915],

[ 0.63495511, 0.39872218, 0.04708872, 0.63240368, 0.07593884],

[ 0.09400682, 0.88605268, 0.75523748, 0.90960914, 0.95090324]])


In [38]: v2.reshape(25, 1)

Out[38]:

array([[ 0.1726089 ],

[ 0.32138997],

[ 0.04819101],

[ 0.43818724],

[ 0.49740441],

[ 0.28443194],

[ 0.09906931],

[ 0.05265563],

[ 0.71285561],

[ 0.71925883],

[ 0.96066373],

[ 0.84804012],

[ 0.48517694],

[ 0.81344891],

[ 0.51913915],

[ 0.63495511],

[ 0.39872218],

[ 0.04708872],

[ 0.63240368],

[ 0.07593884],

[ 0.09400682],

[ 0.88605268],

[ 0.75523748],

[ 0.90960914],

[ 0.95090324]])


In [39]: random.randint(5, size=(2, 4))

Out[39]:

array([[3, 4, 1, 0],

[1, 0, 2, 2]])


In [40]: random.randint(5, size=(2, 3))

Out[40]:

array([[2, 2, 3],

[1, 4, 0]])


In [41]: random.randint(10, size=(2, 3))

Out[41]:

array([[5, 7, 4],

[7, 2, 8]])


# Primitive vs. Reference Data Types:

In [42]: x = 10


In [43]: y = x


In [44]: y = 5


In [45]: x

Out[45]: 10


In [46]: y

Out[46]: 5


In [47]: xar = array([1, 2, 3])


In [48]: yar = xar


In [49]: yar

Out[49]: array([1, 2, 3])


In [50]: yar[2] = 10


In [51]: yar

Out[51]: array([ 1, 2, 10])


In [52]: xar

Out[52]: array([ 1, 2, 10])


In [53]: xar[0] = 99


In [54]: xar

Out[54]: array([99, 2, 10])


In [55]: yar

Out[55]: array([99, 2, 10])


In [57]: zar = copy(xar)


In [58]: zar

Out[58]: array([99, 2, 10])


In [59]: xar

Out[59]: array([99, 2, 10])


In [60]: zar[1] = 144


In [61]: zar

Out[61]: array([ 99, 144, 10])


In [62]: xar

Out[62]: array([99, 2, 10])


# "if" statement:

In [63]: def what_to_wear(t):

    ...: if t > 70:

    ...: print 'Wear shorts.'

    ...: print 'Do not forget a hat.'

    ...: else:

    ...: print('Wear long pants.')

    ...:


In [64]: what_to_wear(86)

Wear shorts.

Do not forget a hat.


In [65]: what_to_wear(50)

Wear long pants.


In [66]: def what_to_wear(t):

    ...: if t > 70:

    ...: print 'Wear shorts.'

    ...: print 'Do not forget a hat.'

    ...: else:

    ...: print('Wear long pants.')

    ...: print('Do not foget a jacket.')

    ...:


In [67]: what_to_wear(86)

Wear shorts.

Do not forget a hat.

Do not foget a jacket.


In [68]: def letterGrade(score):

    ...: if score < 0:

    ...: raise Exception('This is impossible!')

    ...:

    ...: if score >= 90:

    ...: letter = 'A'

    ...: elif score >= 80:

    ...: letter = 'B'

    ...: elif score >= 70:

    ...: letter = 'C'

    ...: elif score >= 60:

    ...: letter = 'D'

    ...: else:

    ...: letter = 'F'

    ...: return letter

    ...:


In [69]: letterGrade(60)

Out[69]: 'D'


In [70]: letterGrade(95)

Out[70]: 'A'


In [71]: letterGrade(-10)


ExceptionTraceback (most recent call last)
<ipython-input-71-f7e1644f248e> in <module>()
----> 1 letterGrade(-10)

<ipython-input-68-2b04c085f76c> in letterGrade(score)
      1 def letterGrade(score):
      2     if score < 0:
----> 3             raise Exception('This is impossible!')
      4
      5     if score >= 90:
Exception: This is impossible!


In [73]: def what_to_wear2(t):

    ...: is_cold = t < 70

    ...: if not is_cold:

    ...: print 'Wear shorts.'

    ...: print 'Do not forget a hat.'

    ...: else:

    ...: print('Wear long pants.')

    ...: print('Do not foget a jacket.')

    ...:


In [74]: what_to_wear(86)

Wear shorts.

Do not forget a hat.

Do not foget a jacket.


# "for" loop:

In [76]: for i in [16, 32, 64]:

    ...: print 'i = %d'

    ...:

i = %d

i = %d

i = %d


In [83]: words = ['cat', 'window', 'defenestrate']


In [84]: for w in words:

    ...: print w, len(w)

    ...:

cat 3

window 6

defenestrate 12


In [86]: matr = random.rand(20).reshape(4,5)


In [87]: matr

Out[87]:

array([[ 0.22661546, 0.6494747 , 0.58495666, 0.81483537, 0.5630347 ],

[ 0.48949314, 0.45571794, 0.76171239, 0.99618017, 0.14531616],

[ 0.62085769, 0.11285599, 0.39260288, 0.74705603, 0.17395606],

[ 0.82953316, 0.2232166 , 0.19965804, 0.13459978, 0.06515557]])


In [88]: dim = matr.shape


In [89]: n = dim[0]


In [90]: m = dim[1]


In [91]: print n, m

4 5


In [93]: for i in range(n):

    ...: print 'Processing row %d' % i

    ...: for j in range(m):

    ...: print 'Element [%d, %d] is %f' % (i, j, matr[i, j])

    ...:

Processing row 0

Element [0, 0] is 0.226615

Element [0, 1] is 0.649475

Element [0, 2] is 0.584957

Element [0, 3] is 0.814835

Element [0, 4] is 0.563035

Processing row 1

Element [1, 0] is 0.489493

Element [1, 1] is 0.455718

Element [1, 2] is 0.761712

Element [1, 3] is 0.996180

Element [1, 4] is 0.145316

Processing row 2

Element [2, 0] is 0.620858

Element [2, 1] is 0.112856

Element [2, 2] is 0.392603

Element [2, 3] is 0.747056

Element [2, 4] is 0.173956

Processing row 3

Element [3, 0] is 0.829533

Element [3, 1] is 0.223217

Element [3, 2] is 0.199658

Element [3, 3] is 0.134600

Element [3, 4] is 0.065156


In [95]: for i in range(10^5):

    ...: print 'Checking %d power of 2' % i

    ...: if 2**i > 200:

    ...: x = i

    ...: break

    ...: print x

    ...:

Checking 0 power of 2

Checking 1 power of 2

Checking 2 power of 2

Checking 3 power of 2

Checking 4 power of 2

Checking 5 power of 2

Checking 6 power of 2

Checking 7 power of 2

Checking 8 power of 2

8


# "while" loop:

In [96]: i = 0


In [97]: while 2**i < 200:

    ...: i = i + 1

    ...: print 'Checking %d power' % i

    ...:

Checking 1 power

Checking 2 power

Checking 3 power

Checking 4 power

Checking 5 power

Checking 6 power

Checking 7 power

Checking 8 power


# Scripts:

In [100]: import os


In [101]: os.getcwd()

Out[101]: 'C:\\Users\\Oleksandr'


In [102]: os.chdir('Python')


In [103]: os.getcwd()

Out[103]: 'C:\\Users\\Oleksandr\\Python'


In [104]: os.chdir('../../')


In [105]: os.getcwd()

Out[105]: 'C:\\Users'


In [106]: os.chdir('Oleksandr\Python')


In [107]: os.getcwd()

Out[107]: 'C:\\Users\\Oleksandr\\Python'


In [108]: execfile('myscript.py')

Processing row 0

Element [0, 0] is 0.342512

Element [0, 1] is 0.510160

Element [0, 2] is 0.165041

Element [0, 3] is 0.182299

Element [0, 4] is 0.396351

Processing row 1

Element [1, 0] is 0.445663

Element [1, 1] is 0.478106

Element [1, 2] is 0.250339

Element [1, 3] is 0.902389

Element [1, 4] is 0.134936

Processing row 2

Element [2, 0] is 0.032231

Element [2, 1] is 0.204114

Element [2, 2] is 0.945526

Element [2, 3] is 0.901677

Element [2, 4] is 0.895153

Processing row 3

Element [3, 0] is 0.626471

Element [3, 1] is 0.443827

Element [3, 2] is 0.279258

Element [3, 3] is 0.297819

Element [3, 4] is 0.848740


In [110]: os.chdir('../../../')


In [111]: os.getcwd()

Out[111]: 'C:\\'


In [112]: execfile('myscript.py')


IOErrorTraceback (most recent call last)
<ipython-input-112-3c68092e52d2> in <module>()
----> 1 execfile('myscript.py')

IOError: [Errno 2] No such file or directory: 'myscript.py'


In [114]: execfile('C:\Users\Oleksandr\Python\myscript.py')

Processing row 0

Element [0, 0] is 0.350968

Element [0, 1] is 0.107134

Element [0, 2] is 0.983441

Element [0, 3] is 0.106485

Element [0, 4] is 0.370584

Processing row 1

Element [1, 0] is 0.202968

Element [1, 1] is 0.077848

Element [1, 2] is 0.332530

Element [1, 3] is 0.046489

Element [1, 4] is 0.727264

Processing row 2

Element [2, 0] is 0.277636

Element [2, 1] is 0.437702

Element [2, 2] is 0.493691

Element [2, 3] is 0.027511

Element [2, 4] is 0.867979

Processing row 3

Element [3, 0] is 0.695296

Element [3, 1] is 0.380557

Element [3, 2] is 0.623928

Element [3, 3] is 0.536510

Element [3, 4] is 0.169521


# Storing functions:

In [115]: import myfuns as mf


ImportErrorTraceback (most recent call last)
<ipython-input-115-06f152f043bf> in <module>()
----> 1 import myfuns as mf

ImportError: No module named myfuns


In [116]: os.getcwd()

Out[116]: 'C:\\'


In [117]: os.chdir('C:\Users\Oleksandr\Python')


In [118]: import myfuns as mf


In [119]: mf.myf(10)

Out[119]: 1244.0


In [120]: mf.myf(10)

Out[120]: 1244.0


In [121]: reload(mf)

Out[121]: <module 'myfuns' from 'myfuns.py'>


In [122]: mf.myf(10)

Out[122]: 1144.0