and
: returns True if statement1 and statement2 are True,
returns False otherwise
syntax: statement1 and
statement2
or
: returns True if statement1 or statement2 is True,
returns False otherwise
syntax: statement1 or
statement3
not
: returns opposite of statement
syntax: not
statement
# example usage of bitwise operators
tf = True and False
print("The value of True and False is", tf)
tf = True or False
print("The value of True or False is", tf)
tf = not True
print("The value of not True is", tf)
tf = (True and False) or (True or False)
print("The value of (True and False) or (True or False) is", tf)
tf = not tf
print("The value of not ( (True and False) or (True or False) ) is", tf)
flow control - things in coding which individual components of a program will run and when.
Ex: if statements, loops, ...
# if statement - if some testExpression is Ture, evaluate the statements
# inside of the if-statement. Indicate that some code is inside an if-statement
# by indentation
# syntax: if testExpression:
# statements
tf = False
if tf == False:
print("Hello, World!")
if not tf:
print("Hello, World!")
num = 10
if num > 4:
print("The number is greater than 4")
# if statements can include other conditions and things to do if the first
# statement isn't true. This is called an "if-else-statement"
# syntax: if textExpression1:
# statements1
# elif textExpression2:
# statements2
# else:
# statements3
# if-else-statements will execute statements based on the first True
# textExpressions or will execute statements in the else-statement if none of
# the expressions are True.
# note: the "else" part cannot have any testEpressions
# note: you can have a many else-if's as you want but you can only have
# 1 or 0 else's.
num = 1
if num < 0:
print("the number is less than 0")
elif num < 3:
print("the number is between 0 and 2")
else:
print("the number is greater than 2")
# For loop - used to iterate over a sequence of things
# syntax: for val in sequence:
# statements
# "val" takes on every value in the sequence and execute "statements" with
# that value.
# program to find the sum of all numbers stored in a list
numbers = [6, 5, 7, 0 , 2, -1]
s = 0
for val in numbers:
s = s + val
#print(s)
# Program to execute code n times
n = 10
for i in range(3, n):
print(i)
# while loop- repeat a specific block of code as long as the test expression
# is true
# syntax: while testExpression:
# statements
# Program to add natural numbers up to n: sum = 1 + 2 + ... + n
n = 10
s = 0
i = 1
while i <= n:
s = s + i
i = i + 1
print("The sum is:", s)
# Program a while loop that multiplies all natural numbers up to 7
n = 3
prod = 1
i = 1
while i <= n:
prod = prod * i
i = i + 1
print(ans)
# Break and continue - keywords used to alter the flow of a normal loop
# When break is evaluated, if "breaks" out of the loop containing it
# When continue is evaluated, it skips the rest of the code inside the loop
# for the current iteration only
# Break and continue examples
# for val in "string":
# if val == "i":
# break
# print(val)
for val in "string":
if val == "i":
continue
print(val)
# Python function
# syntax: def function_name(parameters):
# """Description of the function"""
# statements
def greet(name):
""" Function the greets the person named 'name' """
print("Hello, " + name + ". Good Morning!")
greet("Cindy")
# return keyword is used to exit a function. It also allows you to use wahtever you calculated inside
# the function outside of the function
def abs_val(num):
""" Computing the absolute value of input argument 'num' """
if num >= 0:
return num
else:
return -num
a = -10
a_abs = abs_val(a)
print(a_abs)
# matrices - using lists (actually a list of lists)
L = [[1, 2], [3, 4]]
D = [[0, 1, 2, 3],[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
print(D)
# matrices - using numpy, a more efficient way to work with matrices
# numpy is a python library with a bunch of useful matrix and linear algebra function
import numpy
C = numpy.array(D)
print(C)
n = 4
D_another = numpy.zeros((n, n))
print(D_another)
for i in range(n):
for j in range(n):
D_another[i][j] = i + j
print(D_another)
# relevant numpy functions
print(numpy.shape(C))
print(numpy.shape(C)[0])