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')

4) logical operators
'''
  1. Using logical operations we can execute a block of code based on multiple condition.
'''

sal  = 4000
dname = 'IT'

#if sal >= 50000 and dname == 'IT' : print(sal + 2500)

#if sal >= 50000 or dname == 'IT' : print(sal + 2500)

if not sal != 4000: print('  NOT  ')

5) membership function
'''
1. Using member ship operations we can find a value in list and
 sub string in a string
2. We can't perform membership opertion on number data type.
'''

#name = 'umamahesh'
#if 'ma' in name: print('IN')
#if 'k' not in name: print('NOT IN')

names = ['pratap','ganesh','kumar','balu']
#t = ('sriram','ganesh','kumar','balu')
#s = {'sriram','ganesh','kumar','balu'}
if 'pratap' in names:  print(names)

#d = {'name':3, 'eid':56, 99:89, '55':33}
#if 99 in d: print(' Dictionary')


#n = 8976
#if 97 in n: print(n)

6) identity operator
'''
1. Using identity operators we can check memory location of objects.
'''

#a = 10
#b = 10
#c = a
#if a is c : print(' IS ')


l = [1,2,3]
l2 = l
print(' L id is  : ', id(l))
print(' L2 id is : ', id(l2))

if l is l2 : print(' OK ')

####  Assignment on control statements
1. Use of conditional operations ?
2. How to perform conditional operations in python ?
3. Can i use if statement with out else ?
4. How to create block in python ?
5. what is indentation in python ?
6. What are the logical operations and use ?
7. What are the membership operations ?
8. What are the identity operations ?
9. Syntax for if statement ?
10. What are the True And False scinarios in python ?


Comments

Popular posts from this blog

1 PYTHON PROGRAMMING

4 Tuple data types

2 String data types