Breaking

Saturday, 31 August 2019

Python : Control Flow Tools

#Python05
Python : Control Flow Tools

Besides the while statement just introduced, Python knows the usual control flow statements known from other languages, with some twists, you'll learn everything about Control flow in Python, their syntax and how to use them with examples.



control flow statements:

       1. if statements
       2. for statements
       3. The range function
       4. break and continue Statements, and else Clauses on Loops
       5. pass statement


1.if statements

1.1 Simple if  

Python supports the usual logical conditions from mathematics:


  •  Equals: a == b 
  •  Not Equals: a != b 
  •  Less than: a < b 
  •  Less than or equal to: a <= b 
  •  Greater than: a > b 
  •  Greater than or equal to: a >= b 
These conditions can be used in several ways, most commonly in "if statements" and loops. An "if statement" is written by using the if keyword.


Example:
            x = 5 y = 2
            if x<y:
                 print("x is less than y")  
            if x>y:
                 print("x is greater than y")
            if x==y:
                 print("x is equal to y")
            if x<=y:
                 print("x is less than or equal to y")
            if x>=y:
                 print("x is greater then or equal to y")              
            
1.2 elif  (else if)  

 This is same as else if statement of other programming languages.

Example:
            x = 5 y = 2
            if x<y:
                 print("x is less than y")  
            elif x>y:
                 print("x is greater than y")
            elif x==y:
                 print("x is equal to y")
            elif x<=y:
                 print("x is less than or equal to y")
            elif x>=y:
                 print("x is greater then or equal to y")  
            



TRY CODE ONLINE

            

No comments:

Post a Comment