Python: Perform an action if a condition is true
Conditional Action
Write a Python program to perform an action if a condition is true.
Given a variable name, if the value is 1, display the string "First day of a Month!" and do nothing if the value is not equal.
Sample Solution-1:
Python Code:
# Assign the value 1 to the variable n.
n = 1
# Check if the value of n is equal to 1.
# If the condition is True, print the message indicating the first day of the month.
if n == 1:
print("\nFirst day of a Month!")
# Print an empty line.
print()
Sample Output:
First day of a Month!
Sample Solution-2:
Python Code:
# Prompt the user to input a number and convert it to a floating-point value.
n = float(input("Input a number: "))
# Check if the value of n is equal to 1.
# If the condition is True, print the message indicating the first day of the month.
if (n == 1):
print("First day of a Month!")
# If the condition is False, print an empty line.
else:
print()
Sample Output:
Input a number: 1 First day of a Month!
Python Code Editor:
Previous: Given variables x=30 and y=20, write a Python program to print "30+20=50".
Next: Write a Python program to create a copy of its own source code.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics