In [0]:
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Welcome to this Colab where you will get a quick introduction to the Python programming language and the environment used for the course's exercises: Colab.
Colab is a Python development environment that runs in the browser using Google Cloud.
For example, to print "Hello World", just hover the mouse over [ ] and press the play button to the upper left. Or press shift-enter to execute.
In [0]:
print("Hello World")
In [0]:
def HelloWorldXY(x, y):
if (x < 10):
print("Hello World, x was < 10")
elif (x < 20):
print("Hello World, x was >= 10 but < 20")
else:
print("Hello World, x was >= 20")
return x + y
for i in range(8, 25, 5): # i=8, 13, 18, 23 (start, stop, step)
print("--- Now running with i: {}".format(i))
r = HelloWorldXY(i,i)
print("Result from HelloWorld: {}".format(r))
In [0]:
print(HelloWorldXY(1,2))
Easy, right?
If you want a loop starting at 0 to 2 (exclusive) you could do any of the following
In [0]:
print("Iterate over the items. `range(2)` is like a list [0,1].")
for i in range(2):
print(i)
print("Iterate over an actual list.")
for i in [0,1]:
print(i)
print("While works")
i = 0
while i < 2:
print(i)
i += 1
In [0]:
print("Python supports standard key words like continue and break")
while True:
print("Entered while")
break
Python has lists built into the language. However, we will use a library called numpy for this. Numpy gives you lots of support functions that are useful when doing Machine Learning.
Here, you will also see an import statement. This statement makes the entire numpy package available and we can access those symbols using the abbreviated 'np' syntax.
In [0]:
import numpy as np # Make numpy available using np.
# Create a numpy array, and append an element
a = np.array(["Hello", "World"])
a = np.append(a, "!")
print("Current array: {}".format(a))
print("Printing each element")
for i in a:
print(i)
print("\nPrinting each element and their index")
for i,e in enumerate(a):
print("Index: {}, was: {}".format(i, e))
In [0]:
print("\nShowing some basic math on arrays")
b = np.array([0,1,4,3,2])
print("Max: {}".format(np.max(b)))
print("Average: {}".format(np.average(b)))
print("Max index: {}".format(np.argmax(b)))
In [0]:
print("\nYou can print the type of anything")
print("Type of b: {}, type of b[0]: {}".format(type(b), type(b[0])))
In [0]:
print("\nUse numpy to create a [3,3] dimension array with random number")
c = np.random.rand(3, 3)
print(c)
In [0]:
print("\nYou can print the dimensions of arrays")
print("Shape of a: {}".format(a.shape))
print("Shape of b: {}".format(b.shape))
print("Shape of c: {}".format(c.shape))
print("...Observe, Python uses both [0,1,2] and (0,1,2) to specify lists")
Colab is a virtual machine you can access directly. To run commands at the VM's terminal, prefix the line with an exclamation point (!).
In [0]:
print("\nDoing $ls on filesystem")
!ls -l
!pwd
In [0]:
print("Install numpy") # Just for test, numpy is actually preinstalled in all Colab instances
!pip install numpy
Exercise
Create a code cell underneath this text cell and add code to:
In [0]:
!pwd
!cd /
!ls -l
print("Hello")
All usage of Colab in this course is completely free or charge. Even GPU usage is provided free of charge for some hours of usage every day.
Using GPUs
Some final words on Colab
Learn More