while loops; logic; random numbers; tuples

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

2/7/2008. >>> Overview * boolean * while * random * tuples.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Week 4 Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Strings, if/else, return, user input
Java Syntax Primitive data types Operators Control statements.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Geography 465 Assignments, Conditionals, and Loops.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Expressions Methods if else Statements Loops Potpourri.
If/else, return, user input, strings
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Python Basics.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Higher Order Functions
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
CSc 110, Spring 2017 Lecture 12: Random Numbers
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Week 8 Classes and Objects
2. Java language basics (2)
Python: Control Structures
Building Java Programs
5. Function (2) and Exercises
Building Java Programs
Chapter 6 More Conditionals and Loops
CMPT 201 Functions.
basic Python programs, defining functions
CSc 110, Autumn 2016 Lecture 13: Random Numbers
review; file processing
Week 1 Review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is.
Building Java Programs
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr
while loops; file I/O; introduction to lists
Introduction to Python
basic Python programs, defining functions
CSc 110, Spring 2017 Lecture 13: Random numbers and Boolean Logic
Week 8 Classes and Objects
expressions, variables, for loops
expressions, variables, for loops
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
Practice with loops! What is the output of each function below?
Logical Operations In Matlab.
Classes, Objects and lists
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Programming Using Python PART 2
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
Lambda Functions, MapReduce and List Comprehensions
Understanding Conditions
COMPUTER PROGRAMMING SKILLS
Programming Dr. Jalal Kawash.
Building Java Programs
Chap 7. Advanced Control Statements in Java
Functional Programming
Presentation transcript:

while loops; logic; random numbers; tuples Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

while Loops while test: statements >>> n = 91 >>> factor = 2 # find first factor of n >>> while n % factor != 0: ... factor += 1 ... >>> factor 7

while / else while test: statements else: Executes the else part if the loop does not enter There is also a similar for / else statement >>> n = 91 >>> while n % 2 == 1: ... n += 1 ... else: ... print n, "was even; no loop." ... 92 was even; no loop.

bool Python's logic type, equivalent to boolean in Java True and False start with capital letters >>> 5 < 10 True >>> b = 5 < 10 >>> b >>> if b: ... print "The bool value is true" ... The bool value is true >>> b = not b False

greater than or equal to Logical Operators Operator Meaning Example Result == equals 1 + 1 == 2 True != does not equal 3.2 != 2.5 < less than 10 < 5 False > greater than 10 > 5 <= less than or equal to 126 <= 100 >= greater than or equal to 5.0 >= 5.0 Operator Example Result and 2 == 3 and -1 < 5 False or 2 == 3 or -1 < 5 True not not -1 < 5

Random Numbers from random import * randint(min, max) choice(sequence) returns a random integer in range [min, max] inclusive choice(sequence) returns a randomly chosen value from the given sequence the sequence can be a range, a string, ... >>> from random import * >>> randint(1, 5) 2 5 >>> choice(range(4, 20, 2)) 16 >>> choice("hello") 'e'

Exercise Rewrite the Dice program from Java to Python: 2 + 4 = 6 3 + 5 = 8 5 + 6 = 11 1 + 1 = 2 4 + 3 = 7 You won after 5 tries!

Tuple tuple_name = (value, value, ..., value) A way of "packing" multiple values into one variable name, name, ..., name = tuple_name "unpacking" a tuple's contents into multiple variables >>> x = 3 >>> y = -5 >>> p = (x, y, 42) >>> p (3, -5, 42) >>> a, b, c = p >>> a 3 >>> b -5 >>> c 42

Using Tuples Useful for storing multi-dimensional data (e.g. (x, y) points) Useful for returning more than one value >>> p = (42, 79) >>> from random import * >>> def roll2(): ... die1 = randint(1, 6) ... die2 = randint(1, 6) ... return (die1, die2) ... >>> d1, d2 = roll2() >>> d1 6 >>> d2 4

Tuple as Parameter def name( (name, name, ..., name), ... ): statements Declares tuple as a parameter by naming each of its pieces >>> def slope((x1, y1), (x2, y2)): ... return (y2 - y1) / (x2 - x1) ... >>> p1 = (2, 5) >>> p2 = (4, 11) >>> slope(p1, p2) 3

Tuple as Return def name(parameters): statements return (name, name, ..., name) >>> from random import * >>> def roll2(): ... die1 = randint(1, 6) ... die2 = randint(1, 6) ... return (die1, die2) ... >>> d1, d2 = roll2() >>> d1 6 >>> d2 4

Higher Order Functions filter(func, sequence) returns all values in sequence for which func(value) returns True >>> def close(p1): p2 = (0, 0) return dist(p1, p2) < 7 n = ((1, 3), (4, 45), (65, 5)) >>> print (list(filter(close, n))) [(1, 3)]

Exercise A python version of your homework 5. Change your haiku intro message into a haiku about Monty Python.