Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

In the heart of programming logic, we have the if statement in Python. The if statement is a conditional that, when it is satisfied, activates some part of code. Often partnered with the if statement are else if and else. However, Python's else if is shortened into elif. If statements are generally coupled with variables to produce more dynamic content. Let's cut to an example really quick.

Example a = 20
if a >= 22:
print("if")
elif a >= 21:
print("elif")
else:
print("else")
Result else

So, we have the variable a that equals twenty. Now, we run it through our if statement that checks to see if a is greater than or equal to 22. It isn't. So, we skip past the inner print statement and continue to the elif statement. This conditional checks if a is greater than or equal to 21. It isn't either, which brings us to the final leg of our expanded if statement. The else is the catch all, which means if the previous conditionals are not satisfied, we will run the code inside it. So, we run the print("else"), and we see in the results, the string else.

The If Syntax

The most important thing you might have missed in the example above is how Python's syntax is a lot cleaner than other languages. However, this also means it is very picky and tends to bite beginners with errors. After every conditional we have a colon. Next, you must proceed to a new line with 4 spaces to tell Python you only want this code with 4 spaces to be run when the previous conditional is satisfied. Ok, well it doesn't have to be 4 spaces, but you must be consistent with the spaces you use to indent. You can use 3 every time if you want, but 4 is kind of a standard. Also, if you wanted to run more than one statement after the conditional is satisfied, you must have a new line with the same number of spaces before the next statement.

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    mindwars Jan. 14, 2017, 4:01 a.m.

    i get different results here to the idle result. is this python 2 or 3 simulator?

  • User

    Kallam011011 Oct. 22, 2015, 8:21 a.m.

    A = 25 if a > 20: print ("yes") a- = 6 if a < 20 print ("nope")

  • User

    johansen89 July 10, 2015, 7:40 a.m.

    a = 20 if a >= 21: print("fart") elif a <= 19: print("Fart") else: print("FART MCFARTY!")

  • User

    Clockwork_Automaton June 5, 2015, 5:18 p.m.

    Just starting out, and this is surprisingly fun :)

  • User

    jasalia999 June 4, 2015, 6:21 a.m.

    I'm using IDLE Python 3.2, I typed in the exact code shown in the tutorial. After typing "elif a >= 21:" it immediately tells me I've made a syntax error. How do I fix this? Do I need to close of the original 'if' statement somehow?

  • User

    Wheelie26 June 4, 2015, 3:38 a.m.

    How does the if statement work when you need to have multiple elif ??

  • User

    tron8009 May 15, 2015, 1:04 p.m.

    i have been programming in C and C++. I jus love the simplicity of Python in terms of syntax. I will be going a long way with this. #addictive

  • User

    John Snow May 14, 2015, 2:45 a.m.

    blueBerryHeight = 10 if blueBerryHeight >= 20: print("Adult") elif blueBerryHeight >= 25: print("Aged") else: print("sapling") #My outcomes don't make sense lol, but this programming stuff is fun ;)

  • User

    Subwwoofer May 13, 2015, 9:18 p.m.

    A = 25 if a > 20: print ("yes") a- = 6 if a < 20 print ("nope")

  • User

    John Snow May 12, 2015, 3:18 a.m.

    a = 25 if a >= 26: print("if") elif a <= 26: print("elif") else: print("else")

  • User

    Michael April 14, 2015, 2:31 p.m.

    When i type >>> a = 20 >>> if a >=22: print("if") elif a >= 21: As soon as hit enter after the 21: I get an error SyntaxError: unindent does not match any outer indentation level I'm using 3.2

  • User

    Tito Canovi March 30, 2015, 4:38 a.m.

    I Used to program with C#, Js, SQL, Asp.net etc this is like an aspirin. I like Python...

  • User

    dan March 17, 2015, 12:23 p.m.

    lol

  • User

    Just4U Jan. 17, 2015, 6:46 p.m.

    HEY, just made an account to say thanks. I never new why I was getting a synthax error every time I made an IF ELSE statement, after I looked at you code I realized that after you finish an IF statement, and move onto the else, you have to hit space again THEN write the else. I know other OOP languages, but this never occurred to me that in PY you need a space. MY noob ass teacher never even mentioned this, so I was stuck on my assignment until now. Thanks again.

  • User

    bigingame200 Jan. 10, 2015, 1:45 p.m.

    awesomeness

  • User

    Kreel Jan. 8, 2015, 7:54 p.m.

    a=20 if a>22 print "no" else print "zzz"

  • User

    Josh_J08 Dec. 27, 2014, 9:41 a.m.

    It's quite easy....for now

  • User

    g_econ Oct. 27, 2014, 2:55 p.m.

    I figured out myself! yay!

  • User

    g_econ Oct. 26, 2014, 2:09 p.m.

    I do this in IDLE if a>=22: print"if" elif a>=21: and I get this error File "<pyshell#4>", line 3 elif a>=21: ^ IndentationError: unindent does not match any outer indentation level please help me understand where I am going wrong

  • User

    EpicYoloTime Aug. 29, 2014, 4:17 p.m.

    I like magical rainbow-pooping unicorns.

  • User

    Harrison Defries July 2, 2014, 5:44 p.m.

    While running an &quot;If Statement&quot; in Python and following these directions I was getting an error &quot;SyntaxError: multiple statements found while compiling a single statement&quot; if this is happening to you, here's what you need to do. 1. open up your IDLE (Python GUI) write your code 2. Go to &quot;Run&quot; click &quot;Run module. 3. save file under folder scripts 4. it should run it successfully. Copying and pasting to modules doesn't work like you think it would.

  • User

    Harrison Defries July 2, 2014, 5:39 p.m.

    While running an &quot;If Statement&quot; in Python and following these directions I was getting an error &quot;SyntaxError: multiple statements found while compiling a single statement&quot; if this is happening to you, heres what you need to do.

  • User

    Clau Lacatus June 28, 2014, 2:35 a.m.

    Darren, am new here but I saw u made a mistake, if I am write; Let's see if am right: x= 55 if x &gt;=56: print ('too high') elif x &lt;= 54: &lt;------ first rule print ('too low') elif x &lt;= 33: &lt;------ here it will always take the first rule you`ve made where x&lt;=54 and print ('Im flattered') will never print this even X is lower than 33 cause is also lower than 54! elif x &gt;= 80: print ('your taking the piss') else: print ('you guessed correct')

  • User

    pacoalp May 4, 2014, 10:05 p.m.

    MILOS, if F5 not work, search Run --- Module run

  • User

    pacoalp May 3, 2014, 12:28 a.m.

    I was the same problem

  • User

    pacoalp May 3, 2014, 12:27 a.m.

    MILOS, in your shell, file ---- New Window, write the code in the example, save it, and press F5

  • User

    Nishantha May 1, 2014, 6:18 a.m.

    if statement bit different in Python than other languages. Indent is a new thing to me.

  • User

    MaximusBo April 23, 2014, 7:41 p.m.

    Hello there guys, i am trying to learn python and this is honestly really helping me. Thank you very much :D

  • User

    Vicki April 20, 2014, 8:55 a.m.

    Does Python have a Case statement?

  • User

    Vasiluk Wolf April 15, 2014, 11:07 a.m.

    How work in &quot;If standartment&quot; if i have some many variable?

  • User

    Fowled_Out April 6, 2014, 5:45 p.m.

    Instead of using the spacebar four times just press &lt;tab&gt;

  • User

    rousbel_villar March 2, 2014, 5:48 a.m.

    that was quite good but i have a little question, what if i wanted to execute this as a program. How can i start an input for the user? regards!

  • User

    Lotfi GHAZOUANI Feb. 26, 2014, 1:06 p.m.

    It's so important to understand the syntax of the if statement.

  • User

    Lotfi GHAZOUANI Feb. 25, 2014, 1:52 p.m.

    Very interesting to work with the conditional if statement. It's a great step to understand how it is working with Python.

  • User

    Jarek Feb. 5, 2014, 2:20 p.m.

    That is absolute magic! What have I been doing all my life? :)

  • User

    Plateau14 Jan. 27, 2014, 3:02 p.m.

    This is a great site so far. It seems to teach Python at a good, slow rate. I'm also learning Java/Android at the same time and it's just not clicking yet. I know this site is for web site development, but maybe it could pick up some mobile development in the future.

  • User

    Pyn00b Dec. 24, 2013, 9:52 a.m.

    4 spaces or &quot;tab&quot;

  • User

    SephraDeath Oct. 22, 2013, 8:17 a.m.

    I guess the only suggestion I could come up with would be a color coded shell. It would be nice to see but, not absolutely needed.

  • User

    Onofre Aquino Aug. 28, 2013, 8:54 a.m.

    @sandaruwan, I liked your use of the &quot;input()&quot; function call.

  • User

    Darren Aug. 28, 2013, 12:32 a.m.

    works like a charm: &gt;&gt;&gt; x= 55 &gt;&gt;&gt; if x &gt;=56: print ('too high') elif x &lt;= 54: print ('too low') elif x &lt;= 33: print ('Im flattered') elif x &gt;= 80: print ('your taking the piss') else: print ('you guessed correct') answer you guessed correct

  • User

    sandaruwan Aug. 27, 2013, 6:58 a.m.

    c=input(&quot;Enter Your Marks:&quot;) c=int(c) if c&gt;50: print(&quot;GooD&quot;) elif c&lt;50: print(&quot;BaD!&quot;) else: print(&quot;Enter Your marks correctly&quot;)

  • User

    Onofre Aquino Aug. 26, 2013, 11:29 a.m.

    ''' This are my code that I tried. It's 2:27 AM and I needed something to keep me awake, hence the subject of my code. :) ''' sex = 55 if sex &gt;= 100: print('Too many!') elif sex &lt;= 1: print('Get laid!') elif sex == 20: print('Just right.') else: print('else')

  • User

    m0s1n July 7, 2013, 3:11 a.m.

    # Heres my try a = 5 if a == 4: print (&quot;wrong&quot;) elif a == 6: print (&quot;incorrect&quot;) else: print (&quot;success&quot;)

  • User

    Ahmed July 5, 2013, 8:22 a.m.

    worked perfectly for me and got a little creative ;) print &quot;type a number greater than 25&quot; a =raw_input(&quot;:&quot;) if a &gt; 25: print(&quot;good job&quot;) elif a == 25: print(&quot;that's not greater&quot;) elif a &lt; 25: print(&quot;try again&quot;) else: print(&quot;input a number please&quot;)

  • User

    stefan June 16, 2013, 2:10 p.m.

    Dan c, mine was like this &gt;&gt;&gt; if a &gt;=22: print (&quot;if&quot;) elif a &gt;= 21: SyntaxError: invalid syntax then I changed it to this and it worked: &gt;&gt;&gt; if a &gt;=22: print (&quot;if&quot;) elif a &gt;= 21: print (&quot;elif&quot;)

  • User

    Dan C May 19, 2013, 9:08 a.m.

    getting the same problem with the elif command I'm using 3.3.2, any ideas?

  • User

    Rovi Hilzawan May 11, 2013, 6:31 p.m.

    hey, when I enter elif a &gt;= 21: it's got syntax error. is elif really correct command?

  • User

    Sweet Diablo April 26, 2013, 12:26 p.m.

    @Will H my guess is you use idle ? then try this : IDLE &gt; options &gt; General &gt; at startup Open Edit window . restart idle and you can fill in the example. F5 to run the module

  • User

    Jared Drake April 24, 2013, 2:37 p.m.

    @Will H The example runs perfectly in Python 3.2.3. Are you sure your indentation is consistent?



Advertisement