- Some Builtin Types
import math
def f():
print "This is a user-defined function"
return 42
print "Some basic types in Python:"
print type(2) # int
print type(2**500) # long
print type(2.2) # float
print type("2.2") # str (string)
print type(2 < 2.2) # bool (boolean)
print type(math) # module
print type(math.tan) # builtin_function_or_method
print type(f) # function (user-defined function)
print type(type(42)) # type
print "#####################################################"
print "And some other types we will see later in the course..."
print type(Exception()) # Exception
print type(xrange(5)) # xrange
print type([1,2,3]) # list
print type((1,2,3)) # tuple
print type({1,2}) # set
print type({1:42}) # dict (dictionary or map)
print type(2+3j) # complex (complex number) (we may not see this type)
- Some Builtin Constants
print "Some builtin constants:"
print True
print False
print None
print "And some more constants in the math module:"
import math
print math.pi
print math.e
- Some Builtin Functions
print "Type conversion functions:"
print bin(42) # convert to binary (base 2) string
print bool(0) # convert to boolean (True or False)
print float(42) # convert to a floating point number
print hex(42) # convert to hexadecimal (base 16) string
print int(2.8) # convert to an integer (int)
print "And some basic math functions:"
print abs(-5) # absolute value
print max(2,3) # return the max value
print min(2,3) # return the min value
print pow(2,3) # raise to the given power (pow(x,y) == x**y)
print round(2.354, 1) # round with the given number of digits
- Some Builtin Operators
Category | Operators |
Arithmetic | +, -, *, /, //, **, %, - (unary), + (unary) |
Relational | <. <=, >=, >, ==, !=, <> |
Assignment | +=, -=, *=, /=, //=, **=, %=, <<=, >>=, &=, |=, ^= |
Logical | and, or, not |
Note: for now at least, we are not covering the bitwise operators (<<, >>, &, |, ^, ~).
- Types Affect Semantics
print 3 * 2
print 3 * "abc"
print 3 + 2
print "abc" + "def"
print 3 + "def"
- Operator Precedence
print 2+3*4 # prints 14, not 20
print 2**4/2 # prints 8, not 4
- Integer Division
print " 6/3 =", ( 6/3)
print " 5/3 =", ( 5/3)
print " 2/3 =", ( 2/3)
print "-4/3 =", (-4/3)
- The Modulus or Remainder Operator (%)
print " 6%3 =", ( 6%3)
print " 5%3 =", ( 5%3)
print " 2%3 =", ( 2%3)
print " 0%3 =", ( 0%3)
print "-4%3 =", (-4%3)
print " 3%0 =", ( 3%0)
- Integer vs Floating-Point Division
print 10 / 3
print 10.0 / 3
print 10 / 3.0
x = 10
print x / 3
print 1.0 * x / 3
print float(x) / 3
print float(x / 3) # careful!
-
Approximate Values of Floating-Point Numbers
print (0.1 + 0.1 == 0.2) # True, but...
print (0.1 + 0.1 + 0.1 == 0.3) # False!
print (0.1 + 0.1 + 0.1) # prints 0.3, which seems ok, but...
print (0.1 + 0.1 + 0.1) - 0.3 # prints 5.55111512313e-17 (tiny, but non-zero!)
- Floating-Point Numbers and almostEqual
d1 = 0.1 + 0.1 + 0.1
d2 = 0.3
print (d1 == d2) # still False, of course
epsilon = 10**-10
print (abs(d2 - d1) < epsilon) # True!
# Once again, using an almostEqual function (that we will write)
def almostEqual(d1, d2, epsilon=10**-10):
return (abs(d2 - d1) < epsilon)
d1 = 0.1 + 0.1 + 0.1
d2 = 0.3
print (d1 == d2) # still False, of course
print almostEqual(d1, d2) # True, and now packaged in a handy reusable function!
- Short-Circuit Evaluation
x = 0
y = 0
print ((y != 0) and ((x/y) != 0)) # Works!
print (((x/y) != 0) and (y != 0)) # Crashes!
# Once again, using the "or" operator
x = 0
y = 0
print ((y == 0) or ((x/y) == 0)) # Works!
print (((x/y) == 0) or (y == 0)) # Crashes!
# Yet another example:
def isPositive(n):
result = (n > 0)
print "isPositive(",n,") =", result
return result
def isEven(n):
result = (n % 2 == 0)
print "isEven(",n,") =", result
return result
print "Test 1: isEven(-4) and isPositive(-4)"
print (isEven(-4) and isPositive(-4)) # Calls both functions
print "----------"
print "Test 2: isEven(-3) and isPositive(-3)"
print (isEven(-3) and isPositive(-3)) # Calls only one function!
- Truth Tables for Testing Logical Equivalence
One of DeMorgan's Laws states:
A and B == not ((not A) or (not B))
A | B | A and B | not A | not B | (not A) or (not B) | not ((not A) or (not B)) |
0 | 0 | 0 | 1 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 0 | 1 | 0 |
1 | 0 | 0 | 0 | 1 | 1 | 0 |
1 | 1 | 1 | 0 | 0 | 0 | 1 |
- Boolean Arithmetic
# In general, you should not use boolean arithmetic (it can be confusing).
# Instead, use "if" statements or expressions. That said, you need to
# understand how boolean arithmetic works (in case someone else uses it,
# and also because you cannot use "if" for hw1 or quiz1!).
# In numeric expressions...
# True is treated as 1
# False is treated as 0
# So...
print 5 * True # 5
print 5 * False # 0
print 5 + True # 6
print 5 + False # 5