Learning Python - 4th day (Noon - 2) (Coolest)

Watched this tutorial: Click here

I have learnt about (if, elif, else) functions.

Let's make a story first.

Imagine a football match of Argentina and Brazil has just started.  Lionel Messi - one of the best football player alive, has planned about how he should score a goal against the defenders of brazil if he gets a freekick.
He had a crazy plan. His plan was,

If defenders try to jump higher all together :
     Messi will shot the ball for a goal.
Else if the players try to defend the ball from left :
     Messi will shot the ball for a goal
Else if the players try to defend the ball from right :
     Messi will shot the ball for a goal.
Else
     Messi will pass the ball to a team-mate

Now let's write it in python:

if defenders == Jumps Higher:
     Shot = true
     Pass = false

elif defenders == go left:
     Shot = true
     Pass = false

elif defenders == go right:
     Shot = true
     Pass = false

else:
     Shot = false
     Pass = true

Just like this let's see a real python coding example:

Input:  

x = 18

if (x+1 == 19):
    print("Value is correct")
elif(x+1 == 20):
    print("Value is wrong")
else:
    print("Value cannot be identified")

After I run this code, Output: Value is correct

Now let's change the value of x to 19

x = 19

if (x+1 == 19):
    print("Value is correct")
elif(x+1 == 20):
    print("Value is wrong")
else:
    print("Value cannot be identified")


After I run the second code, I get the Output: Value is wrong

Let's again change the value of x to 40

x = 40

if (x+1 == 19):
    print("Value is correct")
elif(x+1 == 20):
    print("Value is wrong")
else:
    print("Value cannot be identified")


After the third code gets run, Output: Value cannot be identified 


This is how (if, elif, else) work.

Important notes: 
  • You can also use no brackets for faster coding. For example: 

    if x+1 == 19:
       print("Value is correct")


    This will also work.
  • You must have to use (:) colon after each condition you write.
  • You need to keep in mind that, the indents after a condition line is same for every workflows.
    For example: 

    if (x+1 == 19):
        print("First Line")
      print("Second Line")

    This won't work. You have to make it:

    if(x+1 == 19):
        print("First Line")
        print("Second Line")

    You can use your preferrable number of spaces, but just make sure that every workflow line has the same number of spaces.
    Python programmers normally use 4 spaces for better visualization of a code.

Comments

Popular posts from this blog

Apple iPad Air (2022) Specifications and Review in 2023

17-year-old suspect in fatal stabbing at Taylor Swift-themed dance event ID’d

Exercise vs Diet