2/7/2008. >>> Overview * boolean * while * random * tuples.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
ITEC 320 Lecture 4 Sub-Types Functions. Functions / Procedures Review Types Homework Questions?
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.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Week 4. >>> Overview * if else * returns * input.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Week 7 Review and file processing. >>> Methods Hello.java public class Hello { public static void main(String[] args){ hello(); } public static void hello(){
Algorithms Writing instructions in the order they should execute.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
10/30/07. >>> Overview * boolean * randomness * while * tuples.
11/13/07. >>> Overview * lists/arrays * reading files * writing files.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
10/30/07.
10/23/07. >>> Overview * if else * returns * input.
10/16/07.
Chapter 02 Data and Expressions.
4. Java language basics: Function
Catapult Python Programming Wednesday Morning session
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Introduction to Python
Week 8 Classes and Objects
Catapult Python Programming Thursday Session
CS-104 Final Exam Review Victor Norman.
Section 6: Sequences Chapter 4 and 5.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Building Java Programs
Building Java Programs
Arithmetic operations, decisions and looping
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
1/22/2008.
Coding Concepts (Sub- Programs)
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Week 8 Classes and Objects
Topic 1: Problem Solving
while loops; logic; random numbers; tuples
Practice with loops! What is the output of each function below?
Building Java Programs
Building Java Programs
Using Objects (continued)
For loops Taken from notes by Dr. Neil Moore
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Indefinite loop variations
Data Types and Maths Programming Guides.
Lecture 1 Review of 1301/1321 CSE /26/2018.
Computer Science Club 1st November 2019.
Presentation transcript:

2/7/2008

>>> Overview * boolean * while * random * tuples

>>> boolean Just like Java, there are boolean values. These values are True and False. True False < > <= >= == != or and not >>> True True >>> False False >>> 2==3 False >>> "this"=="this" True >>> 2==3 and 4==4 False >>> x = not 1 == 2 >>> x True

>>> while The while loop translates nicely from Java to Python. Scanner console = new Scanner(System.in); int sum = 0; System.out.print("Enter a number (-1 to quit): "); int number = console.nextInt(); while (number != -1) { sum = sum + number; System.out.print("Enter a number (-1 to quit): "); number = console.nextInt(); } System.out.println("The total is " + sum); Sentinel.java sum = 0 number = input(“Enter a number (-1 to quit)? ")‏ while number != -1: sum += number number = input(" Enter a number (-1 to quit)? ")‏ print "The total is " + str(sum)‏ sentinel.py

>>> random Just like in Java, python also has random object. Here is an example: >>> from random import * >>> randint(0,9)‏ 1 >>> randint(0,9)‏ 4 >>> choice(range(10))‏ 7 random.randint(a,b)‏ returns an int between a and b inclusive random.choice(seq)‏ returns a random element of the sequence ‏

>>> tuples as points Python does not have Point Objects. Instead we use tuples. A tuple is able to hold multiple values. These values can correspond to the x and y coordinates of a point. The syntax for a tuple is: = (value1, value 2,..., valueN) For a point, we only need two values. Creates a tuple where the first value is 3 and the second value is 5. This can represent a 2D point where the “x” value is 3 and the “y” value is 5. >>> p = (3, 5) >>> p‏ (3, 5)

>>> retrieving tuple values If we wish to use the values in a tuple, we can assign each value to a vairable. This creates two new variables x and y, and assigns the first value in our tuple to x, and the second value to y. >>> p = (3, 5) >>> p (3, 5) >>> (x, y) = p >>> x 3 >>> y 5

>>> parameters and returns Tuples can be passed just like any other variable. Once inside a method, we will want to access its values. Example: def equal(p1, p2): (x1, y1) = p1 (x2, y2) = p2 return x1==x2 and y1==y2 Additionally, we can return tuples. Assume we wanted to add two two. This does not make much sense for points, but does for 2D vectors. def addVectors(p1, p2): (x1, y1) = p1 (x2, y2) = p2 return (x1 + x2, y1 + y2) NOTE: Tuples are “immutable.” This means that the values within a tuple cannot be altered once it has been created. Because of this, if we would like to change the value of our tuples, we must create a new tuple with the values we want, and use it instead.

>>> point distance method # Calculates the distance between two points def distance(p1, p2): (x1, y1) = p1 (x2, y2) = p2 dx = abs(x1 - x2) dy = abs(y1 - y2) return sqrt(dx * dx + dy * dy)

>>> mini-yahtzee # plays until 3 dice have the same value from random import * def miniYahtzee(): d1 = 0 d2 = 1 d3 = 2 count = 0 while not(d1 == d2 == d3): d1 = randint(1, 6) d2 = randint(1, 6) d3 = randint(1, 6) print str(d1), str(d2), str(d3) count += 1 print “Mini-Yahtzee in” + str(count) + “moves”

>>> graphic example - rectangles from random import * from drawingpanel import * def drawRandomRect(): x = randint(0,490) y = randint(0,490) randomColor = choice(("red", "orange", "yellow", "green", "blue", "purple")) size = randint(1,100) g.create_rectangle(x, y, x+size, y+size, fill=randomColor) return randomColor == "red" #main panel = DrawingPanel(500, 500) g = panel.get_graphics() reds = 0 while reds < 20: if drawRandomRect(): reds += 1

>>> Homework #5 Random walk is becoming random slither! No DEBUG mode Remember to use raw_input() for gathering a whole string of user input Random-Slither will be green and will change shades of green. Colors can be represented as RGB tuples Since Tkinter takes Strings as color arguments, our tuple needs to be converted to a String of hex values (like web colors) Example red = 0 green = 255 blue = 0 hexColor = "#%02x%02x%02x" % (red, green, blue) create_oval(0, 0, 100, 100, fill=hexColor, outline=hexColor) To create a single pixel, make a rectangle where x1 equals x2 and y1 equals y2: create_rectangle(50, 50, 50, 50)

Except where otherwise noted, this work is licensed under © 2007 Scott Shawcroft, Some Rights Reserved Python® and the Python logo are either a registered trademark or trademark of the Python Software Foundation. Java™ is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.