Skip to content

Instantly share code, notes, and snippets.

@fabsta
Created August 28, 2016 20:26
Show Gist options
  • Save fabsta/9ca009f3eb45f7d6ca987874cf7449f6 to your computer and use it in GitHub Desktop.
Save fabsta/9ca009f3eb45f7d6ca987874cf7449f6 to your computer and use it in GitHub Desktop.

CONDITIONAL STATEMENTS

if statement

if x > 0:
    print 'positive'

if/else statement

if x > 0:
    print 'positive'
else:
    print 'zero or negative'

if/elif/else statement

if x > 0:
    print 'positive'
elif x == 0:
    print 'zero'
else:
    print 'negative'

single-line if statement (sometimes discouraged)

if x > 0: print 'positive'

single-line if/else statement (sometimes discouraged)

known as a 'ternary operator'

'positive' if x > 0 else 'zero or negative'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment