Three-Way Decisions

if, elif, else


In [52]:
t=0

if t > 60:
    print('its very hot')
elif t > 50:
    print('its hot')
elif t > 40:
    print('its warm')
else:
    print('its cool')


its cool

In [56]:
t=55

if t > 40:
    print('its very hot')
elif t > 50:
    print('its hot')
elif t > 60:
    print('its warm')
else:
    print('its cool')


its very hot

S be carefull!

while Loop


In [109]:
i=0
while i<10:
    print(i)
    i+=1


0
1
2
3
4
5
6
7
8
9

Queez

Once upon a time, there was king, who wanted lots of soldiers. So he commanded every couple in the country to have children, until their first dauter born. Then the family is banned from having any more child. What will be the ratio of boy/girls in this country?


In [112]:
from random import randint

children = 0
boy = 0

for i in range(10000):
    gender = randint(0,1) # boy=1, girl=0
    children += 1
    while gender != 0:
        boy += gender
        gender = randint(0,1)
        children += 1
print(boy/children)


0.5002498750624688

Control Statments

break, continue and pass


In [113]:
for i in range(10):
    print(i)
    if i == 5:
        break


0
1
2
3
4
5

In [116]:
for i in range(10):
    print(i)
    if i > 5:
        continue
    print("Hey")


0
Hey
1
Hey
2
Hey
3
Hey
4
Hey
5
Hey
6
7
8
9

In [122]:
def func():
    pass

In [123]:
func()

tuple


In [126]:
t = (0,1,'test')

In [129]:
print(t)
t[0]=1


(0, 1, 'test')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-129-98ddcef64135> in <module>()
      1 print(t)
----> 2 t[0]=1

TypeError: 'tuple' object does not support item assignment

In [131]:
(1,)


Out[131]:
(1,)

Dictionaries

items get keys pop update values


In [138]:
d = {}
d['name'] = 'Hamed'
d['family name'] = 'Seyed-allaei'
d[0]=12
d['a']=''
print(d)
print(d['name'])
print(d[0])


{'family name': 'Seyed-allaei', 0: 12, 'name': 'Hamed', 'a': ''}
Hamed
12

In [140]:
for i,j in d.items():
    print(i,j)


family name Seyed-allaei
0 12
name Hamed
a 

set

in, not in, len(), ==, !=, <=, <, |, &, -, ^


In [145]:
a = set(['c', 'a','b','b'])
b = set(['c', 'd','e'])
print(a,b)


{'a', 'c', 'b'} {'d', 'e', 'c'}

In [151]:
a | b


Out[151]:
{'a', 'b', 'c', 'd', 'e'}

In [153]:
a & b


Out[153]:
{'c'}

In [155]:
a - b


Out[155]:
{'a', 'b'}

In [157]:
b - a


Out[157]:
{'d', 'e'}

In [158]:
a ^ b


Out[158]:
{'a', 'b', 'd', 'e'}

List comprehention


In [161]:
l = []
for i in range(10):
    l.append(i*i)
print(l)


[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [167]:
[i*i for i in range(10)]


Out[167]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [169]:
{i:i**2 for i in range(10)}


Out[169]:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

Generators

next()


In [189]:
def myrange(n):
    i = 0
    while i < n:
        yield i
        yield i**2
        i+=1
        
x = myrange(10)
type(x)


Out[189]:
generator

In [202]:
next(x)


Out[202]:
6

In [203]:
[i for i in myrange(10)]


Out[203]:
[0, 0, 1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36, 7, 49, 8, 64, 9, 81]

In [ ]:
for i in myrange(10):
    print(i)

Fibonacci

This time as a generator.