Conditional Statement in Python One-liner on Command Line

C

I had a situation where I needed to pass code to Python in the form of a single command instead of a script file. While this is easily done by invoking Python in command mode, the code contained a conditional statement that caused Python to throw an error.

Consider this simple Python script. It initialises the variable price, sets its value to 25 and then prints it1.

# test.py
price=25
print(price)

 

 

To run the script we pass the file name to python2 on the command line:

Steves-iMac-27-5K:~ steve$ python test.py
25

 

 

The same can be achieved by passing the code to python not as a script file but as a command using the -c option (command mode).

Steves-iMac-27-5K:~ steve$ python -c "price=25; print(price)"
25

 

 

To include a conditional if statement in the script the syntax is:

if condition :
   indented statement block
else :
   indented statement block

 

 

Adding an if statement the script now checks the value of price and if it’s less than 10 prints Cheap otherwise Expensive.

# test.py
price=25
if price < 10 :
   print('Cheap')
else :
   print('Expensive')

 

 

As the value of price is greater than or equal to 10 the script prints Expensive.

Steves-iMac-27-5K:~ steve$ python test.py
Expensive

 

 

However, passing the same code to python in command mode throws a syntax error:

Steves-iMac-27-5K:~ steve$ python -c "price=25; if price < 10 : print('Cheap')  else: print('Expensive')"
  File "", line 1
    price=25; if price < 10 : print 'Cheap' else: print 'Expensive'
               ^
SyntaxError: invalid syntax

 

 

From what I understand this is because the Python interpreter expects the indented statement blocks for the if statement to be proceeded by a newline and significant leading whitespace as they are in the script file.

I haven’t found a practical way to include newlines nor whitespace when invoking python in command mode, but Python – like many other scripting/programming languages – does have a one-line if statement often referred to as a ternary operator. The syntax is:

expression1 if condition else expression2

 

 

In our example expression1 is 'Cheap', condition is price < 10 and expression2 is 'Expensive'.

Let’s change the if statement to a ternary operator in the script file:

# test.py
price=25
print('Cheap' if price < 10 else 'Expensive')

 

 

Running the amended script gives:

Steves-iMac-27-5K:~ steve$ python test.py
Expensive

 

 

As the ternary operator requires no newlines nor whitespace it shouldn’t throw a syntax error when invoking python in command mode:

Steves-iMac-27-5K:~ steve$ python -c "price=25; print('Cheap' if price < 10 else 'Expensive')"
Expensive

 

 

1 All code was tested using Python 2.7.10 and Python 3.6.3 (installed via Homebrew) on macOS High Sierra 10.13.2 and Python 2.7.12 on Ubuntu 16.04.3.

2 When using Python 3 replace python with python3 on the command line.

About the author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of fish & chips and writing the occasional blog post he hopes others will find helpful.

2 responses

2 Comments

  • Hi, can you tell me what’s wrong with my code? 🙂

    I’m trying to learn but struggling with this already in the beginning. There’s an error on last line, but just can’t see it. If there’s also another error, please let me know.

    elif 0.7 >= score >=0.6 :
    print (“D”)
    else (“The error message is here”)

    Thanks!

    • @Maya,

      score=.5
      if 0.7 >= score >=0.6:
         print ("D")
      else:
         print ("The error message is here")
      

      score=.5 returns The error message is here
      score=.6 returns D
      score=.7 returns D
      score=.8 returns The error message is here

      However, not sure what you’re trying to do with if 0.7 >= score >=0.6: as it’s only true if score=.6 or score=.7.

      Regards, Steve

Steve

Recent Comments

Recent Posts