CP215 Application Design
Alphabet's Project Loon to deliver Internet this year in Indonesia Tech News!
Tech News! Alphabet's Project Loon to deliver Internet this year in Indonesia Casey Reas computational art
Hacker's Tip of the Day Eat your own dog food Use the programs you write You'll notice when they're bad You'll have to fix them because they'll drive you crazy
Komodo Edit
Komodo Edit Tabbed editing
Komodo Edit Code syntax highlighting
Komodo Edit Code Folding Fold and unfold code visibility
Linux: Installing Software Option 1: - Manually install a pre-built binary file - Skype for Linux
Linux: Installing Software Option 2: - Compile code from source - configure; make; make install - liblinear
Linux: Installing Software Option 3: - Install binary (or source) from a repository - Ubuntu: apt-get install - Fedora yum install
Linux: Installing Software Searching Repositories - Synaptic - Ubuntu Software Center
Boolean Variables Simple variables that only store one of two values: True or False
Boolean Variables Simple variables that only store one of two values: True or False Can be assigned directly or with a boolean expression my_boolean = True my_boolean = 3 < 5 my_boolean = x == y
Python Conditionals Branching statements
Python Conditionals Branching statements Make a decision about which code to execute next age = int(raw_input( Enter your age: )) if age > 22: print You're old else: print You're not yet old
Python Conditionals Branching statements age > 22 is a boolean condition Make a decision about which code to execute next age = int(raw_input( Enter your age: )) if age > 22: print You're old else: print You're not yet old
Python Conditionals Branching statements Use colon instead of curly braces Make a decision about which code to execute next age = int(raw_input( Enter your age: )) if age > 22: print You're old else: print You're not yet old
Python Conditionals Branching statements Make a decision about which code to execute Code must be indented next age = int(raw_input( Enter your age: )) if age > 22: print You're old else: print You're not yet old
Python Conditionals Branching statements Make This a decision code only about executed which if the code to execute next condition was true age = int(raw_input( Enter your age: )) if age > 22: print You're old else: print You're not yet old
Python Conditionals Branching statements Make a decision about which code to execute next If the condition was false, then do this block age = int(raw_input( Enter your age: )) if age > 22: print You're old else: print You're not yet old
Python Conditionals if BOOLEAN_CONDITION: # Do something elif OTHER_BOOLEAN_CONDITION: # Do something else else: # Do this if nothing else
Boolean Comparison Operators a == b a < b a > b a <= b a >= b a!= b a equals b a is less than b a is greater than b a is less than or equal to b a is greater than or equal to b a is not equal to b
Boolean Logic Operators Suppose A and B are boolean expressions A and B A or B not A True only if both A and B are True True if either A or B is True True only if A is not True
Boolean Truth Table Suppose A and B are boolean expressions A B A and B True True True True False False False True False False False False
Boolean Truth Table - Exercise Suppose A, B, and C are boolean expressions Fill in the truth table for (A or B) and not C A B C (A or B) and not C
Boolean Truth Table - Exercise Suppose A, B, and C are boolean expressions Fill in the truth table for (A or B) and not C A B C (A or B) and not C T T T F T T F T T F T F T F F T F T T F F T F T F F T F F F F F
Nested If Statements age = int(raw_input( Enter your age: )) charisma = int(raw_input( Enter your charisma: )) if age > 22: if charisma > 50: print You're old, but charismatic else: print You're old and not very charismatic else: if charisma > 50: print You're young and charismatic else: print You're young, but not very charismatic
Nested If Statements This inner if statement is only tested if the outer condition is True age = int(raw_input( Enter your age: )) charisma = int(raw_input( Enter your charisma: )) if age > 22: if charisma > 50: print You're old, but charismatic else: print You're old and not very charismatic else: if charisma > 50: print You're young and charismatic else: print You're young, but not very charismatic
Nested If Statements The inner if and else need to line up horizontally age = int(raw_input( Enter your age: )) charisma = int(raw_input( Enter your charisma: )) if age > 22: if charisma > 50: print You're old, but charismatic else: print You're old and not very charismatic else: if charisma > 50: print You're young and charismatic else: print You're young, but not very charismatic
Nested If Statements This block has to be indented further still age = int(raw_input( Enter your age: )) charisma = int(raw_input( Enter your charisma: )) if age > 22: if charisma > 50: print You're old, but charismatic else: print You're old and not very charismatic else: if charisma > 50: print You're young and charismatic else: print You're young, but not very charismatic
Nested If Statements Can you rewrite this code without age = int(raw_input( Enter your age: )) charisma = int(raw_input( Enter nesting your if statements? charisma: )) if age > 22: if charisma > 50: print You're old, but charismatic else: print You're old and not very charismatic else: if charisma > 50: print You're young and charismatic else: print You're young, but not very charismatic
Iteration Counting for loop for i in range(10): print hello for i in range(50, 100): print i for i in range(0, 100, 3): print i
Iteration Traversal for loop my_list = [1, 1, 2, 3, 5, 8, 13] for value in my_list: print value for value in my_list[:4]: print value
Iteration Traversal for loop my_string = colorado college for character in my_string: print ord(character)
Iteration Traversal for loop my_list = [1, 1, 2, 3, 5, 8, 13] for value in my_list: value = 10
Iteration while loop count = 0 while count < 10: print count count += 1
Exercise Given the following matrix (list of lists), write code that prints out each individual element separately matrix = [[5, 5, 6], [2, 7, 9], [1, 0, 1]]
Functions total = 0 for number in numbers: total += number average = float(total) / len(numbers)
Function: A mapping Domain Range 5 17 88 14 6 66 16 0.6
Function: A mapping Domain Range input % 2??
Function: A mapping Domain Range average??
Function: A blackbox input % 2 Input Output
Using Functions Define the function (Create the inner workings of the blackbox) Use/call the function (Give input to the blackbox and get output back)
Define Function average def average(numbers): total = 0 for number in numbers: total += number return float(total) / len(numbers)
Define Function Python keyword def for defining new functions average def average(numbers): total = 0 for number in numbers: total += number return float(total) / len(numbers)
Define Function average def average(numbers): total = 0 for number in numbers: total += number In parentheses list the variable names for the incoming input values return float(total) / len(numbers)
Define Function average The return statement defines the def average(list_numbers): output of the function total = 0 for number in numbers: total += number return float(total) / len(numbers)
Define Function average def average(numbers): return float(sum(numbers)) / len(numbers)
Use/Call Function my_list = [3, 4, 7, 8, 10] your_list = [12, 14, 16, 88] my_list_average = average(my_list) your_list_average = average(your_list)
Use/Call Function The execution flow goes into the average function my_list = [3, 4, 7, 8, 10] your_list = [12, 14, 16, 88] my_list_average = average(my_list) your_list_average = average(your_list)
Wacky call? wrong_type = hello list_average = average(wrong_type)
Documenting Code # This function computes the average # of a list of numbers def average(numbers): return float(sum(numbers)) / len(numbers)
Automatically Documenting Code: docstring def average(numbers): '''This function computes the average of a list of numbers''' return float(sum(numbers)) / len(numbers)
Automatically Documenting Code: docstring Always the first line after a function header. def average(numbers): '''This function computes the average of a list of numbers''' return float(sum(numbers)) / len(numbers)
Automatically Documenting Code: docstring Triple-quoted string can extend multiple lines. def average(numbers): '''This function computes the average of a list of numbers''' return float(sum(numbers)) / len(numbers)
Accessing docstring # StatsFunctions.py def average(numbers): '''This function computes the average of a list of numbers''' return float(sum(numbers)) / len(numbers) >>> import StatsFunctions >>> help(statsfunctions.average)
Automatic generation of documentation ~$ pydoc -w StatsFunctions wrote StatsFunctions.html ~$ pydoc -g
Add other documentation fields '''This file contains functions for performing statistical operations''' author = Matthew version = 1.0 def average(numbers): '''This function computes the average of a list of numbers''' return float(sum(numbers)) / len(numbers)
Exercise Write a function called modtwo that takes an integer as input and returns its value modulus 2. Properly docstring your function and your file. Use pydoc to generate the HTML documentation file.
New Assignment
Break!