× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Control Statements In Python


Control Statements:

Control Statements are used to ramdom execution of the program .In python lamguage these are classified into following types.
1.Conditional Statements
2.Looping Statements
3.Unconditional Statements

Key Points

  • What is Conditonal Statements
  • What is Looping Statements
  • What is Unconditional Statements


Image

Conditional Statements
Conditional statements are used check the condition whether it is true or false.
In python language these opeatrs are classified into following types.
1.if condition
2.if else condition
3.elif condition
4.nested if condition
5.switch expression

Key Points

  • What is if Condition
  • What is if else Condition
  • What is elif Condition
  • What is nested if Condition
  • What is switch Expression


Image

if condition
'if' is a keyword in python language .Which is used to execute one block of statements by negleting some other statements.
Syntax:

if(condition):
    ....
    ....
    ....

In the above syntax 'if' condition is true the statements of 'if' block is execute.'if' the condition is false program terminated.

Key Points

  • What is if Condition


Image
if else condition

'if else' is a keyword in python language .Which is used to execute one block of statements between two blocks
Syntax:

if(condition):
    ....
    ....
else:
    ....
    ....
In the above syntax 'if' condition is true the statements of 'if' block is execute.'if' the condition is false the statements of 'else' block is execute.



ODD or Even Number Program Using IF ELSE Condition
def myfun():
    x=int(input("Enter x value"))
    if(x%2==0):
        print("Even")
    else:
        print("Odd")
    
if __name__=="__main__":
    myfun()

                                

Output:
Even Number
elif condition
'elif' is keyword in python language which is used to execute one block of statemnets amoung the multiple blocks.
Syntax:
if(condition):
    ...
    ...
elif(condition):
    ...
    ...
elif(condition):
    ...
    ...
elif(condition):
    ...
    ...
..
..
..
..
else:
    ...
    ..
    

Age Program:
0-12 child age
12-19 teen age
19-40 younger age
40-80 old age
80-100 too old age
above 100 invalid
-11 negative age

def myfun():
    age=int(input("Enter age value"))
  
    if(age>0 and age<=12):
        print("Child age")
    elif(age>12 and age<=19):
        print("teen age")
    elif(age>19 and age<=40):
        print("younger age")
    elif(age>40 and age<=80):
        print("old age")
    elif(age>80 and age<=100):
        print("too old age")
    elif(age>100):
        print("invalid age")
    else:
        print("negative age")
    
if __name__=="__main__":
    myfun()

                

Output:
Enter age value 24
You are younger
Nested if Condition
One if condition with in another if condition is known as nested if.
Syntax:
if(condition):
    if(condition):
        ...
        ...
    else:
        ....
        ...
else:
    ....
    ....

ODD or Even Number Program Using IF ELSE Condition(Value Must Be Positive)
def myfun():
    x=int(input("Enter x value"))
  
    if(x>0):
        if(x%2==0):
            print("Even Number")
        else:
            print("Odd Number")
    else:
        print("Pls enter positive number")
       
    
if __name__=="__main__":
    myfun()

                

Output:
Even Number
Switch Expression
Switch is a keyword is python language .Which is used to execute one block of statements amoung the multiple blocks. here switch is a choise expression.
Syntax:
switcher ={
    lable1: value,
    lable2: value,
    lable3: value,
    ..
    ..
    }


print(switcher.get(value,"default value"))


def myfun():
    p=(input("Enter character value"))
    switcher={
            'a':"Vowel",
            'e':"Vowel",
            'i':"Vowel",
            'o':"Vowel",
            'u':"Vowel",
            }
    print(switcher.get(p,"consonant"))
    
if __name__=="__main__":
    myfun()

                                

Output:
Vowel
if else Practice Program-1

Check the given number is valid or invalid(value must between 20 and 30)

def myfun():
    x=int(input("Enter x value"))
    if(x>=20 and x<=30):
        print("Valid Number")
    else:
        print("InValid Number")
    
if __name__=="__main__":
    myfun()

Output:
Valid Number
if else Practice Program-2

Check the given number is valid or invalid(value must divisible by 2 and 3)

def myfun():
    x=int(input("Enter x value"))
    if(x%2==0 and x%3==0):
        print("Valid Number")
    else:
        print("InValid Number")
    
if __name__=="__main__":
    myfun()


Output:
Valid Number
if else Practice Program-3

Check the given number is valid or invalid(value must divisible by 2 and 3 not 4)

def myfun():
    x=int(input("Enter x value"))
    if(x%2==0 and x%3==0 and x%!=0):
        print("Valid Number")
    else:
        print("InValid Number")
    
if __name__=="__main__":
    myfun()

Output:
InValid Number
if else Practice Program-4

Check the given character is vowel or consonant

def myfun():
    ch=input("Enter ch value")
    if(ch=='a' or ch=='e or ch=='i' or ch=='o' or ch=='u' or  ch=='A' or ch=='E' or ch=='I' or ch=='O' or ch=='U'):
        print("Vowel")
    else:
        print("Consonant")
    
if __name__=="__main__":
    myfun()

Output:
Vowel
if else Practice Program-5

Check the given character is Small Letter or Not

def myfun():
    ch=input("Enter ch value")
    if(ch>='a' and ch<='z'):
        print("Small Letter")
    else:
        print("Not a Small Letter")
    
if __name__=="__main__":
    myfun()

Output:
Small Letter