Beginners Python Decision Making: Understanding If-else statements effectively.

David Bosah - Jun 23 - - Dev Community

If-Else statements and its significance:

"If-else" statements are a type of conditional statements in programming which allows specific logic or conditions to be given to different blocks of code. From a broader perspective 'If else' statements are used in our everyday interaction, just like sending your neice on an errand to the groceries store and you tell her that if the carrots are fresh get them, else get cabbage.

It's literally creating a bias or condition to a code block. The benefits range from making decisions to controlling possibilities.

Practical example:

Lets say we want to write a code about weight information in such a manner that if the user is above 130kg He/She is overweight, see how we are going to write it.


Weight = int(input("enter your weight in kg here"))

if weight >= 130:
   print("You are overweight ")

else:
   print("You are good to go")

Enter fullscreen mode Exit fullscreen mode

Principles you must apply:

  1. Ensure to use small letters for your functions.

  2. Keep it simple, avoid ambiguity.

  3. Use ":" after 'else' to avoid error.

  4. Use integer function "int" to indicate that the expected input value is a number not a letter.

. . . . . . . . .
Terabox Video Player