From the course: Python Theory for Network Engineers
Conditional logic: If/then and compare values - Python Tutorial
From the course: Python Theory for Network Engineers
Conditional logic: If/then and compare values
- [Instructor] Let's talk about conditional logic in Python and how we could use it to control the flow of our operations. Why is conditional logic and operations important? Because it allows us to control the flow of our operations. We could specify a certain condition in order for us to execute a block of code. In order for us to do specific tasks when certain conditions met. And we would typically use comparison logic to do this. For example, the larger than, the equal to or the less than comparison. Or we could use the Boolean true false comparison that we've seen before. Know the equality comparison is the double equal sign because we use the single equal sign as assignments, right? Like we've seen that before. We specify a variable and assign a value to it using the single equal sign. Therefore, when we do conditional operations, when we try to compare something if the equality is what we're after then that's a double equals sign. So here's a simple example is the if, else if, else statement block. Essentially this example is specify, "Hey I have a variable that equals to number 11, which is age. And now we have this block of code that specifies different conditions for comparison and if that condition is met, then we'll do something. So in this case, if the age is larger than 10, we'll print something. Else if the age is less than 10, we'll print something else. And then we have a catchall else statement that prints the third condition. And when we have this block of code, because the age is 11 that means the first condition is met. Age is larger than 10, therefore we print that person's older than 10. This is a good place to bring out this point about Python's indentation. So Python used white space indentation to specify the group of code or the group of statements that should be grouped together and execute from top to down. So for example, if we wanted to print more things or if we wanted to execute more statements when age is larger than 10 then everything should be grouped together at the print. Same indentation of white spaces before then. So that sounded more complicated than it really is. I often feel, but just keep in mind that indentation matters and if you want to execute something together they should be on the same indentation. And also want to specify the else if is optional and they could be unlimited number of else if, right? So I have more than one condition I want to compare to., then let's just say I have a another else if say age is less than 10 but greater than five or something like that, then I have these different conditions that could compare to. So let's go ahead and look at an example of this conditional logic.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Conditional logic: If/then and compare values2m 49s
-
(Locked)
Lab: Demonstrating if/then and value comparison3m 4s
-
(Locked)
Conditional logic: For and while loops2m 47s
-
(Locked)
Lab: Demonstrate for and while loops5m 4s
-
(Locked)
Challenge: Making your script do repeatable tasks1m 4s
-
(Locked)
Solution: Making your script do repeatable tasks2m 19s
-
-
-
-