Until we learn about python basics, Data types in Python, input() & print() function, so now we start from conditional statements. What is condition statements in python?
Condition Statements -
The most common condition statement is the comparison statement. If we want to compare whether two variables are the same, we use the == sign.
For instance, if you write x == y, you are asking the program to check if the value of x is equals to the value of y. If they are equal, the condition is met and the statement will evaluate to True. Else, the statement will evaluate to False.
Read also - What is input() & output() function in python?
Some important comparison signs-
!= Not equal
< less than
> greater than
<= less than or equal to
>= greater than or equal to
For example -
(i) 5!=2 (5 is not equal to 2)
(ii) 2<8 (2 is less than 8)
(iii) 6>12 (6 is greater than 12)
(iv) 3>=1 3>=3 Greater than or equals to
(v) 3<=7 3<=3 Smaller than or equals to
If - else statement -
The if statement is one of the most commonly used control flow statements. It allows the program to evaluate if a certain condition is met & elif stands for else if.
Example - (With full code)
amount=int(input("Enter amount : "))
if amount<5000:
discount=amount*0.08
print("Discount is: ",discount)
else:
discount=amount*0.4
print("Big Discount is: ",discount)
print("Net payable amount is: ", amount-discount)
output of this code is -
if amount is less than 5000 then output is discount with 0.08.
But if amount is greater than 5000 then output is discount with 0.4
Read also - What are data types in Python?
For loop -
The for loop executes a block of code repeatedly until the condition in the for statement is no longer valid.
Example -
pets = ['cat','dog','rabbits','cow', 'horse']
for myPets in pets:
print (myPets)
output of this for loop -
While loop -
Like the name suggests, a while loop repeatedly executes instructions inside the loop while a certain condition remains valid.
Example -
i = 3
while i < 9:
print(i)
i+= 1
output of this while loop is -
Conclusion -
I hope this article will help you to understanding that what is conditional statement in python. If you have any doubt you can ask me in comment.
0 Comments