If, Then, Else in Python || Python Tutorial || Learn Python Programming

Video is ready, Click Here to View ×


When coding in Python, you will frequently encounter a fork in the road. Depending on the values of certain data, you may want to go in one direction or the other. There may even be more than two directions for you to choose. The if-then-else statements help you navigate these situations.

➢➢➢➢➢➢➢➢➢➢
To learn Python, you can watch our playlist from the beginning:…

38 Comments

  1. python doesnt support switch cases which is a shame because I do find them easier to read when dealing with larger conditional flow. Using a dictionary is the nearest thing to a switch case. Which I use regulary when dealing with large conditions. Its much less code and imo a little easier to read. example:
    import sys
    import os
    switcher = {
    "q": (sys.exit, []),
    "c": (os.getcwd, []),
    "l": (os.listdir, ["/"])}
    while 1:
    sys.stdout.write("Q – QuitnC – List Current DirnL – List Rootn")
    func, args = switcher.get(raw_input(": "), (lambda : "Unknown Command", []))
    if callable(func):
    print func(*args)

  2. just my 2 cents for those stuck in 1:33 and using python 3.
    a = input("Please enter a test string: ")

    if len(a) < 6:

    print("Your string is too short.")

    print("Please enter a string with at least 6 characters.")

  3. C:Usersvuhunpython_examples>python if_then.py

    Traceback (most recent call last):

    File "if_then.py", line 1, in <module>

    input = raw_input("Please enter a test string:")

    NameError: name 'raw_input' is not defined

    Please help me why happening Error!!!! thanks a lot

  4. i use python 3.7 but i can not see VIM.please help me

    C:Usersvuhun>cd python_examples

    C:Usersvuhunpython_examples>vim if_then.py

    'vim' is not recognized as an internal or external command,

    operable program or batch file.

    C:Usersvuhunpython_examples>

  5. To test to see if it is a valid triangle…
    The sum of lengths of the two smaller sides must be greater than the length of the longest side.

    You've got to sort the sides to find the biggest one.
    You then have to sum the two smaller sides and make sure the total is bigger than the longest side.
    If it's not then it's not a valid triangle.

  6. This video was convoluted again. raw_input does not work. u have to use input. raw input was merged to just input after ver 3.4 apparently…thankyou comments!Just disappointing we don't know what version u use for this video and we wouldnt know that without the nice commenters etc. Whatever, I'm just disappointed.

  7. a = int(input("The length of side a = "))
    b = int(input("The length of side b = "))
    c = int(input("The length of side c = "))

    if a+b>c and a+c>b and b+c>a:
    if a != b and b != c and a != c:
    print("This is a scalene triangle.")
    elif a == b and b == c:
    print("This is an equilateral triangle.")
    else:
    print("This is an isosceles triangle.")
    else:
    print("This is not a triangle.")

    # 1st day of coding, pls let me know what you think about this!

  8. Thanks for the video. But when I am trying as per below. nothing is working "

    input = raw_input("Please Enter A String :")
    if len(input) < 6:
    print("Your string is too short.")
    print("Please enter a string with at least 6 characters")

    The shell is showing

    "Traceback (most recent call last):
    File "C:UsersMPPLDesktoptest.py", line 2, in <module>
    input = raw_input("Please Enter A String :")
    NameError: name 'raw_input' is not defined"

    Where is the mistake I made ??

  9. The exercise at 1:37 does not run correctly in Python 3.6.4 shell. The raw_input is not recognized, nor is it recognized in pycharm. I had to try this exercise in pycharm:

    input = input("Please enter a test string: ")

    if len(input) < 6:
    print("Your string is too short.")
    print("Please enter a string with at least 6 characters.”)

    This way worked.

Leave a Reply

Your email address will not be published.


*