Lab #5-6 Follow-Up: More Python; Images. Part 1: Python Conditionals, Return Values, and Lists.

Slides:



Advertisements
Similar presentations
Conditional Execution
Advertisements

Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Analysis of Algorithms CS Data Structures Section 2.6.
Chapter 12: Testing hypotheses about single means (z and t) Example: Suppose you have the hypothesis that UW undergrads have higher than the average IQ.
The Efficiency of Algorithms
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Regression. So far, we've been looking at classification problems, in which the y values are either 0 or 1. Now we'll briefly consider the case where.
Conditional Statements Introduction to Computing Science and Programming I.
Nearest Neighbor. Predicting Bankruptcy Nearest Neighbor Remember all your data When someone asks a question –Find the nearest old data point –Return.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 5 New York University.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Chapter 4 Making Decisions
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Objectives Evaluate expressions containing square roots.
Numerical Roots & Radicals
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Computer Science 111 Fundamentals of Programming I Search Algorithms.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
What is an And Gate? It is a digital circuit that produce logical operations The logical operations are call Boolean logical Boolean operation consist.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Application of Data Programming Blocks. Objectives  Understand the use of data programming blocks and their applications  Understand the basic logic.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
BASIC LOGIC GATES. In studying digital in integrated circuits, one must start with the simples group of circuits, the SSIs or Small Scale Integrated Circuits.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS Jehan-François Pâris
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
MIT App Inventor Lesson 4 – Decision Making. Agenda Comparisons ◦ Relational Operators Conditions (state checking) ◦ Boolean Operators.
Decision Structures, String Comparison, Nested Structures
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Squares of Numbers Greater than 20 Return to Table of Contents.
 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Searching and Sorting Arrays
Algorithmic complexity: Speed of algorithms
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
Python: Control Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Line Continuation, Output Formatting, and Decision Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Searching and Sorting Arrays
Practice with loops! What is the output of each function below?
String and Lists Dr. José M. Reyes Álamo.
Introduction to Programming Using Python PART 2
Algorithmic complexity: Speed of algorithms
Introduction to Python
Linear Search Binary Search Tree
15-110: Principles of Computing
Objectives Evaluate expressions containing square roots.
Class 13 function example unstring if if else if elif else
Algorithmic complexity: Speed of algorithms
Searching and Sorting Arrays
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Control Statements.
The Binary Search by Mr. Dave Clausen
Presentation transcript:

Lab #5-6 Follow-Up: More Python; Images

Part 1: Python Conditionals, Return Values, and Lists

if :... elif :... elif... else:... Conditionals: General Form

if IQ > 140: print("OMG genius!") elif IQ > 130: print("Wicked smaht!") elif IQ > 120: print("Way above average.") elif IQ > 110: print("Still no slouch.") elif IQ > 100: print("College material.") elif IQ > 90: print("Hope springs eternal!") else: print("Dude, where's my car?") Example

x > y# x greater than y x < y# x less than y x >= y# x greater than or equal to y x <= y# x less than or equal to y x != y# x not equal to y Comparison Operators

x > y and y > z x > y or y > z not (x > y) Logical / Boolean Operators G. Boole ( )

Logic Gates: The Building Blocks of Digital Circuits

Logic Gates: The Building Blocks of Digital Circuits resistor transistor

# The grading scale will be A; A-; B+; # B; B-; C+; C; C-; # D+; D; D-; below 60 F. if average >= 93 and average <= 100: grade = 'A' elif average >= 90 and average <= 92: grade = 'A-' elif average >= 87 and average <= 89: grade = 'B+' elif average >= 83 and average <= 86: grade = 'B' elif average >= 80 and average <= 82: grade = 'B-'... elif average < 60: grade = 'F' Putting It All Together

# The grading scale will be A; A-; B+; # B; B-; C+; C; C-; # D+; D; D-; below 60 F. if average >= 93: grade = 'A' elif average >= 90: grade = 'A-' elif average >= 87: grade = 'B+' elif average >= 83: grade = 'B' elif average >= 80: grade = 'B-'... else: grade = 'F' Less Is More!

if average >= 93: grade = 'A' elif average >= 90: grade = 'A-' elif average >= 87: grade = 'B+' elif average >= 83: grade = 'B' elif average >= 80: grade = 'B-'... else: grade = 'F' Dimes Pennies Nickels Quarters

Returning Values from Functions For many robot applications, functions just need to cause side-effects: def wiggle(speed, waitTime): rotate(-speed) wait(waitTime) rotate(speed) wait(waitTime) stop() Outside of robotics, functions are described by what value they return as well ….

Returning Values from Functions def square(x): return x * x # without square(), we'd have to do this: dst = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) # instead of this: dst = sqrt(square(x1-x2) + square(y1-y2))

Multiple return points def absoluteValue(x): if x < 0: return –x elif x > 0: return x elif x == 0: return 0

Less is more! def absoluteValue(x): if x < 0: return –x else: return x

Putting it all together: Writing our own sqrt() function For perfect squares (0, 1, 4, 9, 16, 25, …), we typically just memorize them For other numbers, we need a function: from math import sqrt How does sqrt() work? E.g., what is sqrt(3) ?

Putting it all together: Writing our own sqrt() function How does sqrt() work? E.g., what is sqrt(3) ? It must be between 0 and 3, since a square root can't be negative, and a number can't be bigger than its own square root (?) So our first approximate could just be the average of 0 and 3: (0 + 3) / 2 = 1.5 But 1.5 * 1.5 = 2.25, which is less than 3 So the square root must lie between 1.5 and 3 ( ) / 2 = 2.25; 2.25 * 2.25 = , too big! So the square root must lie between 1.5 and 2.25 Et cetera

Putting it all together: Writing our own sqrt() function Algorithm for computing the square root of a number N: 1.Start with a lower bound of zero and an upper bound equal to N 2.While the square of their mean is too far from N, do the following: 3. If squared mean is too big, use mean as the new upper bound 4. Otherwise, use mean as new lower bound What haven't we considered?

PRECISION = def mysqrt(n): if n < 1: lower = n upper = 1 else: lower = 1 upper = n while True: mean = (lower + upper) / 2.0 meansqr = mean * mean if abs(meansqr-n) < PRECISION: return mean if meansqr > n: upper = mean else: lower = mean

Python Lists Computer Science consists mainly in 1.Putting data into lists 2.Sorting the lists according to some criterion (e.g. alphabetical order) 3.Searching the sorted list to find an item of interest For sorting and searching to be efficient, the lists must support random access : like RAM (vs. sequential access of disk, one byte at a time) Python lists provide all these features for us Python's range() function generates numerical lists of arbitrary size

Self-Test: Predict the result of each print() family = ['Simon', 'Linda', 'Sam'] print(family[0]) # random access print(family[1]) print('Simon' in family) # search print('George' in family) cousins = ['Stephanie', 'Susan', 'Michael'] print(family + cousins) family.sort() print(family) print(len(family)) family.append('Sharon') print(family) family.reverse() print(family) for person in family: print(person)

Self-Test: Predict the result of each print() for k in range(5): print(k) for k in range(3,7): print(k) for k in range(1,10,2): print(k)