Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

What are if,else and elif statements in Python?

Author: Mansoor Ahmed
by Mansoor Ahmed
Posted: Nov 16, 2020

if statements

  • We can use the concept of branching to have our code alter its execution sequence depending on the values.
  • We can use an if statement to evaluate a comparison.
  • We start with the if key word, followed by our comparison.
  • We end the line with colon:
  • The body of the if statement is then indented to the right.
  • If the comparison is True, the code inside the if body is executed.
  • If the comparison evaluates to False, then the code block is skipped and will not be run.

Assume we want to know whether the string assigned to the variable object is "computer". This is the code.

if object == "computer"

print("Yes, it's computer")

If the string "computer" has been assigned to the variable object, Python displays the message Yes,it's computer. If the string "computer" hasn't been assigned to the variable object, nothing happens.

  • It begins with the keyword if.
  • Note that if is all lowercase.
  • If we write If instead of if, it won't work.We 'll get an error message.
  • Note that it's two equal signs, ==, not one. One equal sign, =, can only be used to assign a value to a variable, as in.. object = "computer"
  • Whenever we're testing whether one thing is the same as another, the operator has to be ==. Otherwise, we'll get an error message.
  • The first line ends with a colon. If the string "computer" has been assigned to the variable object we tell Python what to do..
  • We put this on its own line, and we indent the line one tab.
  • We can make any number of things happen when the answer to the if question is "yes".
  • Each thing that happens gets its own line. And each line is indented.
  • There are other things we can test, including numbers. It works the same way.
  • In Python, indents aren't just for pretty formatting. They have meaning for Python. They aren't optional.
  • In general, any lines of code that take their orders from a line that ends during a colon are indented.
Comparison Operators
  • ==, it's a sort of comparison operator, specifically it's the equality operator. We use it to compare two things to see if they're equal.
  • We can use the equality operator to compare a variable with a string, a variable with a number, a variable with a math expression, or a variable with a variable.
  • We can use the equality operator to compare various combinations.
  • When we are comparing strings, the equality operator is case-sensitive, "Flower" does not equal to "flower".
  • Another comparison operator,!=, is the opposite of ==. It means is not equal to.It can be used to compare numbers, strings, variables, math expressions, and combinations.
  • Four more comparison operators;
  1. > is greater than
  2. = is greater than or equal to
  3. 200 and time 19 and height avg or GPA> 2.5 or parent == "alum":
  4. message = " Welcome to Government College! "
  5. Only one of the conditions need to be met in order for the welcome message to be sent out-- a high SAT score, a decent grade point average, or a parent who attended the college.
  6. Any of them will do. Of course, line 2 executes if more than one condition is met.
  7. We can combine any number of and and or conditions. When we do, we create ambiguities.
  8. More Complex Branching with elif Statements
    • Building off of the if and else blocks, which permit us to branch our code counting on the evaluation of one statement, the elif statement allow us even more comparisons to perform more complex branching.
    • Very similar to the if statements, an elif statement starts with the elif keyword, followed by a comparison to be evaluated.
    • This is followed by a colon, and then the code block on the next line, indented to the right.
    • An elif statement must follow an if statement, and will only be evaluated if the statement was evaluated as false.
    • We can include multiple elif statements to build complex branching in our code to do all kinds of powerful things.
    A Note on Syntax and Code Blocks
    • When writing a code, using correct syntax is super important.
    • Even a small typo, like a missing parentheses or comma, can cause a syntax error and the code won't
    • After the colon, the function body starts.
    • It's important to note that in Python the function body is delimited by indentation.
    • This means that all code indented to the right following a function definition is part of the function body.
    • The first line that's no longer indented is the boundary of the function body.
    • It's up to us how many spaces we use when indenting---just make sure to be consistent.
    • So if we choose to indent with four spaces, we need to use four spaces everywhere in our code.

    About the Author

    Mansoor Ahmed Chemical Engineer,Web developer

    Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Mansoor Ahmed

Mansoor Ahmed

Member since: Oct 10, 2020
Published articles: 124

Related Articles