Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Syntax of if Statement | Python if Statement
Conditional Statements in Python

Syntax of if StatementSyntax of if Statement

Let's revisit the if-statement construct and consider another example.

Talk about the syntax.

  1. The keyword used for the conditional operator is if. It's essential to note that it is case-sensitive. Therefore, using If with a capital I` will result in an error since it is incorrect;
  2. Following the if keyword, we encounter a condition. It's worth mentioning that conditions can be expressed in various ways, a topic we will delve into later. In our example, we are checking if a variable is equal to a specific value, and to do this, we employ the equality operator;
  3. Then, after the condition, we put a colon;
  4. Then, we have a code indent that is executed inside the if-block.

That is, this is an instruction that is executed if our conditions are True.

Let's move on to consolidating knowledge.

Note

f-strings allow embedding expressions inside string, using curly braces {}, for easier and more readable string formatting. Example: f"Hello, {name}!".

Task

Let's play the game. Guess the number between 1 and 10.

  1. You have to implement if statement which will check that the number is equal to the number that I thought of;
  2. If this condition is True you have to display the next text: Right, You guessed my number! I have in my mind number ....

Everything was clear?

Section 1. Chapter 2
toggle bottom row
course content

Course Content

Conditional Statements in Python

Syntax of if StatementSyntax of if Statement

Let's revisit the if-statement construct and consider another example.

Talk about the syntax.

  1. The keyword used for the conditional operator is if. It's essential to note that it is case-sensitive. Therefore, using If with a capital I` will result in an error since it is incorrect;
  2. Following the if keyword, we encounter a condition. It's worth mentioning that conditions can be expressed in various ways, a topic we will delve into later. In our example, we are checking if a variable is equal to a specific value, and to do this, we employ the equality operator;
  3. Then, after the condition, we put a colon;
  4. Then, we have a code indent that is executed inside the if-block.

That is, this is an instruction that is executed if our conditions are True.

Let's move on to consolidating knowledge.

Note

f-strings allow embedding expressions inside string, using curly braces {}, for easier and more readable string formatting. Example: f"Hello, {name}!".

Task

Let's play the game. Guess the number between 1 and 10.

  1. You have to implement if statement which will check that the number is equal to the number that I thought of;
  2. If this condition is True you have to display the next text: Right, You guessed my number! I have in my mind number ....

Everything was clear?

Section 1. Chapter 2
toggle bottom row
some-alt