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.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
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.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
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.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
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.
If Statements and Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits:
Decision Structures, String Comparison, Nested Structures
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.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
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.
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.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
 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
Chapter 3 Selection Statements
Selection (also known as Branching) Jumail Bin Taliba by
Making Choices with if Statements
Intro to Computer Science CS1510 Dr. Sarah Diesburg
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
And now for something completely different . . .
Conditions and Ifs BIS1523 – Lecture 8.
String Encodings and Penny Math
Types, Truth, and Expressions (Part 2)
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Programming Using Python PART 2
More on Functions (Part 2)
Intro to Nested Looping
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
Boolean logic Taken from notes by Dr. Neil Moore
Understanding Conditions
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

Line Wrap Some rules Strings must be closed in the line in which they start Adding a backslash at the end of the line tells python that the command is continued on the end of the line

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.

Let’s test your knowledge Let‘s look at #1-4 in my questions.py program…

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

Let’s test your knowledge Let’s look at statements with if, elif, else Problems #5-9 : Which letters are printed? A B A and B Neither

Python Selection, Round 2 if boolean expression: suite1 else: suite2 The process is: evaluate the boolean if True, run suite1 if False, run suite2

Python Selection, Round 3 if boolean expression1: suite1 elif expression2: suite2 The process is: evaluate expression1 if True, run suite1 If False, evaluate expression 2 if True, run suite2

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