Conditions


In [1]:
x = 2
print x == 2 # prints out True
print x == 3 # prints out False
print x < 3  # prints out True


True
False
True

In [8]:



Out[8]:
True

In [11]:
name = "John"
age = 23
if name == "John" and age == 23 :
    print "Your name is John, and you are also 23 years old."

if name == "John" or name == "Rick":
    print "Your name is either John or Rick."


Your name is John, and you are also 23 years old.
Your name is either John or Rick.

In [17]:
age=50
if age<0:
    print('age<0')
elif age <50:
    print('age<50')
else:
    print('age>=50')


age>=50

In [18]:
age = input("Age of the dog: ")
print
if age < 0:
	print "This can hardly be true!"
elif age == 1:
	print "about 14 human years"
elif age == 2:
	print "about 22 human years"
elif age > 2:
	human = 22 + (age -2)*5
	print "Human years: ", human

### 
raw_input('press Return>')


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-18-3621194f2dae> in <module>()
----> 1 age = input("Age of the dog: ")
      2 print
      3 if age < 0:
      4         print "This can hardly be true!"
      5 elif age == 1:

/Library/Python/2.7/site-packages/IPython/kernel/zmq/ipkernel.pyc in <lambda>(prompt)
    362         if content.get('allow_stdin', False):
    363             raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
--> 364             input = lambda prompt='': eval(raw_input(prompt))
    365         else:
    366             raw_input = input = lambda prompt='' : self._no_raw_input()

/Library/Python/2.7/site-packages/IPython/kernel/zmq/ipkernel.pyc in <lambda>(prompt)
    361         # raw_input in the user namespace.
    362         if content.get('allow_stdin', False):
--> 363             raw_input = lambda prompt='': self._raw_input(prompt, ident, parent)
    364             input = lambda prompt='': eval(raw_input(prompt))
    365         else:

/Library/Python/2.7/site-packages/IPython/kernel/zmq/ipkernel.pyc in _raw_input(self, prompt, ident, parent)
    763             except KeyboardInterrupt:
    764                 # re-raise KeyboardInterrupt, to truncate traceback
--> 765                 raise KeyboardInterrupt
    766             else:
    767                 break

KeyboardInterrupt: 
Age of the dog: 2

In [25]:
a=3
b=2
max1 = 1 if (a == 1) else 0
print max


3

In [10]:



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-2951250487bf> in <module>()
----> 1 map(1,2)

TypeError: argument 2 to map() must support iteration

In [18]:
if name in ["John", "Rick"]:
    print "Your name is either John or Rick."


Your name is either John or Rick.

In [24]:
age = 6
if age in [x for x in range(10)]:
    print age


6

In [4]:
x = 2
if x == 2:
    print "x equals two!"
else:
    print "x does not equal to two."


x equals two!

In [5]:
x = [1,2,3]
y = [1,2,3]
print x == y # Prints out True
print x is y # Prints out False


True
False

In [6]:
print not False # Prints out True
print (not False) == (False) # Prints out False


True
False

Loops


In [25]:
primes = [2, 3, 5, 7]
for prime in primes:
    print prime


2
3
5
7

In [39]:
xs = [100,200,300,400]
xs2 = []
for x in xs:
    xs2.append(x+1)

In [41]:
for x in range(len(xs)):
    print xs[x]


100
200
300
400

In [50]:
range(6,-1,-1)


Out[50]:
[6, 5, 4, 3, 2, 1, 0]

In [73]:
# Prints out the numbers 0,1,2,3,4
for xfaas in xrange(5): # or range(5)
    print xfaas

# Prints out 3,4,5
for x in xrange(3, 6): # or range(3, 6)
    print x

# Prints out 3,5,7
for x in xrange(3, 8, 2): # or range(3, 8, 2)
    print x


0
1
2
3
4
3
4
5
3
5
7

In [60]:
# Prints out 0,1,2,3,4

x=[]
count = 0
while count < 5:
    count += 1  # This is the same as count = count + 1
    x.append(count)

In [61]:
x


Out[61]:
[1, 2, 3, 4, 5]

In [62]:
# Prints out 0,1,2,3,4

count = 0
while True:
    print count
    count += 1
    if count >= 5:
        break


0
1
2
3
4

In [63]:
# Prints out only odd numbers - 1,3,5,7,9
for x in xrange(10):
    # Check if x is even
    if x % 2 == 0:
        continue
    print x


1
3
5
7
9

In [64]:
phonebook = {
    "John" : 938477566,
    "Jack" : 938377264,
    "Jill" : 947662781
}

In [72]:
for x, y in phonebook.iteritems():
    print "Phone number of %s is %d" % (x, y)


Phone number of Jill is 947662781
Phone number of John is 938477566
Phone number of Jack is 938377264

In [70]:
for key in phonebook.iterkeys():
    print key


Jill
John
Jack

In [78]:
for x in range(1,10):
    for y in range(1,10):
        print(x * y)


1
2
3
4
5
6
7
8
9
2
4
6
8
10
12
14
16
18
3
6
9
12
15
18
21
24
27
4
8
12
16
20
24
28
32
36
5
10
15
20
25
30
35
40
45
6
12
18
24
30
36
42
48
54
7
14
21
28
35
42
49
56
63
8
16
24
32
40
48
56
64
72
9
18
27
36
45
54
63
72
81