This notebook includes a quick review of the elements of the Python language we will need to discuss NetworkX. Each editable box contains a chunk of code that will execute when you press shift + enter. Try it here - try it with and without the "print":
In [ ]:
print "Hello world!"
A variable represents a piece of data that you can store in memory for later use. Variables can have one of several types, especially string (sequence of characters), integer (whole number), or float (number with decimals). Variables can have any name we want, without spaces.
You can put a value into a variable using the equals sign. More details are here.
Try running these commands to see variables in action:
In [ ]:
my_string_variable = "Hello world"
print my_string_variable
print type(my_string_variable)
In [ ]:
my_float_variable = 123.45
print my_float_variable
print type(my_float_variable)
Python supports basic arithmetic operators, where are overloaded to support different types, but not all types. For example:
In [ ]:
print my_string_variable + "!"
print my_float_variable + 0.5
print my_string_variable + 0.5
In [ ]:
my_list_variable = [1,2,9,6,2]
print my_list_variable
Individual elements are accessed by a sequential index number, also surrounded by square brackets, starting with zero. Negative numbers wrap to the end of the list. Note the error when we access an index number past the end of the list:
In [ ]:
print my_list_variable[2]
print my_list_variable[-1]
print my_list_variable[99]
A new list can be created with empty brackets:
In [ ]:
another_list = []
print type(another_list)
Items can be added using the append function, either to append individual items or multiples.
In [ ]:
another_list.append([1,2,3,4,5])
print another_list
The built-in len() function tells you how long a list is:
In [ ]:
len(my_list_variable)
List can contain any type of data, like strings:
In [ ]:
my_list_var = ["good", "morning"]
print my_list_var
In [ ]:
my_dictionary = { "word": "the", "count": 123456789 }
print my_dictionary
Individual elements are accessed the key, also using square brackets, for example:
In [ ]:
print my_dictionary["word"]
print my_dictionary["count"]
The elements stored in lists and dictionaries can be treated like any other variables, for example:
In [ ]:
a = my_list_variable[3] + my_dictionary["count"]
print a
Functions are multiple statements grouped together and given a name. Functions are indicated by parentheses following the name. Functions must first be defined before they can be used.
Note the colon following the function definition. Also, note that in Python indentation is significant - it knows the second line below is part of the function because it is indented following the function name:
In [ ]:
def say_hello():
print "Hello world"
Functions are called by using the parentheses only, without the "def" or colon:
In [ ]:
say_hello()
Within the parentheses, arguments can be passed to the function, which operates on the parameter. For example:
In [ ]:
def say_hello(name):
print "hello, " + name + "!"
This then lets us use the function with multiple different values:
In [ ]:
say_hello("there")
say_hello("bunny rabbit")
say_hello(my_string_variable)
Functions can also be set up to return values, using the keyword "return":
In [ ]:
def square(n):
return n ** 2
The returned values can then be assigned to variables, for example:
In [ ]:
b = square(4)
c = b * square(b)
print c
In [ ]:
print range(5)
print range(5, 10)
print range(5, 100, 5)
sorted - sorts a list:
In [ ]:
my_list = [2,4,25,6,7,5]
print sorted(my_list)
Built-in functions are often grouped into modules, for example, random - returns a random number between zero and one. The dot notation means that you are referencing something that is contained in another, in this case the rand function within the random module.
In [ ]:
random.rand()
Each line we have worked with so far is a statement, which we've been running individually. Flow control commands (keywords) determine the sequence in which they will execute.
for-in iterates over items; if-else conditionally selected between statements. Note the colon and indentation define which statements are included in the conditional blocks.
In [ ]:
x = range(10)
for a in x:
if a > random.rand() * 10:
print a
else:
print "false"
print "all done"
In [ ]:
a = range(10)
print a
b = [n/10.0 for n in a]
print b
In [ ]:
import networkx as nx
In [ ]:
range(10)
In [ ]:
a = []
for i in range(10):
a.append(random.rand())
OR
In [ ]:
a = [random.rand() for b in range(10)]
In [ ]:
b = sorted(a)
In [ ]:
print b[-2]
Or, in one line:
In [ ]:
sorted([random.rand() for i in range(10)])[-2]