If-Else statements - Python Tutorials For Beginners #6

What is If-Else statement ?

An if-Else statement is a method for writing conditions. Another method available today is match cases. We start writing our first condition by using the 'if' keyword and the other by using the keyword 'elif'. The last condition was else it runs when other conditions will not execute. Every condition contains some code, It runs when the condition will be executed.

Writing technique:-

Steps for writing condition:-

  1. Create a variable. (Optional).
  2. Start the first condition with the 'if' keyword.
  3. Use parenthesis and write the condition in it.
  4. Use a colon after the condition.
  5. After completing all these steps press 'Enter' for in condition and start writing code for coditions as shown in the code.
  6. Finally, Use the 'else' keyword and use the colon for writing the 'else' keyword.
 1# If-Else statements in python
 2
 3x = input("Enter the number : ")
 4
 5if (x == "1"):
 6    print("The value is 1")
 7
 8elif (x == "2"):
 9    print("The value is 2")
10
11elif (x == "3"):
12    print("The value is 3")
13
14elif (x == "4"):
15    print("The value is 4")
16
17else:
18    print("Other value")

Watch Video Tutorial On Youtube:-