RELATIONAL OPERATORs IN PYTHON Before we go to conditional statements we would need to understand Relational Operators in python and learn how they work.
CONDITIONAL STATEMENT In Python we use different conditional statements to put condition on the flow of program and control the flow of program. For right now let’s see simple ‘if’ conditions Simple ‘if’ statement can be used to do decision making
IF STATEMENTS IN PYTHON It is very easy to write ‘if’ statements in python Syntax if <condition>: statement1 statement 2 One thing we have to keep in mind while writing ‘if’ statement is that we have to keep indent while we are writing statements inside ‘if’ statements.
EXAMPLE
IF ELSE STATEMENT IN PYTHON 'If ..else’ evaluates test expression and will execute the body of if only when the test condition is True. If the condition is False, the body of else is executed. Indentation is used to separate the blocks. Syntax: if <condition>: statements… else: statements…
EXAMPLE
IF..ELIF..ELSE STATEMENTS IN PYTHON The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False, it checks the condition of the next elif block and so on. If all the conditions are False, the body of else is executed. Only one block among the several if... elif ...else blocks is executed according to the condition. The if block can have only one else block. But it can have multiple elif blocks.
NESTED IF STATEMENTS We can have a if... elif ...else statement inside another if... elif ...else statement. This is called nesting in computer programming. Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting.
SYNTAX if <condition>: if <condition>: statement else: statement else: statement
EXAMPLE
Sample Programs # 1 WAP to compare 2 numbers n1=int(input(“Enter first number-:”)) n2=int(input(“Enter second number-:”)) if n1>n2: print(n1, “is larger”) else: print(n2, “is larger”)
Sample Programs # 2 WAP to decide whether the given number is odd or even no=int(input(“Enter a number-:”)) if no % 2==0: print(no, “is even”) else: print(no, “is odd”)
Sample Programs # 3 WAP to decide whether the given year is leap year or not year=int(input(“Enter year in yyyy format-:”)) if year % 4==0: print(no, “is even”) else: print(no, “is odd”)
Sample Programs # 4 WAP to decide whether the given number is + ve or – ve no=int(input(“Enter a number-:”)) if no < 0: print(no, “is - ve ”) else: print(no, “is + ve ”)
Sample Programs # 5 WAP to decide whether the student is passing or not mark=int(input(“Enter the aggregate %-:”)) if mark < 33: print(“Student is failing”) else: print (“Student is passing”)
Sample Programs # 6 WAP to decide whether the shopkeeper is getting profit or loss on selling a product. Display profit/loss cp=float(input(“Cost price of the product:”)) sp =float(input(“Selling price of the product:”)) if cp> sp print(“Shopkeeper is getting loss”) print(“Loss= ”,cp- sp ) else: print(“Shopkeeper is getting profit”) print(“Profit= ”, sp -cp)