In [1]:
from __future__ import print_function

We will review for loops, while loops and functions

1. If statements

In [16]:
x=1 
if x==10:                   
    print('x=0') 
elif x==2:
    print('x=2')
else:
    print('x is neither 0 nor 2')
x is neither 0 nor 2
In [ ]:
            if something (e.g. x==0)
				        |
				        |
                     /-----\
				    /       \
			      True      False
				  /           \
			     /             \
			    /               \ 
        Do something      if something (e.g. x==2)
    (e.g. print ('x=0'))            |
                                    |
                                 -------
                                /       \
                               /         \
                            True        False 
                             /             \
                            /               \ 
                Do Something        DO something 
        (e.g. print ('x=2'))     (e.g. print ('x is neither 0 nor 2')   

2. For loops

In [8]:
fruits=['organge', 'banana', 'apple', 'pinapple']

How do I print all of the fruits?

  1. using range
  2. not using range
In [13]:
#Using range
print('-----Using range-------')
for i in range(len(fruits)): 
    print(fruits[i])

print('-----Not using range-----')
#Not using range
for i in fruits:
    print(i)
-----Using range-------
organge
banana
apple
pinapple
-----Not using range-----
organge
banana
apple
pinapple
In [7]:
fruits=['organge', 'banana', 'apple', 'pinapple']
for f in fruits: 
    print(i)
    
#What does this print? What happened?
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-d94857f63e58> in <module>()
      1 fruits=['organge', 'banana', 'apple', 'pinapple']
      2 for f in fruits:
----> 3     print(i)
      4 
      5 #What does this print? What happened?

NameError: name 'i' is not defined
In [5]:
#What if I only want to print orange?
#1. Using continue
for i in range(len(fruits)):
    if i>0: 
        continue
    print(fruits[i])
organge
In [6]:
#What if I only want to print orange?
#2. Using break
for i in range(len(fruits)):
    if i>0: 
        break
    print(fruits[i])
organge
In [19]:
#What if I only want to print pinapple?
for i in range(len(fruits)):
    if i<len(fruits): #What is wrong?
        continue
    print(fruits[i])
In [20]:
#What if I only want to print pinapple?
for i in range(len(fruits)):
    print(i)#print i to understand what is going on
    if i<len(fruits): #It continues until end of list
        continue
    print(fruits[i])
0
1
2
3
In [23]:
#What if I only want to print pinapple?
for i in range(len(fruits)):
    if i<len(fruits)-1: #now it continues until pinapple
        continue
    print(fruits[i]) 
    #when it gets to pinapple, it doesn't continue anymore
pinapple
In [30]:
#What if I want to print orange, banana, pinapple?
for i in range(len(fruits)):
    if i % 2==0: #What is wrong?
        print(fruits) #what is wrong?
        
['organge', 'banana', 'apple', 'pinapple']
['organge', 'banana', 'apple', 'pinapple']
In [31]:
#What if I want to print orange, apple?
fruits=['organge', 'banana', 'apple', 'pinapple']
for i in range(len(fruits)):
    if i % 2==0: 
        print(fruits[i])
organge
apple

3. While loops

In [32]:
fruits=['organge', 'banana', 'apple', 'pinapple']

#1. Print all fruits
y=0
while y<len(fruits):
    print(fruits[y])
    y += 1
organge
banana
apple
pinapple
In [39]:
#What if I only want to print orange?
fruits=['organge', 'banana', 'apple', 'pinapple']
i=0
while i<len(fruits): 
    print(fruits[i]) 
    if i>0: #what is wrong?
        break
    i +=1 #What happens if I comment this out?
organge
banana
In [41]:
#What if I only want to print orange?
fruits=['organge', 'banana', 'apple', 'pinapple']
i=0
while i<len(fruits): 
    print(fruits[i]) 
    i +=1 
    if i>0: 
        break
organge
In [35]:
#What if I only want to print pinapple?
fruits=["organge", "banana", "apple", "pinapple"]
i=0

while i<len(fruits):
    if i==len(fruits)-1:
        print(fruits[i])
    i += 1
pinapple
In [2]:
#What if I want to print banana, pinapple?
fruits=['organge', 'banana', 'apple', 'pinapple']
i=0
while i<len(fruits):
    if i % 2==1:
        print(fruits[i])
    i +=1 
banana
pinapple