+91 81900 98289

Shorthand If

This Shorthand If lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A single action can sit on the same line as if. Keep that line short enough to read in one glance.

As soon as you need a second statement, move the body onto indented lines.

Start here

The colon is still required.

score = 82 # a whole number if score >= 70: print("pass") # run the indented lines only when this is true

One-line if

A single action can sit on the same line as the if.

score = 82 # a whole number if score >= 80: print("Pass") # run the indented lines only when this is true

Choose a value

This form always produces a value, so it fits inside print.

score = 70 # a whole number print("Pass" if score >= 80 else "Try again") # a comparison is True or False

Keep the long form when it grows

Use the indented form as soon as you need more than one line.

score = 82 # a whole number if score >= 80: # run the indented lines only when this is true print("Pass") print(score)
The colon is still required.