From the course: Python Quick Start
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Using If-else statements - Python Tutorial
From the course: Python Quick Start
Using If-else statements
An if-else statement is used to instruct the computer to perform a certain action, if a certain condition is met and a different action if that condition is not met. For example, say you want a function that takes two numbers as inputs. These numbers can be either integers or floating point numbers, and the function returns the minimum of these two numbers. You would do something like this. I'll start with the def keyword because I'm defining a function followed by the name of the function. I'll name this function "minimum" followed by (x, y) and x and y will be generic representations of the inputs that minimum takes in, followed by a colon. I hit "Enter" to get to the next line and after the indent, I'll begin writing my if-else statement. I start with the if keyword followed by the condition that I want to check. In this case, I want to check if x is less than y, so I say x less than y followed by a colon and on the next line after the indent, I write return x. What this part means…
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.