What is a conditional operator?
The conditional operator checks whether a certain condition is true, and based on this executes the corresponding block of code. It's like in life: "If it's raining, I'll take an umbrella, otherwise I'll go without an umbrella."
If operator: basic check
The if operator is the simplest conditional operator. It checks the condition and executes the code only if the condition is true.
Syntax:
if условие: # the code will be executed if the condition is true action
Example:
возраст = 20 if возраст >= 18: print("You are an adult")
In this example, the program will check if the age is greater than 18. If yes, it will display a message. Note the colon after the condition and the indentation before print — this is a required part of Python syntax.

Operator else: alternative path
The else operator is used together with if and determines what to do if the condition is false.
Syntax:
if условие: # will be executed if the condition is true action_1 else: # will be executed if the condition is false action_2
Example:
температура = 15 if температура > 20: print("It's warm outside") else: print("It's chilly outside")
Here the program will choose one of two paths: either the first message (if the temperature is above 20) or the second (if not).
elif operator: multiple choice
What if there are more than two options? For this, there is elif (short for "else if"). It allows you to check several conditions sequentially.
Syntax:
if условие_1: действие_1 elif условие_2: действие_2 elif условие_3: действие_3 else: действие_по_умолчанию
Example:
оценка = 85 if оценка >= 90: print("Excellent!") elif оценка >= 75: print("Good!") elif оценка >= 60: print("Satisfactory") else: print("You need to improve your knowledge")
The program checks the conditions in order from top to bottom. As soon as one of the conditions is true, the corresponding code block is executed, and the remaining checks are skipped.
Practical examples
Checking the parity of a number:
число = 7 if число % 2 == 0: print("Even number") else: print("The number is odd")
Determining the time of day:
час = 14 if час < 12: print("Good morning!") elif час < 18: print("Hello!") elif час < 22: print("Good evening!") else: print("Good night!")
Login and password verification:
логин = "admin" пароль = "12345" if логин == "admin" and пароль == "12345": print("Access allowed") else: print("Incorrect username or password")

Important points for beginners
Indents matter. In Python, indentation (usually 4 spaces) determines which code belongs to a conditional block. Incorrect indentation will result in an error.
Colons are required. Each if, elif and else must be followed by a colon.
Comparison operators: use == for comparison (do not confuse with =, which assigns a value), != for "not equal", >, <, >=, <= for numerical comparisons.
Logical operators: you can combine conditions using and (and), or (or), not (not).
The order of inspections is important. When using elif, the first true condition will be executed, the rest will be skipped.
Typical mistakes of beginners
Using
=instead of==in the condition will result in an error.A forgotten colon will cause a syntax error.
Incorrect indents will break the program structure.
Too complex nested conditions make it difficult to read the code — try to simplify the logic.
Conclusion
Conditional statements are the basis of programming logic. Having mastered if, else and elif, you will be able to create programs that respond to different situations and make decisions. Practice, experiment with different conditions, and soon the use of these operators will become second nature to you.
Start with simple examples, gradually complicating the logic, and you will see how a powerful tool is in your hands!
Do you want to master conditional operators in practice?
In Python course from Kodika we analyze conditional operators in much more detail! You will find:
Interactive lessons with step-by-step explanations
Cool practical tasks, which will help to consolidate the material
Real projects, where you will use if, else, and elif to create games and useful programs
Tasks of different levels of complexity - from simple to advanced
Instant code verification and tips if something went wrong
Don't just read about programming — program! Join the Python course in Kodik and start creating your first programs today.
Join our Telegram channel!
Subscribe to our Telegram channel, so as not to miss:
📚 New lessons and articles in programming
💡 Useful tips and life hacks for beginner developers
🎯 Practical tasks with analysis of decisions
🚀 News from the IT world and technologies
🎁 Exclusive materials and bonuses for subscribers
👥 Communication with like-minded people and support from experienced programmers
Become a part of our developer community!
