For loop, definite vs indefinite iteration, range, random

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Sentinel Logic Assumes while loops and input statements.
Fundamentals of Python: From First Programs Through Data Structures
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
Python Programming Fundamentals
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
Python 2 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
Basic Control Structures
Chapter 5: Control Structures II
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Loops and Simple Functions CS303E: Elements of Computers and Programming.
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
CS1010: Programming Methodology
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Binary Numbers Practice.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
CIS199 Test Review 2 REACH.
Program design Program Design Process has 2 phases:
Introduction to Programming
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Python Loops and Iteration
COP 3503 FALL 2012 Shayan Javed Lecture 15
Python: Control Structures
Methods Chapter 6.
ALGORITHMS AND FLOWCHARTS
Think What will be the output?
CMPT 201 Functions.
Java Programming: Program Design Including Data Structures
Iterations Programming Condition Controlled Loops (WHILE Loop)
Random numbers Taken from notes by Dr. Neil Moore
Lecture 2 Introduction to Computer Science (continued)
Algorithm design and Analysis
Arithmetic operations, decisions and looping
Example: Finding the Mode
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Iteration: Beyond the Basic PERFORM
Introduction to Programming
Statement-Level Control Structures
Python programming exercise
Basics of Recursion Programming with Recursion
Introduction to Repetition Structures
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
15-110: Principles of Computing
Java Programming: Chapter 9: Recursion Second Edition
Lecture 7 Algorithm Design & Implementation. All problems can be solved by employing any one of the following building blocks or their combinations 1.
The structure of programming
Loops and Simple Functions
Another Example Problem
The structure of programming
Introduction to Programming
Thinking procedurally
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
REPETITION Why Repetition?
Fundamentals of Python: First Programs Second Edition
Introduction to Python
Presentation transcript:

For loop, definite vs indefinite iteration, range, random

For Loop used in situations where the programmer knows exactly how many times to repeat a Python for loop is an example of a definite iteration (vs. indefinite iteration) the easiest way to construct a for loop is with the range function for i in range(10) : # count from 0 to 9 print i # print from 0 to 9

For n in range(r) When used in a for loop, the range(x) function will execute x times but the counting starts at 0 and ends at x -1 (for example, if x is 3, then 0, 1, 2)   0, 1, 2, 3, … x -1 for i in range(10) : # count from 0 to 9 print i + 1 # print from 1 to 10

Indefinite vs. Definite Iteration Compare an indefinite loop to a definite loop Indefinite loop Definite loop name = raw_input(“Name : ") while (name <> "") : print name print "Done" m = input(“Max : ") for x in range(m) : print x + 1

Range Parameters Using range(); returns a list of integers range() can take additional arguments; range(start, stop, step) Examples of Range Result range(4) [0, 1, 2, 3] range(1, 5) [1, 2, 3, 4] range(0, 15, 3) [0, 3, 6, 9, 12] range(7, 0, -1) [7, 6, 5, 4, 3, 2, 1]

Convert Decimal to Binary the text book version (Unit 3) appears to have some “bugs” I have provided an alternate version of the pseudocode as well, note the algorithm only works with numbers within range(255); 0, 1, … 255

Algorithm to convert Some adjustments in the right panel makes the algorithm different from the text (Unit 3) Convert Decimal -> Binary Algorithm From Textbook Suggested Algorithm read num for positions p from 7, 6, . . . 0, do this: if num ≤ 2p, then set binary to “0” + binary otherwise, set binary to “1” + binary set num to num – 2p write binary read num between 0 & 255 if num < 2p, then set binary to binary + “0” set binary to binary + “1”

Python implementation  # Convert Decimal input to Binary import math binary = "" n = input("number n (0 .. 255) :") if (n >= 0 and n <= 255) : for p in range(7, -1, -1) : if (n < math.pow(2, p)) : binary = binary + '0' else : binary = binary + '1' n = n - math.pow(2, p)   print binary

Random Numbers Python has a “random number” module; used to generate random numbers ref : http://docs.python.org/library/random.html randint(a, b) can be used to generate a random integer between a & b; a <= N <= b

Use randint Import the random module Call the randint() function within limits a & b import random print random.randint(0, 10)

Random sequence The random module will generate a sequence of random numbers; if for example, randint is call more than once The sequence will be the same, unless seed is called import random random.seed() print random.randint(0, 10) print random.randint(0, 10)