Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Geography 465 Assignments, Conditionals, and Loops.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Python Control of Flow.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
More Looping Structures
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Four Fundamental Pieces Instruction Control Structure Function Expression.
If..else Use random numbers to compute an approximation of pi Simulation of a special game of darts Randomly place darts on the board pi can be computed.
Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math.
Course A201: Introduction to Programming 09/16/2010.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CIS 3301 C# Lesson 3 Control Statements - Selection.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Programming: Simple Control Structures
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Control Flow (Python) Dr. José M. Reyes Álamo.
For vs. While loop count=0 while count< 5: for count in range(5):
Chapter 4 Repetition Statements (loops)
Computer Programming Fundamentals
Making Choices with if Statements
Programming: Simple Control Structures
Chapter 3: Decisions and Loops
Python: Control Structures
turtle module Simple graphics programming How to use it ?
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Summary Conditional: if .. else New Nested conditional elif
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.
Expressions and Control Flow in JavaScript
Logical Operators and While Loops
Programming: Simple Control Structures
Programming: Simple Control Structures
Practice with loops! What is the output of each function below?
More Looping Structures
Chapter 8: More on the Repetition Structure
Programming: Simple Control Structures
Programming: Simple Control Structures
Summary Two basic concepts: variables and assignments Basic types:
Conditional and iterative statements
Computer Science Core Concepts
Programming: Simple Control Structures
Logical Operators and While Loops
More Looping Structures
Programming: Simple Control Structures
Presentation transcript:

Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer loop: “,a, b)

Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math problem, or ‘q’ to quit: “) while (problem != “q”): print(“Answer to “, problem, “ is: “, eval(problem)) problem = input(“Enter a math problem, or ‘q’ to quit: “)

Chap. 5 Conditional Expressions Can test equality with == Can also test, >=, <=, != (not equals) In general, 0 is false, 1 is true  So you can have a function return a “true” or “false” value. Alert! = means “make them equal!” == means “are they equal?”

Multiple conditionals (Boolean) A and B A or B True only when both True when either A and B are true A or B is true B\A False True B\A False True False False False False False True True False True True True True not A

Boolean Operators and, or, not 6<10 and 3<7 4!=4 or 5<8 not 6<10

If..else Conditional def posSum(list): psum = 0 for v in range(0, len(list)): if list[v] > 0: psum = psum+list[v] return psum Add all values in a list Add only positive values def sum(list): psum = 0 for v in range(0, len(list)): psum = psum + list[v] return psum

How an if works if is the command name Next comes an expression: Some kind of true or false comparison Then a colon Then the body of the if—the things that will happen ONLY WHEN the expression is true if list[v] > 0 : psum = psum+list[v]

if (expression): next indented block else: Statements false Pictorial view of how if works true

Iteration Example Investment Current investment Investment+return at a year’s end Investment+return becomes the current investment at a new year’s beginning

Iteration – drunken turle Random walk or Brownian motion A turtle From the current position Take an arbitrary direction and distance as the next position Goto the next position Next position becomes the current position next time around

Random package import random random.random() returns a random fraction between 0 and 1 How to get a random number between (0,100)? A random number between (-50, 50) ?

Drunken Turtle Present location of turtle: curX, curY Next random location ? nextX = curX + 100* random.random() - 50 nextY = curY + 100* random.random() - 50

Random Walk import random # # function randNext(curX, curY) # Given coordinate (curX, curY), generate next random coordinate # def randNext(curX, curY): nextX = curX *random.random() nextY = curY *random.random() return nextX, nextY

Lab_0224 Write a Python program to simulate a drunken turtle Modify the drunken turtle program so that the turtle stays in bound wn = turtle.getscreen() wWid = wn.window_width() wHgt = wn.window_height() Make it sure that you halve the window sizes for +- coordinates your Python program