• Conditional statements execute code if certain conditions are true.
  • There are three basic forms of conditional code.

This is a plain if statement:

if user == 'Mike' : 
  print "Welcome Instructor."

This is an if/else statement:

if user == 'Mike' :
  print "Welcome Instructor."
  print "Welcome Student."

This is a ladder:

if user == 'Mike' :
  print "Welcome Instructor."
elif user == 'Dan' : 
  print "Welcome Instructor's Brother"   
  print "Welcome Student."

The else is always optional:

if user == 'Mike' :
  print "Welcome Instructor."
elif user == 'Dan' : 
  print "Welcome Instructor's Brother"   
  • In and if/elif/else construction only one of the alternatives is ever run.
  • If there's no else none of the alternatives may be chosen.
  • You can use any Python statements inside of an if block
  • You can use any Python statements in the conditional of an if block.

Using a function as an if conditional:

def is_superuser(username) : 
  if username == 'Mike' : 
    return True
    return False 
def main() :
  program, user = sys.argv 
  if is_superuser(user) : 
    print ('Welcome Administrator')
  • You can combine statements that are logically True or False using logical operators
    • and is True when both inputs are True
    • or is True when either input is True
    • not reverses True and False

Here's an example of using a logical operator in an if statement:

if user == 'Mike' or user == 'root' : 
  print 'Welcome admin.' 


Here's how to declare a list:

list_var = ['value1', 3, 'Elephant', 'Mike', False, 10.3]
  • Lists can contain any type of variable in Python
    • Including other lists!

Here's a list of lists:

list_of_lists = [ [1, 2, 3], ['Mike', 'Bob', 'Sandy'], ['Lion', 'Tiger', 'Bear'] ] 
  • There are a few important operations on lists
    • We'll talk more about them next week.
    • The append() function adds a value to the back of the list.
    • The pop() function does the opposite of append()
    • The clear() function deletes the contents of the list.
    • The len() function takes a list as an argument and returns the number of elements in the list.

Here's a few examples of list manipulation:

>>> my_list = []
>>> my_list 
>>> my_list.append('Lion') 
>>> my_list 
>>> my_list.append('Tiger')
>>> my_list.append('Bear')
>>> my_list
['Lion', 'Tiger', 'Bear']
>>> my_list.pop()
>>> my_list 
['Lion', 'Tiger']
>>> len(my_list) 
>>> my_list.clear()
>>> len(my_list) 
  • Strings can be treated as lists of letters.
  • This is useful sometimes when you want to analyze what's in a string.
>>> str = "Hello World" 
>>> str[0] 
>>> str[1]                                                                                                           
>>> str[-1]

The for Loop

  • Lists are interesting to know but you can't really use them until you understand loops.
  • Loops are structures that repeat a block of statements.
  • Loops look a bit like if statements.
  • There are two kinds of loops:
    • Definite loops repeat a known number of times.
    • Indefinite loops repeat an unknown number of times.
  • The for loop is the definite loop in Python.
  • The for loop is perfect for doing something for every item in a list.

Here's a loop that prints the contents of a list:

animals = ['Lion', 'Tiger', 'Bear'] 
for animal in animals : 
  print (animal + 's')
print ('Oh my!')

Notice some important things about the for loop:

  • The loop runs once for each element in the animals array.
  • The animal variable is created to give access to the element in animals
  • The range() function gives you a range of numbers
  • range() is great when combined with for loops.
for number in range(0, 1) : 
  print ('The number is:', number) 
  • The range() function starts at the first argument.
  • It ends at the last argument minus one.
  • So… range(0, 5) is…
    • [0, 1, 2, 3, 4]
  • For loops are much more convenient in Python than other languages.
    • In most other languages for loops use a numerical index.
    • You can do that in Python too if you like.

Here's an example of how you can use the range() function to make Python's for loop a bit more like C++ or Java:

my_list = ['one', 'two', 'three'] 
for i in range(0, 3) : 
  print (f"my_list[{i}] = {my_list[i]}") 

Wait! That code has a problem. Can you spot it? What happens if the list contains more elements? You should make sure you always loop through ever element of the list. You can do that like this:

my_list = ['one', 'two', 'three', 'four'] 
for i in range(0, len(my_list)) : 
  print (f"my_list[{i}] = {my_list[i]}") 

That's better. But, there's an easier way in Python to get indexes for lists.

for i, value in enumerate(my_list) : 
  print (f"my_list[{i}] = {value}") 
  • There are many clever uses of a for loop in Python
  • Any function that returns a list can be given to a for loop.
    • For example the readlines() (notice the s!) function on a file returns list of lines
    • Functions like readlines() are very efficient because they don't read the whole file into memory

Here's an example of a function that tests to see if a word is in the dictionary:

def is_word(word) : 
    with open ('/usr/share/dict/american-english') as d : 
        for dict_word in d.readlines() : 
            if word == dict_word.strip() :
                return True
    return False

Install the Dictionary

Before that function can work you have to install the English dictionary into your workspace:

$ sudo apt-get install wamerican

The while Loop

  • A while loop is an indefinite loop.
  • Use it when you don't know how many times your loop should run
  • The while loop continues so long as a condition is True
  • When the condition is False the while loop terminates.
  • Your code will have fewer while loops than for loops.
    • But they're still essential.

Here's an example of using a while loop to validate user input:

def get_number() : 
    '''Make the user pick a number between 1 and 100, do not allow invalid numbers.''' 
    number = 0
    while not (number > 1 and number < 100) : 
        number = int(input('Pick a number between 1 and 100: '))
    print ('Thanks!')
    return number

The get_number() function won't return until the user chooses a valid number. Here's an example of how it works:

$ python3.6 ./ 
Pick a number between 1 and 100: 0
Pick a number between 1 and 100: -1
Pick a number between 1 and 100: 100
Pick a number between 1 and 100: 99

This code can still fail. How could you add a try/except block to catch the case where the user types something that's not a number?

  • One other common use of a while loop is when you want to do something forever (an infinite loop).
  • Infinite loops are at the heart of every game
  • The will obviously not run forever, but it waits for the user to tell it to exit.

Here's an example of a “game” that uses an infinite loop:

def dice() : 
    while True : 
        command = input('Type "roll" to roll the dice or "quit" to exit: ')
        if command == 'roll' : 
            die1 = random.randint(1, 6)
            die2 = random.randint(1, 6)
            if die1 == 1 and die2 == 1 : 
                print ("The dice say snake eyes!")
                print (f"The dice say {die1} and {die2}")
        elif command == 'quit' :