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.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
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.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Selection (decision) control structure Learning objective
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
Fundamentals of Python: From First Programs Through Data Structures
Python quick start guide
The University of Texas – Pan American
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014
Python Control Flow statements There are three control flow statements in Python - if, for and while.
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.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
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.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
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.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Python Basics.
Control Flow (Python) Dr. José M. Reyes Álamo.
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Programming Fundamentals
Chapter 6: Conditional Statements and Loops
Selection By Ramin && Taimoor
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
IF if (condition) { Process… }
Lesson 04: Conditionals Class Chat: Attendance: Participation
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Practice with loops! What is the output of each function below?
Coding Concepts (Basics)
Repetition Control Structure
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Computer Science Core Concepts
SELECTIONS STATEMENTS
Python ~ Control Structure
Boolean Expressions to Make Comparisons
An Introduction to Linux
CHAPTER 5: Control Flow Tools (if statement)
Control Statements.
REPETITION Why Repetition?
Presentation transcript:

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 1

Lesson objectives 1. Use conditional statements in Python, including the “if”, “if…else”, and “if…elif” statements 2. Use Boolean logic to create complex conditionals 3. Use the “for” statement to create loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7 2

Conditional execution (if…) Syntax: if condition: do_something Condition must be statement that evaluates to a boolean value (True or False) 4/18/09 Python Mini-Course: Day 2 - Lesson 7 3

Example: Checking user input x = raw_input("X? ") if x.isdigit(): print "You input a number" 4/18/09 Python Mini-Course: Day 2 - Lesson 7 4

Alternative execution Syntax: if condition: do_something else: do_alternative 4/18/09 Python Mini-Course: Day 2 - Lesson 7 5

Example: Checking user input x = raw_input("x? ") if x.isdigit(): print "You input a number" else: print "Please input a number\ next time" 4/18/09 Python Mini-Course: Day 2 - Lesson 7 6

Example: Avoiding division by zero x = int(raw_input("x? ")) y = int(raw_input("y? ")) if y <> 0: print x / y else: print "Attempted division by\ zero" 4/18/09 Python Mini-Course: Day 2 - Lesson 7 7

Chained conditionals Syntax: if condition: do_something elif condition: do_alternative1 else: do_alternative2 4/18/09 Python Mini-Course: Day 2 - Lesson 7 8

Example: x = int(raw_input("x? ")) y = int(raw_input("y? ")) if x < y: print 'x is less than y' elif x > y: print 'x is greater than y' else: print 'x and y are equal' 4/18/09 Python Mini-Course: Day 2 - Lesson 7 9

Notes on conditionals You have to have at least one statement inside each branch but you can use the pass command while stubbing if x < 0: pass #Handle neg values 4/18/09 Python Mini-Course: Day 2 - Lesson 7 10

Notes on conditionals You can have as many elif branches as you need This replaces the select and switch commands in other languages Conditionals can be nested Example: nested.py 4/18/09 Python Mini-Course: Day 2 - Lesson 7 11

Complex condition statements Use Boolean logic operators and, or, and not to chain together simple conditions Example: boolean.py 4/18/09 Python Mini-Course: Day 2 - Lesson 7 12

The for loop Used to repeat a section of code a set number of times Syntax: for target in sequence: do_statements 4/18/09 Python Mini-Course: Day 2 - Lesson 7 13

Example: (power.py) def power(base, exponent): val = base for x in range(1,exponent): val = val * base print val power(3,4) 4/18/09 Python Mini-Course: Day 2 - Lesson 7 14

Example: (power.py) def power(base, exponent): val = base for x in range(1,exponent): print x, val val = val * base print val 4/18/09 Python Mini-Course: Day 2 - Lesson 7 15

Example: (power.py) def power(base, exponent): val = base for x in range(1,exponent): print x, val val = val * base print val 4/18/09 Python Mini-Course: Day 2 - Lesson 7 16

Exercises 1. Find a major bug in the power function and correct it 2. Add a docstring to the power function 3. Add type checking and value checking to the power function 4/18/09 Python Mini-Course: Day 2 - Lesson 7 17