Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On..
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Python Control of Flow.
4. Python - Basic Operators
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
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.
2440: 211 Interactive Web Programming Expressions & Operators.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Secret Signal … on your chest “1” if true “2” if false 2 3 = (2)(2)(2)
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15.
Basic Control Structures
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
1-2 Order of Operations and Evaluating Expressions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
ARITHMETIC OPERATORS COMPARISON/RELATIO NAL OPERATORS LOGIC OPERATORS ()Parenthesis>Greater than &&And ^Exponentiation=>=
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.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
LESSON 4-7 EXPONENTS & MULTIPLYING. When we multiply terms with exponents  ADD exponents of like variables.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Python Basics.
Python Loops and Iteration
University of Central Florida COP 3330 Object Oriented Programming
Lesson 05: Iterations Class Chat: Attendance: Participation
University of Central Florida COP 3330 Object Oriented Programming
Think What will be the output?
Python - Loops and Iteration
Arithmetic operations, decisions and looping
Chapter (3) - Looping Questions.
Java Tokens & Data types
Solving Two-Step Equations Lesson 2-2 Learning goal.
Introduction to Programming Using Python PART 2
Introduction to Programming – 4 Operators
Chap 7. Advanced Control Statements in Java
A3 4.2a To Operate on Polynomials
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1

Lesson objectives 1. Use arithmetic and binary operators 2. Use the increment and decrement operators 3. Create loops using the for and while statements 4. Control loop execution using the break and continue statements 05/02/09 Python Mini-Course: Day 3 - Lesson 10 2

Arithmetic operators add subtract multiply divide x+y x–y x*y x/y exponent floor divide modulus x**y x//y x%y negation absolute value -x abs(x) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 3

Binary (bitwise) operators AND OR XOR a&b a|b a^b Left shift Right shift Complement a >x ~a 05/02/09 Python Mini-Course: Day 3 - Lesson 10 4

Incrementing Updating the value of a variable by adding some value to the current value Example: x = 4 x = x /02/09 Python Mini-Course: Day 3 - Lesson 10 5

Decrementing Updating the value of a variable by subtracting some value from the current value Example: x = 4 x = x /02/09 Python Mini-Course: Day 3 - Lesson 10 6

Updating operators Python uses the augmented assignment operators: += Increment -= Decrement *= Update by multiplication /= Update by division 05/02/09 Python Mini-Course: Day 3 - Lesson 10 7

Examples x = 5 print x x += 4 print x x -= 3 print x x *= 15 print x x /= 5 print x 05/02/09 Python Mini-Course: Day 3 - Lesson 10 8

Implicit type conversion Just like the arithmetic operators, the update operators perform automatic type conversion x = 3 print x, type(x) x += 1.5 print x, type(x) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 9

The while statement Syntax while conditional: do_something A general loop that executes code as long as the conditional statement is true Exits loop when conditional is false 05/02/09 Python Mini-Course: Day 3 - Lesson 10 10

Example 1: blastoff2.py def countdown(n): while n > 0: print n n = n-1 print 'Blastoff!' countdown(10) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 11

Example 2: sequence.py def sequence(n): while n != 1: print n, if n%2 == 0: # n is even n = n/2 else: # n is odd n = n*3+1 sequence(15) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 12

Important notes The while statement uses negative logic to express the stop condition: "keep going until that happens" Also, the loop can only terminate at the beginning of each iteration Often we want positive logic: "stop when this happens" 05/02/09 Python Mini-Course: Day 3 - Lesson 10 13

The break statement Used to break out of a loop ( for or while loop) early Loop stops immediately and execution picks up at the next line outside the loop 05/02/09 Python Mini-Course: Day 3 - Lesson 10 14

Example: break.py while True: line = raw_input('> ') if line == 'done': break print line print 'Done!' 05/02/09 Python Mini-Course: Day 3 - Lesson 10 15

The continue statement Used to restart a loop early Execution immediately goes back to the loop header 05/02/09 Python Mini-Course: Day 3 - Lesson 10 16

Example: print_odd.py def print_odd(start=1, stop=1): for x in range(start, stop): if x % 2 == 0: continue print x print_odd(1,7) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 17

Example: print_odd2.py def print_odd(start=1, stop=1): if start % 2 == 0: start += 1 for x in range(start, stop, 2): print x print_odd(4,10) 05/02/09 Python Mini-Course: Day 3 - Lesson 10 18