Statements in Python

In this part of Learning Python we Cover how to use Statements in python
Written by Paayi Tech |10-Apr-2019 | 0 Comments | 430 Views

In python there are two types of statement IF or ELSE. It can be explained by the example “A father told his son to go to his room and search for his glasses, if he finds his glasses than bring those glasses to him otherwise go to mom and asked her about the glasses”. Same is the property of if else in python the basic syntax of if else statement is following:

 

Syntax:

If( condition ):
    #statement
else:
    #statement

 

The above defined syntax is compulsory for if else statement. There must be 4 tabs indentation after the condition and there must be colon after if and else.

Some of the example codes are following which make it clearer.

 

#Case1

string = "Pooh"
 
if(string=="Pooh"):
    print("String Matched")
else:
    print("String Not Matched")
 

 

 

In the above case string is actually equal to pooh so if condition will be printed

 

#Case2

string = "Alladin"
if(string=="Pooh"):
    print("String Matched")
else:
    print("String Not Matched")

 

 

In case 2 string is not matched so else statement will be printed.

 

Finding word in string:

suppose we have a long string and we want to know that particular word is present in a string or not for that we can use if statements. If that word is present in that string than it will say I found else not found.

string = '''Python is one of the growing programming language
according to stackoverflow specially in the field of data science
and machine learning'''
 
if 'machine learning' in string:
    print('Yes it is there')
else:
    print('not present')

 

 

The in keyword search that particular argument in a string or maybe in a list.

We can negate by using not keyword before in keyword as following:

 

string = '''Python is one of the growing programming language
according to stackoverflow specially in the field of data science
and machine learning'''
 
if 'Java' not in string:
    print('Not present')
else:
    print('present')

 

 

Statements with bool opeartors:

We can also verify the conditions by using bool operator in a condition section. Following is the example which clarify the concept:

friend1 = "Robert"
friend2 = "Peter"
 
if((friend1=="Robert")and(friend2=="Peter")):
    print("Both friends matched")
else:
    print("not matched")

 

 

Elif Statement:

We can use multiple if statements by using elif keyword for this:

Following is the example of using elif keyword:

 

friend1 = "Robert"
friend2 = "Peter"
 
if((friend1=="Robert")and(friend2=="Norton")):
    print("If statement run")
 
elif((friend1=="Robert")and(friend2=="Peter")):
    print("Elif statement run")
 
else:
    print("else run")

 

 

As we see the condition of elif statement is true so the output will be “Elif statement run”.

We can represent the flow diagram of if else statement as following:

 

 

 

As we can see the code starts it enter into the condition where it have to make decision. If condition is true than “if” block is executed otherwise “else” code will be executed.





Login/Sign Up

Comments




Related Posts



© Copyright 2019, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.