#6 Python Tutorial for Beginners

Hello Guys ,  

  In previous tutorial you learned about Dictionary , Nested Dictionary , Dictionary Method , set and set Method in python.

 In this tutorial you will learn below points:

  • if statement
  • if-else statement
  • Nested if-else statement
  • elif statement
  • pass statement

Let's get started.

If Statement

  • if statement is the simple decision making statement.
  • if certain condition is met then a true block statement is executed. 
  • if statement is written using "if" keyword.
  • else statement is optional.
Example :

A = 10

if(A == 10):
    print("This is equal to 10")

#else statement is optional   

Output :

This is equal to 10


If-else Statement

  • if certain condition is met then a true block statement is executed and otherwise else block executed.
  • else block is optional. 
Example :

a = 20
b = 30

if (a > b):
    print(a ,"is greater than"b)
else:
    print(b ,"is greater than" ,a)    


Output :

30 is greater than 20


Nested If-else Statement

  • if statement inside another if statement called "nested if-else statement". 
  • In this statement (outside)if condition is check if it's true then inside if statement condition check and it's true then inside if statement block executed otherwise inside else statement block executed.
  • (outside) if statement condition is false then outside else block executed.
 Example :

#find maximum number
A = 20
B = 50
C = 40

if(A > B):
    if(A > C):
        print(A,"is maximum number")
    else:
        print(C , "is maximum number")    
else:
    if(B > C):
        print(B ,"is maximum number")
    else:
        print(C , "is maximum number")    
 
Output :

50 is maximum number


elif Statement

  • In this statement you can create multiple condition options. 
  • In this statement if condition is check if it's false then check another condition if that condition is true then statement executed . if no one condition is true then else block excuted. 
  • elif statement create using "elif" keyword. we can create elif statement after if statement.
Example :

A = 30

if (A > 20):
    print(A , "is greater than given number ")
elif (A == 20):
    print(A , "is equal to given number ")
else:
    print(A , "is less than to given number")

Output :

30 is greater than given number


Pass Statement

  • pass is null statement.
  • pass  statement does not effect to result.
  • if you create if statement with no content and  use pass statement avoid getting an error.
  • pass statement actually use for if you want to implement in future. 
Example :
'''if statement cannot be empty. 
  But you have some reason you create 
  if statement with no content. put in the 
  pass statement avoid getting an error.'''

A = 2
B = 5

if(A > B):
   pass





I hope you like post. If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏   

Post a Comment

0 Comments