Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Conditional Statements | Control Flow & Logic
Introduction to Python with AI

bookConditional Statements

In Python, many operations depend on checking if something is true or false. This begins with comparisons, such as testing if one value is greater than another or if two values are equal.

Conditional statements then use these comparisons to make decisions. If a condition is true, a specific block of code runs. This is the foundation of programming logic.

Note
Example Prompts
  • List all comparison operators in Python and show example using them.
  • Explain how if, elif, and else work in Python. Show a code example with all three.
  • Show in code example how logical operators and, or, and not work in Python conditions.
  • Explain how indentation affects if-statements in Python. Show in code example what happens with incorrect indentation.

Comparison Operators

Conditions are built on comparisons β€” they evaluate to either True or False. Python supports the following operators:

  • == β€” equal to;
  • != β€” not equal to;
  • > β€” greater than;
  • < β€” less than;
  • >= β€” greater than or equal to;
  • <= β€” less than or equal to

Basic Structure

In Python, conditional blocks use if, elif, and else. Python reads conditions from top to bottom and executes the first block where the condition is True.

Logical Operators

You can combine conditions with logical operators:

  • and β€” both must be true;
  • or β€” at least one must be true;
  • not β€” reverses the result.

These make your logic more flexible.

Note
Note

Python has no direct xor operator, but you can simulate it with != when comparing boolean values.

Indentation in Conditional Blocks

Python uses indentation instead of braces to define code blocks. All lines under if, elif, or else must be indented by 4 spaces or 1 tab.

Wrong indentation will cause errors or unexpected behavior.

Summary

  • Conditions use if, elif, and else;
  • They evaluate to boolean values;
  • Comparison and logical operators define the logic;
  • Proper indentation is required to structure your blocks;

Try It Yourself

  1. Write a program that asks for a number;
  2. If the number is greater than 10, print "Big number";
  3. If it equals 10, print "Exactly ten";
  4. Otherwise, print "Small number";
  5. Extend it by checking if the number is also even or odd, and print that information too.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me an example of how to write this program?

What does the code look like for checking if a number is even or odd?

Can you explain how the `elif` statement works in this context?

Awesome!

Completion rate improved to 5

bookConditional Statements

Swipe to show menu

In Python, many operations depend on checking if something is true or false. This begins with comparisons, such as testing if one value is greater than another or if two values are equal.

Conditional statements then use these comparisons to make decisions. If a condition is true, a specific block of code runs. This is the foundation of programming logic.

Note
Example Prompts
  • List all comparison operators in Python and show example using them.
  • Explain how if, elif, and else work in Python. Show a code example with all three.
  • Show in code example how logical operators and, or, and not work in Python conditions.
  • Explain how indentation affects if-statements in Python. Show in code example what happens with incorrect indentation.

Comparison Operators

Conditions are built on comparisons β€” they evaluate to either True or False. Python supports the following operators:

  • == β€” equal to;
  • != β€” not equal to;
  • > β€” greater than;
  • < β€” less than;
  • >= β€” greater than or equal to;
  • <= β€” less than or equal to

Basic Structure

In Python, conditional blocks use if, elif, and else. Python reads conditions from top to bottom and executes the first block where the condition is True.

Logical Operators

You can combine conditions with logical operators:

  • and β€” both must be true;
  • or β€” at least one must be true;
  • not β€” reverses the result.

These make your logic more flexible.

Note
Note

Python has no direct xor operator, but you can simulate it with != when comparing boolean values.

Indentation in Conditional Blocks

Python uses indentation instead of braces to define code blocks. All lines under if, elif, or else must be indented by 4 spaces or 1 tab.

Wrong indentation will cause errors or unexpected behavior.

Summary

  • Conditions use if, elif, and else;
  • They evaluate to boolean values;
  • Comparison and logical operators define the logic;
  • Proper indentation is required to structure your blocks;

Try It Yourself

  1. Write a program that asks for a number;
  2. If the number is greater than 10, print "Big number";
  3. If it equals 10, print "Exactly ten";
  4. Otherwise, print "Small number";
  5. Extend it by checking if the number is also even or odd, and print that information too.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt