Python Conditionals: The if statement

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Python Programming Language
Introduction to Computing Science and Programming I
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Geography 465 Assignments, Conditionals, and Loops.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
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.
While Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
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.
Course A201: Introduction to Programming 09/16/2010.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Python Conditionals chapter 5
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
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: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
 Type Called bool  Bool has only two possible values: True and False.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
JavaScript Controlling the flow of your programs with ‘if’ statements
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Line Continuation, Output Formatting, and Decision Structures
Python: Control Structures
IF statements.
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
CMSC201 Computer Science I for Majors Lecture 04 – Decision Structures
Procedures Programming Guides.
Topics The if Statement The if-else Statement Comparing Strings
Lesson 8: Boolean Expressions and "if" Statements
Line Continuation, Output Formatting, and Decision Structures
Selection (IF Statements)
Logical Operations In Matlab.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Visual Basic – Decision Statements
Selection Statements.
Introduction to Decision Structures and Boolean Variables
Conditional and iterative statements
Python Programming Language
CISC101 Reminders All assignments are now posted.
Relational Operators.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CHAPTER 5: Control Flow Tools (if statement)
Text Copyright (c) 2017 by Dr. E. Horvath
Copyright (c) 2017 by Dr. E. Horvath
Lecture 5 – Unit 1 – Chatbots Python – More on Conditional statements
Data Types and Expressions
Flow Control I Branching and Looping.
Introduction to Python
Presentation transcript:

Python Conditionals: The if statement A conditional is a statement that tests if a condition is true. Let's consider the simplest conditional, the if statement. If the condition is true, then code is executed. The syntax in Python is made clear in the following snippet of code. a = 4 b = 3 if a > b: print(a, ' is greater than ', b) print('Outside of if block') Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Explanation of Code Snippet The condition is a > b, and since this condition is true, the block of indented code that follows the if statement is then executed. In Python, the conditional statement must end with a colon. Python 3 automatically indents code that follows a colon. The convention is to indent four spaces; do not use tabs. Once a statement is reached that is not indented, the Python interpreter treats that as the sign that the end of the if-block has been reached. Hence, since the second print statement is not indented; it will be executed regardless of whether or not the condition is true. Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Relational Operators The greater than sign in the first example is a relational operator. There are six relational operators that can be used to test whether a condition is true or not: < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to

Python Conditionals: Snippet of code with six relational operators b = 3 if a > b: print(a, ' is greater than ', b) if a < b: Print(a, ' is less than ', b) if a <= b: print(a, ' is less than or equal to ', b) if a >= b: print(a, ' is greater than or equal to ',b) if a==b: print(a, ' is equal to ', b) if a != b: print(a, ' is not equal to ', b) Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Another if Example Let's suppose we wish to determine which variable is larger. We could code this as follows x = 4.59 y = 7.86 if x > y: print('x is larger than y') if x < y: print('y is larger than x') Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Introducing the if-else block There is a better way of coding. x = 4.59 y = 7.86 if x > y: print('x is larger than y') else: print('y is larger than x') The first print statement is executed if x is larger than y, otherwise the second statement is executed. This means the second print statement is executed if y is larger than x or if x equals y. Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Introducing the if-elif-else block x = 4.59 y = 7.86 if x > y: print('x is larger than y') elif y > x: print('y is larger than x') else: print('x and y are the same') With the if-elif-else block, we can handle every possibility. Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: A Comparison of Conditionals Let's consider how this code is executed. if temp1 > temp2: print(temp1, ' is greater than ', temp2) if temp1 >= temp2: print(temp1, ' is greater than or equal to ', temp2) If temp1 is greater than temp2, then both of these print statements will be executed. If temp1 is equal to temp2, then only the second print statement will be executed. elif temp1 >= temp2: If temp1 is greater than temp2, then only the first print statement will be executed. Only if temp1 equals temp2 will the second print statement be executed. Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: A Brief Digression: The pass statement Let's say you're writing a complicated program and you've only decided on the structure of the program, but you haven't yet decided how to handle the details. The pass command serves as placeholder for code you intend to incorporate into the program. if x != user_input: pass else: Here the program is testing if x is equal to user_input, but the programmer hasn't yet decided how to handle each situation so she uses the pass command. If the programmer doesn't add any indented code, then the interpreter will throw an error. Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Another Example of an if-elif-else block user_input = input('In what units do you wish to express the temperature? (F)ahrenheit or (C)elsius?') tempC = 26 if user_input == 'C': print('The temperature is ',tempC) elif user_input == 'F': tempF = 9/5*tempC+32 print('The temperature is ',tempF) else: pass #We haven't yet decided what to do if the user doesn't input #C or F Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Chained Relational Operators gravity_earth = 9.80 #Units are meters/sec**2 gravity_mars = 3.71 gravity_jupiter = 274 if gravity_mars < gravity_earth < gravity_jupiter: print('The acceleration due to gravity is smallest on Mars') Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Boolean Operators: and gravity_earth = 9.80 #Units are meters/sec**2 gravity_mars = 3.71 gravity_jupiter = 274 acc_units = “m/sec**2” if gravity_earth < gravity_jupiter and gravity_mars < gravity_earth: print('(1) Jupiter '+ str(gravity_jupiter)+acc_units) print('2) Earth '+ str(gravity_earth)+acc_units) print('3) Mars ' + str(gravity_mars)+acc_units) Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Boolean Operators: or cube_side1 = 4.2 volume1 = cube_side1**3 cube_side2 = 6.9 volume2 = cube_side2**3 if volume1 > 300 or volume2 > 300: print('One or both of these cubes have a volume larger than 300') Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Boolean Operators: not the_answer = False if not the_answer: print('The answer is false') Text Copyright (c) 2017 by Dr. E. Horvath

Python Conditionals: Nested Conditionals x1 = 3354 x2 = 98670 color = 'chartreuse' if x2 > x1: if color == 'chartreuse': print('Inside the nest and the color is chartreuse') else: print('Inside the nest, but the color is not chartreuse') Text Copyright (c) 2017 by Dr. E. Horvath