How to Use Python `if...else` in One Line
Daniel Hayes
Full-Stack Engineer · Leapcell

Key Takeaways
- Python supports one-line conditional expressions using the ternary operator.
- Use one-line
if...else
for simple assignments or return values. - Avoid nesting ternary expressions to maintain code readability.
Writing clean and concise code is one of Python’s strengths, and one way to make your code more compact is by using single-line if...else
expressions, also known as ternary operators.
This article will guide you through how to write one-line if...else
statements in Python, explain their syntax, and provide examples of best practices.
Basic Syntax
The one-line if...else
statement in Python follows this structure:
value_if_true if condition else value_if_false
This is Python’s version of the ternary operator that is common in many programming languages.
Example Usage
Here are a few simple examples to demonstrate how it works:
Example 1: Simple Comparison
x = 10 result = "Even" if x % 2 == 0 else "Odd" print(result) # Output: Even
Example 2: Assigning Based on Condition
age = 20 status = "Adult" if age >= 18 else "Minor" print(status) # Output: Adult
Example 3: Inside a Function
def max_value(a, b): return a if a > b else b print(max_value(5, 8)) # Output: 8
Nested Ternary Expressions
You can nest ternary expressions, but it’s recommended to avoid overusing this feature, as it can reduce code readability.
score = 85 grade = "A" if score > 90 else "B" if score > 80 else "C" print(grade) # Output: B
Although this works, it’s often better to use a standard if...elif...else
block for complex conditions.
When to Use It
Use one-line if...else
statements when:
- The condition and both outcomes are simple
- You want to write more concise and readable code
- You are assigning a value based on a condition
Avoid using it when:
- You need to execute multiple statements depending on the condition
- Readability would be harmed by a nested or overly complex expression
Conclusion
Python’s one-line if...else
expression is a powerful way to simplify your code for basic conditional logic. While it's useful for clarity and brevity in many cases, always balance conciseness with readability.
If you're writing more complex logic, stick with the traditional multi-line if...else
blocks to keep your code maintainable.
FAQs
Yes, it's commonly used for returning values based on conditions.
Technically yes, but it's discouraged due to poor readability.
Use multi-line blocks when logic is complex or involves multiple statements.
We are Leapcell, your top choice for hosting Python projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ