Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
Python Conditionals: Chained Relational Operators
gravity_earth = #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
12
Python Conditionals: Boolean Operators: and
gravity_earth = #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
13
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
14
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
15
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.