Python Programming - If Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better.

Python Programming – If Statement

The statement if tests a particular condition. Whenever that condition evaluates to true, an action or a set of actions is executed. Otherwise, the actions are ignored. The syntactic form of the if statement is as follows:

if (expression):
statement
The expression must be enclosed within parentheses. If it evaluates to a non-zero value, the condition is considered as true, and the statement following it is executed.
For example,
if(marks>=40) :
print(“Pass”)
if (num < 0):
print(“Number is negative.”)
if ch==”:
print(“You entered space.”)

Simple Program

Demo of if Statement.

In the program, shown in Figure 5.4, a credit card limit is tested for a certain purchase. The value of the variable amount is accepted from the keyboard; then it is verified using the if statement. When the condition is true (i.e., the “amount” is less than or equal to 1000), the message “Your charge is accepted” is displayed. If the condition is false, the program displays a message “The amount exceeds your credit limit”.

# Credit Card Program
amount = int(input(“Enter the amount :”))
if (amount <= 1000):
print(“Your charge is accepted.”)
if (amount > 1000):
print (“The amount exceeds your credit limit.”)RUN
>>>
Enter the amount: 1000 Your charge is accepted.
>>>
>>>
Enter the amount:1289
The amount exceeds your credit limit.
>>>

Let’s Tty

price=int(input(“Enter Price: “))
qty=int(input(“Enter Quantity: “))
amt=price*qty
if amt>1000:
print (“10% discount is applicable”)
discount=amt*10/100
amt=amt-discount
print (“Amount payable: “,amt)

Leave a Reply

Your email address will not be published. Required fields are marked *