Expressions

Arithmetic and String Expressions

print 2+8
print 2-8
print 2*8
print 8/2
print 2**8
#be careful with division.  Sometimes to be safe, do 2.0/8.0
print 2/8
#or you can do type conversion; 
#if either top or bottom is a float then automatically the result is a float
top = 2
bot = 8
print float(top)/float(bot)
#usual rules of precedence:  parantheses, exponent, mult and divide, add and substact
print (5-2)**3*2-5
 
#expressions involving strings
print greeting + " Ko"
print greeting*3

Modulus

#The modulus.  x%y gives the remainder after x divides y
#checks to see whether something is odd(1) or even(0)
print 5%2
print 14%2
#checks to see whether something is a multiple of another;  if it is, returns 0
print 14%6
print 48%6
#extracts the rightmost digit of a number
print 531%10
#or the rightmost 2 digits
print 531%100

Boolean Expressions

#Boolean expressions - an expression that is either true or false
x =5
y = 6
print x==y  	#True if x equals y.  False otherwise
print x!=y	#True if x is not equal to y.  False otherwise
print x < y	#True if x is less than y.  False otherwise
print x > y	#True if x is greater than y.  False otherwise
print x<=y	#True if x is less than or equal to y.  False otherwise
print x >=y	#True if x is greater than or equal to y.  False otherwise

Logical Operators

#Logical operators - and, or, not
n = 7
print n%2 or n%3  	#True if either statement is true
print n%2 and n>5 	#True if both statements are true
print not(n>5)		#Opposite of the expression.  Since n>5 is True, then not(n>5) is False