IF statements using logical operators and conditional expressions


Going beyond basic conditional checks, Python provides advanced tools and techniques that allow for concise and complex condition evaluations. In this lesson, we delve deeper into these advanced features.

a. Conditional Expressions:

Python's conditional expression allows you to evaluate a value based on a condition in a single, concise line.

Code

x = 5
message = 'Even' if x % 2 == 0 else 'Odd'
print(message)

Output

Odd

b. Logical Operators:

Logical operators ('and', 'or', 'not') allow you to combine multiple conditions.

Code

x = 5
y = 10

if x > 0 and y % 2 == 0:
    print('x is positive and y is even')

Output

x is positive and y is even

Enquiries

[email protected]

Copyright © 2023 - slash-root.com