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
course content

Course Content

Conditional Statements in Python

Conditional Statements in Python

1. Python if Statement
2. Python if-else Statement
3. Python if-elif-else Statement
4. Python Ternary Operator

book
Syntax of if Statement

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

123
age = 18 if age == 18: print('Adult')
copy

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
test

Swipe to show code editor

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 ....

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
toggle bottom row

book
Syntax of if Statement

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

123
age = 18 if age == 18: print('Adult')
copy

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
test

Swipe to show code editor

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 ....

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt