if Statements


In [1]:
x = int(input("Please enter an integer: "))
x


Please enter an integer: 42
Out[1]:
42

In [2]:
if x < 0:
    x = 0
    print('Negative changed to zero')
elif x == 0:
    print('Zero')
elif x == 1:
    print('Single')
else:
    print('More')


More

for Statements


In [7]:
# Measure some strings:

words = ['cat', 'window', 'defenestrate']

for w in words:
    print(w, len(w))


cat 3
window 6
defenestrate 12

In [8]:
for w in words[:]:
    if len(w) > 6:
        words.insert(0, w)

words


Out[8]:
['defenestrate', 'cat', 'window', 'defenestrate']

The range() Function


In [9]:
for i in range(5):
    print(i)


0
1
2
3
4

In [12]:
list(range(5, 10))


Out[12]:
[5, 6, 7, 8, 9]

In [15]:
list(range(0, 10, 3))


Out[15]:
[0, 3, 6, 9]

In [16]:
list(range(-10, -100, -30))


Out[16]:
[-10, -40, -70]

In [17]:
a = ['Mary', 'had', 'a', 'little', 'lamb']

for i in range(len(a)):
    print(i, a[i])


0 Mary
1 had
2 a
3 little
4 lamb

In [18]:
range(10)


Out[18]:
range(0, 10)

In [19]:
type(range(10))


Out[19]:
range

In [20]:
r1 = range(2)

In [23]:
dir(r1)


Out[23]:
['__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'count',
 'index',
 'start',
 'step',
 'stop']

In [24]:
iter(r1)


Out[24]:
<range_iterator at 0x7f811054cfc0>

In [25]:
ir1 = iter(r1)

In [27]:
dir(ir1)


Out[27]:
['__class__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__length_hint__',
 '__lt__',
 '__ne__',
 '__new__',
 '__next__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']

In [28]:
next(ir1)


Out[28]:
0

In [29]:
next(ir1)


Out[29]:
1

In [30]:
next(ir1)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-30-ecae289c5260> in <module>()
----> 1 next(ir1)

StopIteration: 

In many ways the object returned by range() behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.

We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the for statement is such an iterator. The function list() is another; it creates lists from iterables:


In [31]:
list(range(2))


Out[31]:
[0, 1]

break and continue Statements, and else Cluases on Loops


In [32]:
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print("{0} equals {1} * {2}".format(n, x, n//x))
            break
    else:
        # loop fell through without finding a factor
        print(n, ' is a prime number')


2  is a prime number
3  is a prime number
4 equals 2 * 2
5  is a prime number
6 equals 2 * 3
7  is a prime number
8 equals 2 * 4
9 equals 3 * 3

In [33]:
for num in range(2, 10):
    if num % 2 == 0:
        print("Found an even number", num)
        continue
    print("Found a number", num)


Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9

pass Statements


In [4]:
while True:
    pass


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-4-cccdd40a5a4c> in <module>()
      1 while True:
----> 2     pass

KeyboardInterrupt: 

Note

Press I, I to interrupt the ipython kernel


In [5]:
class MyEmptyClass:
    pass

In [6]:
def initlog(*args):
    pass  ## TODO: To implement this!

In [ ]: