- 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 bool(0) # convert to boolean (True or False)
print float(42) # convert to a floating point number
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
def yes():
return True
def no():
return False
def crash():
return 1/0 # crashes!
print (no() and crash()) # Works!
print (crash() and no()) # Crashes!
print (yes() and crash()) # Also crashes (but does not short-circuit)
# Once again, using the "or" operator
print (yes() or crash()) # Works!
print (crash() or yes()) # Crashes!
print (no() or crash()) # Also crashes (but does not short-circuit)
# 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!
- Truthy and Falsey
# "x is Truthy" means "bool(x) == True"
# "x is Falsey" means "bool(x) == False"
# Also: Python returns the last value it required
# to determine the Truth value of a logical expression.
# So, what do these do?
print (True and 5)
print (False and 5)
print (1 and 2) or 42
print (1 and 0) or 42
- 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
# 112 Boolean Arithmetic Style Rule #1: Do not use boolean arithmetic!
# But if you really can't help yourself, then....
# 112 Boolean Arithmetic Style Rule #2: If you do use boolean arithmetic,
# be sure to first explicitly convert the boolean to an int using int().
# So...
print 5 * int(True) # 5
print 5 * int(False) # 0
print 5 + int(True) # 6
print 5 + int(False) # 5