==== 9/18/13 ====

Modulus Operator gives remander after division. For example.


In [1]:
5 % 2


Out[1]:
1

In [2]:
4 % 2


Out[2]:
0

===== Booleans =====


In [3]:
5 < 6


Out[3]:
True

In [4]:
5 != 6


Out[4]:
True

In [8]:
3 + 1 == 2 + 2


Out[8]:
True

In [9]:
not True


Out[9]:
False

In [10]:
not False


Out[10]:
True

In [12]:
True and False


Out[12]:
False

In [19]:
True and not (True or False) and not False


Out[19]:
False

In [20]:
False is not True


Out[20]:
True

In [21]:
4 % 2 == 0


Out[21]:
True

In [22]:
n = 5
n % 2 == 0


Out[22]:
False

In [23]:
17 and False


Out[23]:
False

In [24]:
-1 and True


Out[24]:
True

In [25]:
0 and True


Out[25]:
0

In [26]:
True and 0


Out[26]:
0

In [27]:
0 or True


Out[27]:
True

In [28]:
0 or False


Out[28]:
False

In [29]:
'fred' and True


Out[29]:
True

In [30]:
'' and True


Out[30]:
''

In [31]:
'' or True


Out[31]:
True

In [33]:
None and True

In [34]:
None or True


Out[34]:
True

==== 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


9

In [43]:
name = 'susan'
if name == 'jane' or name == 'susan':
    print "Hello " + name
else:
    print "Who are you?"


Hello susan

In [48]:
name = 'fred'
if name == 'tom':
    print "Hello Tom"
elif name == 'jim':
    print "Hello Jim"
else:
    print "I don't know any "+name


I don't know any fred

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'


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"


Divisible by 3

==== 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)


5
4
3
2
1
Blastoff!

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)


What's your name:ted
How old are you:40
Hello ted
Boy you are old
<type 'int'>

In [ ]: