How do i use conditional logic in python?



  • Note: All code is typed on the python interactive prompt.

    Conditional logic is like real life logic, for example, the question, "Do I go to the cake shop first or go to the library?" You are deciding on whether to do one or the other.

    if, elif, else logic

    elif is short of for "else if"

    The login is correct 10 is greater than 3 so the print statement is executed.

    if  10 > 3:
      print("I am 10 and greater than 3")
    

    Using the example of the cake shop and library. We set a value to a variable called decision, based on what that value is, it prints out the relevant text. If you're used to shell scripting, you have to remember to include the colons in proper places. Otherwise, it's the same.

    In this code the variable, decision is currently set to "cake"

    if decision == "cake":
        print("I'm going to eat lots of cake")
    elif decision == "library":
        print("I'm going to read a good book")
    else:    
        print("I've decided to go back home")
    

    Result:
    I'm going to eat lots of cake

    If we change the variable, decision to "library"

    if decision == "cake":
        print("I'm going to eat lots of cake")
    elif decision == "library":
        print("I'm going to read a good book")
    else:    
        print("I've decided to go back home")
    

    Result:
    I'm going to read a good book

    If the variable, decision, is set to something other than cake or library. It will print the following.

    Result:
    I've decided to go back home

    while loop

    A simple while loop, while loops will loop until a certain condition is met.

    This while loop will loop until the number is 9 or less, because the condition is x < 10. The x = x + 1, keeps adding 1 to the value of x. A short way of writing that is x += 1

    >>> x = 0
    >>> while x < 10:
    >>>    print("Value of x is: " + str(x))
    >>>    x = x + 1
        
    Value of x is: 0
    Value of x is: 1
    Value of x is: 2
    Value of x is: 3
    Value of x is: 4
    Value of x is: 5
    Value of x is: 6
    Value of x is: 7
    Value of x is: 8
    Value of x is: 9
    

    When using while loops, you can also have if/else logic with the while loop, and use statements to break out of the while loop or continue the while loop. Those statements are called:

    break

    continue

    The while loop starts with number of cakes set to 15, "cakenum = 15"; then the count is subtracted by 1, when it reaches 5 it breaks out the loop, and message printed to order more cakes.

    cakenum = 15
    while cakenum > 0:
        print("We got " + str(cakenum) + " cakes left")
        cakenum = cakenum -1
        if cakenum == 5:
            print("Quick! Only " + str(cakenum) + " cakes left, order more cakes!")
            break
           
    print("Enjoy eating at cakenum")
    

    Result:
    We got 15 cakes left
    We got 14 cakes left
    We got 13 cakes left
    We got 12 cakes left
    We got 11 cakes left
    We got 10 cakes left
    We got 9 cakes left
    We got 8 cakes left
    We got 7 cakes left
    We got 6 cakes left
    Quick! Only 5 cakes left, order more cakes!
    Enjoy eating at cakenum

    If the break statement is changed to continue it will continue until all cakes have finished.

    cakenum = 15
    while cakenum > 0:
        print("We got " + str(cakenum) + " cakes left")
        cakenum = cakenum -1
        if cakenum == 5:
            print("Quick! Only " + str(cakenum) + " cakes left, order more cakes!")
            continue
           
    print("Enjoy eating at cakenum")
    

    Result:
    We got 15 cakes left
    We got 14 cakes left
    We got 13 cakes left
    We got 12 cakes left
    We got 11 cakes left
    We got 10 cakes left
    We got 9 cakes left
    We got 8 cakes left
    We got 7 cakes left
    We got 6 cakes left
    Quick! Only 5 cakes left, order more cakes!
    We got 5 cakes left
    We got 4 cakes left
    We got 3 cakes left
    We got 2 cakes left
    We got 1 cakes left
    Enjoy eating at cakenum

    Getting hungry yet?


Log in to reply
 

© Lightnetics 2024