CP215 Application Design

Similar documents
Decision Making in C

Text Input and Conditionals

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Flow Control: Branches and loops

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Flow Control. CSC215 Lecture

Visualize ComplexCities

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CSI33 Data Structures

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Information Science 1

Conditionals and Recursion. Python Part 4

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

ENGR 101 Engineering Design Workshop

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

5. Control Statements

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Key Differences Between Python and Java

Language Reference Manual

Information Science 1

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

JME Language Reference Manual

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Test #2 October 8, 2015

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

C++ for Python Programmers

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Full file at C How to Program, 6/e Multiple Choice Test Bank

Basic Syntax - First Program 1

Python: common syntax

CSCE 110: Programming I

The L Line. The Express Line to Learning. Wiley Publishing All Rights Reserved.

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Working with JavaScript

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Lab 4 Fruitful Functions

Introduction to Python (All the Basic Stuff)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CSC Software I: Utilities and Internals. Programming in Python

The for Loop. Lesson 11

Begin to code with Python Obtaining MTA qualification expanded notes

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

COMPUTER PROGRAMMING LOOPS

MITOCW watch?v=0jljzrnhwoi

Introduction to Java & Fundamental Data Types


Accelerating Information Technology Innovation

corgi Language Reference Manual COMS W4115

Unit E Step-by-Step: Programming with Python

Introduction to Bioinformatics

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

CS 1301 Exam 1 Answers Fall 2009

Making Decisions In Python

Ruby: Introduction, Basics

Python for Informatics

Decisions, Decisions. Testing, testing C H A P T E R 7

Conditional Expressions and Decision Statements

Boolean Expressions (Conditions)

4.2 Function definitions the basics

Quiz 1: Functions and Procedures

Jarek Szlichta

Ruby: Introduction, Basics

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS 106 Introduction to Computer Science I

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Introduction. C provides two styles of flow control:

C++ Programming: From Problem Analysis to Program Design, Third Edition

Programming: Part I. In this section of notes you will learn about how to write simple programs using JES. Translators

Pace University. Fundamental Concepts of CS121 1

Lab 5 - Repetition. September 26, 2018

Control Structures. CIS 118 Intro to LINUX

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python Unit

8. Control statements

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

An Introduction to Python

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

QUIZ: What value is stored in a after this

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

LOOPS. Repetition using the while statement

Creating a C++ Program

Statements 2. a operator= b a = a operator b

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Accelerating Information Technology Innovation

ME30 Lab3 Decisions. February 20, 2019

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Session 1: Introduction to Python from the Matlab perspective. October 9th, 2017 Sandra Diaz

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

PHP 1. Introduction Temasek Polytechnic

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

APCS Semester #1 Final Exam Practice Problems

Transcription:

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!