Arithmetic Operations


In [1]:
2 + 2


Out[1]:
4

In [2]:
(1 + 3) * 4


Out[2]:
16

In [3]:
cups_of_flour = 5
cups_of_flour * 0.5


Out[3]:
2.5

In [4]:
1/2


Out[4]:
0

In [5]:
1.0/2


Out[5]:
0.5

In [6]:
1/2.0


Out[6]:
0.5

In [7]:
"Hello" + 1


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-465e48023fdd> in <module>()
----> 1 "Hello" + 1

TypeError: cannot concatenate 'str' and 'int' objects

In [ ]:
"Hello" + str(1)

In [ ]:
"Hello" + `1`

In [ ]:
"hello" * 5

What is None?


In [8]:
a = 1
a


Out[8]:
1

In [9]:
a = None
a

In [10]:
not None


Out[10]:
True

In [11]:
None != False


Out[11]:
True

Control Statements

Note: Python uses indentation to group things.


In [12]:
if 2 > 1:
    print "Hello World!"


Hello World!

In [13]:
if 1 > 2:
    print "Hello World!"
else:
    print "World is not a beautiful place!"


World is not a beautiful place!

In [14]:
if 1 > 2:
    print "Hello World!"
elif 2 > 3:
    print "World is even better!"
elif 3 > 4:
    print "I don't want to live here!"
else:
    print "World is not a beautiful place!"


World is not a beautiful place!

In [15]:
a = None
if a is None:
    print "a is None"
else:
    print "a is not None"
    
b = 10
if b == None:
    print "b is None"
else:
    print "b is not None"


a is None
b is not None

if / else with strings and lists


In [16]:
list1 = [1, 2, 4, 2, 100]
s = "Hello world"

In [17]:
if 4 in list1:
    print "Hey! 4 exists in list1"


Hey! 4 exists in list1

In [18]:
if 2 in list1:
    i = list1.index(2)
    print "Search successful. Found at index {0}".format((i+1))


Search successful. Found at index 2

In [19]:
if 'o' in s:
    print "'o' exists!"


'o' exists!

In [20]:
if 'llo' in s:
    print "'llo' exists!"


'llo' exists!

In [21]:
if 'house' not in s:
    print "house not found!"


house not found!

Exercise

Q. Six is a Mob

Create a list with at least 6 names inside it. Write an if/else test case to test the following

  • If there are more than 5 people, a message is printed about there being a mob in the room.
  • If there are 3-5 people, a message is printed about the room being crowded.
  • If there are 1 or 2 people, a message is printed about the room not being crowded.
  • If there are no people in the room, a message is printed about the room being empty.

Loops

  • while loop
  • for loop

While loop syntax

while <condition>:
    # do something
    # do something

In [22]:
count = 0
while count < 10:
    print count
    count += 1  # count = count + 1


0
1
2
3
4
5
6
7
8
9

An infinite loop

while <a true condition>:
    # do something

a true condition can be anything which evaluates to True e.g. True, 1, not False, etc.

Nested while loops


In [23]:
i = 0;
while i < 5:
    j = 5;
    while j > 0:
        print i, j
        j -= 1
    i += 1


0 5
0 4
0 3
0 2
0 1
1 5
1 4
1 3
1 2
1 1
2 5
2 4
2 3
2 2
2 1
3 5
3 4
3 3
3 2
3 1
4 5
4 4
4 3
4 2
4 1

Using break, pass and continue


In [24]:
count = 0
while count < 10:
    print count
    count += 1  # count = count + 1
    if count == 5:
        break


0
1
2
3
4

In [25]:
count = 0
while count < 10:
    count += 1  # count = count + 1
    if count < 5:
        continue
    print count


5
6
7
8
9
10

In [26]:
count = 0
while count < 10:
    print count
    count += 1  # count = count + 1
    if count < 5:
        pass
    else:
        break


0
1
2
3
4

Question:

Do prime factorization using while loops