L5:Python
L5:Python
if Statement(simplest one)
There is no switch or case statement in Python. Python lacks a switch statement (if
you haven’t, there is no need to worry about it with Python). Developers
commonly use the switch statement in other languages to create menu-based
applications.
The if...elif statement is generally used for the same purpose in Python.
However, the if...elif statement doesn’t provide quite the same functionality as a
switch statement because it doesn’t enforce the use of a single variable for
comparison purposes. As a result, some developers rely on Python’s dictionary
functionality to stand in for the switch statement.
25
09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 8
print("1. Red")
Decisions Making
print("2. Orange")
print("3. Yellow")
print("4. Green")
print("5. Blue")
print("6. Purple")
choice = int(input("Select your favorite color: "))
if (choice == 1):
print("You chose Red!")
elif (choice == 2):
print("You chose Orange!")
elif (choice == 3):
print("You chose Yellow!")
elif (choice == 4):
print("You chose Green!")
elif (choice == 5):
print("You chose Blue!")
elif (choice == 6):
print("You chose Purple!")
else:
print("You made an invalid choice!")
09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 9
Decisions Making
For x=10, what will be the output of the following code snippet?
x=int(input("Enter value of x : "))
if x < 0:
print('x is negative')
elif x % 2:
print('x is positive and odd')
else:
print('x is even and non-negative')
27