==== 9/18/13 ====
Modulus Operator gives remander after division. For example.
In [1]:
5 % 2
Out[1]:
In [2]:
4 % 2
Out[2]:
===== Booleans =====
In [3]:
5 < 6
Out[3]:
In [4]:
5 != 6
Out[4]:
In [8]:
3 + 1 == 2 + 2
Out[8]:
In [9]:
not True
Out[9]:
In [10]:
not False
Out[10]:
In [12]:
True and False
Out[12]:
In [19]:
True and not (True or False) and not False
Out[19]:
In [20]:
False is not True
Out[20]:
In [21]:
4 % 2 == 0
Out[21]:
In [22]:
n = 5
n % 2 == 0
Out[22]:
In [23]:
17 and False
Out[23]:
In [24]:
-1 and True
Out[24]:
In [25]:
0 and True
Out[25]:
In [26]:
True and 0
Out[26]:
In [27]:
0 or True
Out[27]:
In [28]:
0 or False
Out[28]:
In [29]:
'fred' and True
Out[29]:
In [30]:
'' and True
Out[30]:
In [31]:
'' or True
Out[31]:
In [33]:
None and True
In [34]:
None or True
Out[34]:
==== Conditionals ====
In [36]:
name = 'bob'
if name == 'fred':
print "Hi fred!"
In [40]:
x = 99
if x > 10:
z = 9
x = x + z
print z
In [43]:
name = 'susan'
if name == 'jane' or name == 'susan':
print "Hello " + name
else:
print "Who are you?"
In [48]:
name = 'fred'
if name == 'tom':
print "Hello Tom"
elif name == 'jim':
print "Hello Jim"
else:
print "I don't know any "+name
In [49]:
x = 7
y = 6
if x == y:
print 'x and y are equal'
else:
if x < y:
print 'x is less than y'
else:
print 'x is greater than y'
In [52]:
x = 986469
if x % 3 == 0:
print "Divisible by 3"
elif x % 5 == 0:
print "Divisible by 5"
==== Recursion ====
Basically, the act of a function calling itself. For example, see this function calls itself.
In [53]:
def countdown(n):
if n <= 0:
print 'Blastoff!'
else:
print n
countdown(n-1)
countdown(5)
Recursion is a useful tool in solving some problems (mostly the ones where a "divide and conquer" strategy are useful), however, it can be error prone and should be used with caution.
==== Gettin INPUT ====
A simple function to get input from the user is raw_input. Be aware that the value returned from raw_input is ALWAYS a string! So if you need a number, you must convert the value to a number using one of the conversion functions ( int() or float() ).
In [58]:
name = raw_input("What's your name:")
age = raw_input("How old are you:")
age = int(age)
print "Hello "+name
if int(age) > 22:
print "Boy you are old"
print type(age)
In [ ]: