Intro to Python


In [1]:
# using the notebook

1 + 1


Out[1]:
2

In [2]:
# variables

a = 2
b = a * 3
print(b)


6

In [12]:
# lists

my_list = [1, "hi", [2, 3]]

In [13]:
print(my_list[0])


1

In [14]:
print(my_list[-1])


[2, 3]

In [6]:
# loops

for item in my_list:
    print(item)


1
hi
[2, 3]

In [8]:
# conditionals

value = 5

if value > 3:
    print("{0} > 3".format(value))
else:
    print("{0} <= 3".format(value))


5 > 3

In [9]:
# arrays

import numpy as np

my_array = np.array([1.0, 2.3, 3.5])

In [10]:
print(my_array)


[ 1.   2.3  3.5]

In [11]:
print(my_array[0])


1.0

In [16]:
print(my_array[:2])


[ 1.   2.3]

In [17]:
print(my_array.shape)


(3,)

In [18]:
print(np.arange(10))


[0 1 2 3 4 5 6 7 8 9]

In [3]:
array_2d = np.reshape(np.arange(6), (2, 3))

In [4]:
print(array_2d)


[[0 1 2]
 [3 4 5]]

In [5]:
print(array_2d[0])


[0 1 2]

In [6]:
# NBVAL_IGNORE_OUTPUT

print(array_2d[1, 1])


4

In [30]:
print(array_2d.shape)


(2, 3)

In [31]:
print(array_2d * my_array)


[[  0.    2.3   7. ]
 [  3.    9.2  17.5]]

In [32]:
print(array_2d + my_array)


[[ 1.   3.3  5.5]
 [ 4.   6.3  8.5]]

In [1]:
# plotting

%matplotlib inline

import matplotlib.pyplot as plt

x = np.random.random(100)

plt.plot(x);



In [2]:
data = np.random.random((100, 100))
plt.imshow(data);



In [ ]: