A continuation of the previous lesson "Introduction to Python", here we introduce more advanced concepts.
Operators are the constructs which can manipulate the value of operands.
The standard operators are +, -, , *, /, % for addition, subtraction, multiplication, exponentials, division and modulus respectively.
var_a = 10
var_b = 5
var_sum = var_a + var_b
The comparison operators are similar to any other language with the commonly known symbols >, <, =, ==, >=, <= denoting, greater than, less than, assignment, equal to, greater than or equal to and less than or equal to respectively.
To compare equality of the two variables, var_a and var_b and assign the result (True or False) to variable, var_c:
var_c = var_a == var_b
Try out the above example by assigning var_a
, a value of 5 and var_b
, a value of 10. Store the comparaed result in var_c and print it out.
var_a = 5
var_b = 10
# Assign result of comparison of above variables to var_c
# Hint
# var_a and var_b are not equal
var_c = var_a == var_b
print (var_c)
False
ref_tmp_var = False
try:
if var_c == False:
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions.')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. If x is a subset of y, then the result is True:
>>> if var_a in [1, 2, 3, 4, 5, 6]:
... print("True")
True
Identity operators compare the memory locations of two python objects and returns True if they point to the same location.
Two objects can contain the same value but can be different objects. Here is an example which illustrates the 'is' operator:
a = 'This '
b = a + 'is a python tutorial'
c = 'This is a python tutorial'
>> b == c
True
>> b is c
False
Ref: https://docs.python.org/3/library/operator.html
The two commonly used loops are the While loop and the For loop.
Note that Python works with indentation and no braces are necessary. The next line of code following the loops or conditions or function blocks should have an indentation of two spaces or four spaces.
Syntax:
While :
statements
Example:
i = 1
while i < 5:
i += 1
Syntax:
range(1, 10) -- generates a list of numbers from 1 to 10
for i in range(1, 10):
statements
Ref: https://docs.python.org/3/tutorial/controlflow.html
Compute the square of each number in X and assign it to a list y print y.
x = range(1, 10)
y = []
# To compute power of a variable, use **
for i in x:
y.append(i**2)
print(y)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
ref_tmp_var = False
try:
import numpy as np
if np.all(y == [1, 4, 9, 16, 25, 36, 49, 64, 81]):
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions.')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
Decision Making is to execute a set of statements based on the validation of a condition. The decision making in Python involves an 'if' condition that is written as:
Syntax:
if condition:
statements
Ref: https://docs.python.org/3/tutorial/controlflow.html
Push even numbers in [0, 10] to the list variable even_numbers
print even_numbers
even_numbers = []
x = range(0, 10)
Hint: Use the for loop
for i in x:
if (i%2) == 0:
even_numbers.append(i)
print(even_numbers)
[0, 2, 4, 6, 8]
ref_tmp_var = False
try:
import numpy as np
even_numbers_ = []
for i in x:
if (i%2) == 0:
even_numbers_.append(i)
if np.all(even_numbers == even_numbers_):
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions.')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
A tuple is a sequence of immutable sequential objects. Tuples use parentheses for access unlike lists and cannot be changed. It is an ordered sequence and used for objects such as coordinates that contain latitude, longitude.
Example:
loc = (30.456, 50.436)
Ref: https://docs.python.org/3/c-api/tuple.html
Sets are collection types and very useful for Mathematicians, Statisticians and Data Scientists. A set contains an unordered collection of unique objects of the same type. It is equivalent to the mathematical definition of sets.
even_number_set = set([0, 2, 4, 6])
Ref: https://docs.python.org/3/c-api/set.html
Dictionaries are hash maps containing key-value pairs. While the keys need to be unique, the referenced values need not be. The values are accessed via the keys.
Example: A dictionary of city to state:
location_dict = {'Boston': 'MA', 'Chicago': 'IL', 'New York': 'NY'}
Ref: https://docs.python.org/3/c-api/dict.html
Build a location map that contains latitude and longitude of the following cities.
Boston: 42.318365, -71.086692
Chicago: 41.797568, -87.620958
New York: 40.685526, -73.887406
Assign it to the variable location_map
Print out the variable location_map
location_map = {}
Hint: Use dictionary and tuples.
location_map = {'Boston': (42.318365, -71.086692),
'Chicago': (41.797568, -87.620958),
'New York': (40.685526, -73.887406)}
print(location_map)
{'Boston': (42.318365, -71.086692), 'Chicago': (41.797568, -87.620958), 'New York': (40.685526, -73.887406)}
ref_tmp_var = False
try:
location_map_ = {'Boston': (42.318365, -71.086692),
'Chicago': (41.797568, -87.620958),
'New York': (40.685526, -73.887406)}
if location_map == location_map_:
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions.')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
A function is a block of code that executes in a sequence and can be invoked in a repeated fashion. We have already used some functions such as .append(), .pop(), print(). These are called 'builtin' functions. We can also define our own set of functions. Here is an example of a function that takes in two numbers and returns their sum:
def add_values(x, y):
z = x + y
return z
Here x, y, z are called local variables as they only retain their values inside the function. This function is invoked as:
>>print(add_values(5, 10))
15
Python has a standard library where functions such as print(), range(), len() can be invoked at any point in our code. However, some functions that belong to a category of operations often exist in modules. For example, if we intend to use a logartithm, a square root operation, then such operations exist in a module called the 'math' module. Similarly, there are tons of modules in python that we can leverage to write our code. These modules need to be imported to use them.
To import a module you must explicity specify 'import ' in the beginning of your code.
For example, to compute a square root of a number:
import math
print(math.sqrt(25))
5
These modules sometimes are heavy in terms of memory of functions that are loaded. Also modules are often built up of submodules that could be imported to save memory. Consider a module built of a heirarchy of submodules in this manner:
For example consider module A that has submodule B, C, D as submodules. Suppose the submodule D has a function func(), which is all we need for our code. Then, we can import just the submodule in a heirarchial fashion as:
from A.C import D
D.func()
Write a function, compute_sqrt(x), which takes a non-negative number as an input and returns a dictionary with key as number and square root of the number as its value.
import math
# def compute_sqrt(x):
# <statements>
Hint: Use { key: value } to specify a dictionary
def compute_sqrt(x):
return {x: math.sqrt(x)}
sqrt_25 = compute_sqrt(25)
ref_tmp_var = False
try:
if sqrt_25[25] == 5:
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions.')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var