Types, Truth, and Expressions (Part 2)

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Logical Operators and While Loops CS303E: Elements of Computers and Programming.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Python Programming Language
Conditional Statements Introduction to Computing Science and Programming I.
Expressions and statements Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
Geography 465 Assignments, Conditionals, and Loops.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Chapter 2 Control.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
IB Computer Science – Logic
If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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.
Computer Science 101 If Statement. Python Relational Operators.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
 Type Called bool  Bool has only two possible values: True and False.
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
Selection (also known as Branching) Jumail Bin Taliba by
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
Lecture 3- Decision Structures
Topics The if Statement The if-else Statement Comparing Strings
Logical Operators and While Loops
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
String Encodings and Penny Math
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More on Functions (Part 2)
CS 1111 Introduction to Programming Spring 2019
Intro to Nested Looping
Computer Science Core Concepts
Nate Brunelle Today: Conditional Decision Statements
Types, Truth, and Expressions (Part 2)
Class 13 function example unstring if if else if elif else
Logical Operators and While Loops
Understanding Conditions
Nate Brunelle Today: Conditional Decision Statements
Truth tables Mrs. Palmer.
CHAPTER 5: Control Flow Tools (if statement)
String Encodings and Penny Math
Types, Truth, and Expressions
Nate Brunelle Today: Conditional Decision Statements
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

Types, Truth, and Expressions (Part 2) Intro to Computer Science CS1510 Dr. Sarah Diesburg

Review : Python if Statement if <expression> : suite Evaluate the expression to a boolean value (True or False) if True, execute all statements in the suite

Review : Warning About Indentation Elements of the “suite” must all be indented the same number of spaces/tabs Python only recognizes suites when they are indented the same “distance” You must be careful to get the indentation right to get suites right.

Python Selection, Round 3 if boolean expression1: suite1 elif boolean expression2: suite2 #(as many elif’s as you want) else: suiteLast

if, elif, else, the Process Evaluate boolean expressions until: The boolean expression returns True None of the boolean expressions return True If a boolean returns True, run the corresponding suite. Skip the rest of the if If no boolean returns True, run the else suite, the default suite

Compound Expressions Logical Operators (lower case) and or not

Let’s test your knowledge Problems #10-11 : Which letters are printed? A B A and B Neither

Truth Tables

Truth Tables

Truth Tables

Truth Tables

Truth Tables

Chained Comparisons You are going to be tempted to write: 0 <= myInt <= 5 But you will need to be very careful

Compound Evaluation Logically 0 < X < 3 is actually (0 < X) and (X < 3) Evaluate using X with a value of 2: (0< X) and (X< 3) Parenthesis first: (True) and (True) Final value: True (Note: parentheses are not necessary in this case.)

Compound Evaluation BUT, I’ve seen students write: 3 < X < 0 Meaning they want to know if x is outside of that range. But this is actually (3 < X) and (X < 0) Evaluate using X with a value of 5: (3< X) and (X< 0) Parenthesis first: (True) and (False) Final value: False