11 conditional operations in python
1. Using if, elif and else we can perform conditional operations
2. Using conditioanl operations we can execute a block of code based on condition
# Below are the python operators to perform conditional operations
1. Arthmetic operators(+,-,*,%,/,//)
1. Uesd to do addition and substraction operations
2. Assignment operators(=,+=,-=,*=,%=,/+,//+)
1. Used to assign value to variable
3. Comparison operators(==,!=,>,>=,<,<=)
1. These operators compare the values on either sides of them and decide the relation among them.
They are also called Relational operators.
4. Logical operators(and,or,not)
1. Using logical operations we can execute a block of code based on multiple condition.
5. Membership operators(in,not in)
1. Using member ship operations we can find a value in list and sub string in a string
6. Identity operators(is,is not)
1. Using identity operators we can check memory location of objects
1) simple if statement
#name = 78
#if name <= 78:
# name //= 10
# print(' Name is :', name)
if name = 99:
name // = 10
print(' Name is :', name)
# Give nuber is even or not
n = int(input("enter the number?"))
if n%2 == 0:
print("Number is even")
#Program to print the largest of the three numbers
a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");
2) if else statement
name = 'uma'
if name == 'mahesh':
name += 'uma'
print(' Name is :', name)
else:
print(name)
sal = 2344
print(sal)
#Program to check whether a number is even or not.
n = int(input("enter the number?"))
if n%2 == 0:
print("Number is even")
else:
print("Number is odd")
#to check whether a person is eligible to vote or not.
age = int (input("Enter your age? "))
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
3) if else comparision
# number equal to 10,20 or 100
n = int(input("Enter the number?"))
if n==10:
print("number is equals to 10")
elif n==50:
print("number is equal to 20");
elif n==100:
print("number is equal to 100");
else:
print("number is not equal to 10, 20 or 100");
# Identify grade of students
marks = int(input("Enter the marks? "))
if marks > 60 and marks <= 100:
print("Congrats ! first class ...")
elif marks > 50 and marks <= 60:
print("second class.")
elif marks > 40 and marks <= 50:
print("Third class")
elif (marks > 35 and marks <= 40):
print("pass")
else:
print("Sorry you are fail ?")
n = 54
if n == 20:
print(' If block ')
elif n != 54:
print(' First elif ')
elif n > 30:
print('Second elif')
elif n >= 11:
print('Third elif')
elif n < 2:
print(' Forth elif')
else:
print(' Else block')
Comments
Post a Comment