Starting Out with Python by Tony Gaddis. Statement. The if Statement (cont d.) Boolean Expressions and Relational Operators 8/26/15

Similar documents
Boolean Expressions & the if Statement

Selection Statements

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Python Programming: An Introduction To Computer Science

River Dell Regional School District. Computer Programming with Python Curriculum

Python Programming: An Introduction to Computer Science

JavaScript: Control Statements I

Chapter 5. Selection 5-1

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Introduction to Python

CSC 221: Computer Programming I. Fall 2011

Python Evaluation Rules

Conditional Statements Summer 2010 Margaret Reid-Miller

Writing Control Structures

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Object Oriented Software Design

Programming Logic and Design Eighth Edi(on

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Object Oriented Software Design

Outline. logical expressions pseudocode and flowcharts. conditional operators if, else, elif. computing truth tables with Sage

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Computer Programming I & II*

PL / SQL Basics. Chapter 3

ESCI 386 Scientific Programming, Analysis and Visualization with Python. Lesson 5 Program Control

Some programming experience in a high-level structured programming language is recommended.

Command Scripts Running scripts: include and commands

Conditions & Boolean Expressions

The previous chapter provided a definition of the semantics of a programming

Two-way selection. Branching and Looping

I PUC - Computer Science. Practical s Syllabus. Contents

COMP 110 Prasun Dewan 1

Introduction to Python

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Oracle SQL. Course Summary. Duration. Objectives

Chapter 3 Operators and Control Flow

VB.NET Programming Fundamentals

MATLAB Programming. Problem 1: Sequential

Instant SQL Programming

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Problem Solving Basics and Computer Programming

Python Loops and String Manipulation

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Introduction to Python for Text Analysis

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Software Engineering Techniques

Programming and Software Development CTAG Alignments

Exercise 4 Learning Python language fundamentals

Programming Your App to Make Decisions: Conditional Blocks

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

The Rules 1. One level of indentation per method 2. Don t use the ELSE keyword 3. Wrap all primitives and Strings

1 Introduction. 2 Overview of the Tool. Program Visualization Tool for Educational Code Analysis

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

X86-64 Architecture Guide

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Moving from CS 61A Scheme to CS 61B Java

The design recipe. Programs as communication. Some goals for software design. Readings: HtDP, sections 1-5

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Consulting. Personal Attention, Expert Assistance

Programming A PLC. Standard Instructions

Lecture 2 Notes: Flow of Control

Exercise 1: Python Language Basics

Logic in Computer Science: Logic Gates

This module explains fundamental aspects of Microsoft Dynamics NAV Development Environment.

Charles Dierbach. Wiley

Writing Simple Programs

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

HOMEWORK # 2 SOLUTIO

Lumousoft Visual Programming Language and its IDE

Computer Programming I

Computer Programming Tutorial

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Tokens and Python s Lexical Structure

Python Basics. S.R. Doty. August 27, Preliminaries What is Python? Installation and documentation... 4

Section of DBMS Selection & Evaluation Questionnaire

Curriculum Map. Discipline: Computer Science Course: C++

Wage Calculator Application

Variables, Constants, and Data Types

#820 Computer Programming 1A

Course Title: Software Development

Computer Programming I

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Oracle Database: SQL and PL/SQL Fundamentals

BOOLEAN LOGIC IN PROGRAMMING

Performance Tuning for the Teradata Database

TIP: To access the WinRunner help system at any time, press the F1 key.

Python Lists and Loops

Database Programming with PL/SQL: Learning Objectives

Paper Merges and Joins Timothy J Harrington, Trilogy Consulting Corporation

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Transcription:

Starting Out with Python by Tony Gaddis The if Statement Comparing Strings Nested Decision Structures and the if-elif-else Statement Logical Operators Boolean Variables The if Statement Control structure: logical design that controls order in which set of execute Sequence structure: set of that execute in the order they appear Decision structure: specific action(s) performed only if a condition exists Also known as selection structure The if Statement The if Statement In flowchart, diamond represents / condition that must be tested Actions can be conditionally executed Performed only when a condition is Single alternative decision structure: provides only one alternative path of execution If condition is not, exit the structure The if Statement Python syntax: if condition: Statement Statement First line known as the if clause Includes the keyword if followed by condition The condition can be or When the if statement executes, the condition is tested, and if it is the block are executed. otherwise, block are skipped Boolean s and Relational Operators Boolean expression: expression tested by if statement to determine if it is or Example: a > b if a is greater than b; otherwise Relational operator: determines whether a specific relationship exists between two values Example: greater than (>) 1

Boolean s and >= and <= operators test more than one relationship It is enough for one of the relationships to exist for the expression to be == operator determines whether the two operands are equal to one another Do not confuse with assignment operator (=)!= operator determines whether the two operands are not equal Boolean s and Boolean s and Using a Boolean expression with the > relational operator Boolean s and Any relational operator can be used in a decision block Example: if balance == 0 Example: if payment!= balance It is possible to have a block inside another block Example: if statement inside a function Statements in inner block must be indented with respect to the outer block Dual alternative decision structure: two possible paths of execution One is taken if the condition is, and the other if the condition is Syntax: if condition: else: other if clause and else clause must be aligned Statements must be consistently indented 2

Comparing Strings Strings can be compared using the == and!= operators String comparisons are case sensitive Strings can be compared using >, <, >=, and <= Compared character by character based on the ASCII values for each character If shorter word is substring of longer word, longer word is greater than shorter word Comparing Strings Nested Decision Structures and the if-elif-else Statement A decision structure can be nested inside another decision structure Commonly needed in programs Example: Determine if someone qualifies for a loan, they must meet two conditions: Must earn at least $30,000/year Must have been employed for at least two years Check first condition, and if it is, check second condition Nested Decision Structures and the if-elif-else Statement Important to use proper indentation in a nested decision structure Important for Python interpreter Makes code more readable for programmer Rules for writing nested if : else clause should align with matching if clause Statements in each block must be consistently indented 3

The if-elif-else Statement if-elif-else statement: special version of a decision structure Makes logic of nested decision structures simpler to write Can include multiple elif Syntax: if condition1 elif condition2 else The if-elif-else Statement Alignment used with if-elif-else statement: if, elif, and else clauses are all aligned Conditionally executed blocks are consistently indented if-elif-else statement is never required, but logic easier to follow Can be accomplished by nested if-else Code can become complex, and indentation can cause problematic long lines Logical Operators Logical operators: operators that can be used to create complex Boolean expressions and operator and or operator: binary operators, connect two Boolean expressions into a compound Boolean expression not operator: unary operator, reverses the truth of its Boolean operand The and Operator Takes two Boolean expressions as operands Creates compound Boolean expression that is only when both sub expressions are Can be used to simplify nested decision structures Truth table for the and operator and and and and Value of the The or Operator Takes two Boolean expressions as operands Creates compound Boolean expression that is when either of the sub expressions is Can be used to simplify nested decision structures Truth table for the or operator or or or or Value of the 4

Short-Circuit Evaluation Short circuit evaluation: deciding the value of a compound Boolean expression after evaluating only one sub expression Performed by the or and and operators For or operator: If left operand is, compound expression is. Otherwise, evaluate right operand For and operator: If left operand is, compound expression is. Otherwise, evaluate right operand The not Operator Takes one Boolean expressions as operand and reverses its logical value Sometimes it may be necessary to place parentheses around an expression to clarify to what you are applying the not operator Truth table for the not operator Value of the Checking Numeric Ranges with Logical Operators To determine whether a numeric value is within a specific range of values, use and Example: x >= 10 and x <= 20 To determine whether a numeric value is outside of a specific range of values, use or Example: x < 10 or x > 20 Boolean Variables Boolean variable: references one of two values, True or False Represented by bool data type Commonly used as flags Flag: variable that signals when some condition exists in a program Flag set to False à condition does not exist Flag set to True à condition exists Summary This chapter covered: Decision structures, including: Single alternative decision structures Dual alternative decision structures Nested decision structures Relational operators and logical operators as used in creating Boolean expressions String comparison as used in creating Boolean expressions Boolean variables 5