Nia Colab
Nia Colab
ipynb - Colaboratory
Logic
Boolean Values
The boolean type has only two values: True and False . Let's assign a boolean value to a variable and verify
the type using the built-in function type() :
python_is_fun = True
print(python_is_fun)
True
type(python_is_fun)
bool
Let's assign the value False to a variable and again verify the type:
math_is_scary = False
print(math_is_scary)
False
type(math_is_scary)
bool
Comparison Operators
Comparison operators produce Boolean values as output. For example, if we have variables x and y with
numeric values, we can evaluate the expression x < y and the result is a boolean value either True or
False .
Comparison Operator Description
== equal
!= not equal
https://colab.research.google.com/drive/1X1e6ghdW6_71VPd8bPQE8LRF6ap2_wv5#scrollTo=JUaP6Yq_tc1h&printMode=true 1/6
11/30/2020 logic.ipynb - Colaboratory
For example:
1 == 2
False
1 < 2
True
2 == 2
True
3 != 3.14159
True hu
20.00000000001 >= 20
True
Boolean Operators
We combine logical expressions using boolean operators and , or and not .
Boolean Operator Description
(1 < 2) and (3 != 5)
True
False
(1 < 2) or (3 < 1)
True
https://colab.research.google.com/drive/1X1e6ghdW6_71VPd8bPQE8LRF6ap2_wv5#scrollTo=JUaP6Yq_tc1h&printMode=true 2/6
11/30/2020 logic.ipynb - Colaboratory
( )
True
if statements
An if statement consists of one or more blocks of code such that only one block is executed depending on
logical expressions. Let's do an example:
Discriminant = -24604
Roots are complex
Examples
Invertible Matrix
Represent a 2 by 2 square matrix as a list of lists. For example, represent the matrix
[
https://colab.research.google.com/drive/1X1e6ghdW6_71VPd8bPQE8LRF6ap2_wv5#scrollTo=JUaP6Yq_tc1h&printMode=true 3/6
11/30/2020 logic.ipynb - Colaboratory
2 −1] 5 7
Write a function called invertible which takes an input parameter M , a list of lists representing a 2 by 2
matrix, and returns True if the matrix M is invertible and False if not.
def invertible(M):
'''Determine if M is invertible.
Parameters
----------
M : list of lists
Representation of a 2 by 2 matrix M = [[a,b],[c,d]].
Examples
--------
>>> invertible([[1,2],[3,4]])
True
'''
# A matrix M is invertible if and only if
# the determinant is not zero where # det(M) =
ad - bc for M = [[a,b],[c,d]] determinant = M[0][0]
* M[1][1] - M[0][1] * M[1][0] if determinant != 0:
return True
else: return
False
invertible([[1,2],[3,4]])
True
invertible([[1,1],[3,3]])
False
Concavity of a Polynomial
Write a function called concave_up which takes input parameters p and a where p is a list
representing a polynomial p(x) and a is a number, and returns True if the function p(x) is concave
up at x = a (ie. its second derivative is positive at x = a, p′′(a) > 0).
https://colab.research.google.com/drive/1X1e6ghdW6_71VPd8bPQE8LRF6ap2_wv5#scrollTo=JUaP6Yq_tc1h&printMode=true 4/6
11/30/2020 logic.ipynb - Colaboratory
We'll use the second derivative test for polynomials. In particular, if we have a polynomial of degree n
then the second derivative of p(x) at x = a is the sum p′′(a) = 2(1)c2 + 3(2)c3a +
4(3)c4a2 + ⋯ + n(n − 1)cnan−2
def concave_up(p,a):
'''Determine if the polynomial p(x) is concave up at x=a.
Parameters
----------
p : list of numbers
List [a_0,a_1,a_2,...,a_n] represents the polynomial
p(x) = a_0 + a_1*x + a_2*x**2 + ... + a_n*x**n
Returns
------ bool
True if p(x) is concave up at x=a (ie. p''(a) > 0) and False otherwise.
Examples
--------
>>> concave_up([1,0,-2],0)
False
>>> concave_up([1,0,2],0)
True
'''
# Degree of the polynomial p(x)
degree = len(p) - 1
p = [1,1,0,-1] a = 2
concavity = concave_up(p,a)
print(concavity)
False
https://colab.research.google.com/drive/1X1e6ghdW6_71VPd8bPQE8LRF6ap2_wv5#scrollTo=JUaP6Yq_tc1h&printMode=true 5/6
11/30/2020 logic.ipynb - Colaboratory
Exercises
1. The discriminant of a cubic polynomial p(x) = ax3 + bx2 + cx + d is
The discriminant gives us information about the roots of the polynomial p(x):
2. Represent a 2 by 2 square matrix as a list of lists. For example, represent the matrix
$$
[ 2
−1]
5 7
$$
as the list of lists [[2,-1],[5,7]] . Write a function called inverse_a which takes an input
parameter a and returns a list of lists representing the inverse of the matrix
$$
[ 1
a]
a −1
$$
3. Write a function called real_eigenvalues which takes an input parameter M , a list of lists
representing a 2 by 2 matrix (as in the previous exercise), and returns True if the eigenvalues of the
matrix M are real numebrs and False if not.
https://colab.research.google.com/drive/1X1e6ghdW6_71VPd8bPQE8LRF6ap2_wv5#scrollTo=JUaP6Yq_tc1h&printMode=true 6/6