Course Content
Conditional Statements in Python
Conditional Statements in Python
Syntax of if Statement
Let's revisit the if
-statement construct and consider another example.
age = 18 if age == 18: print('Adult')
Talk about the syntax.
- The keyword used for the conditional operator is
if
. It's essential to note that it is case-sensitive. Therefore, usingIf
with a capitalI
will result in an error since it is incorrect; - 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; - Then, after the condition, we put a colon
:
; - 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}!"
.
Swipe to show code editor
Let's play the game. Guess the number between 1 and 10.
- You have to implement
if
statement which will check that the number is equal to the number that I thought of; - If this condition is
True
you have to display the next text:Right, You guessed my number! I have in my mind number ...
.
Thanks for your feedback!
Syntax of if Statement
Let's revisit the if
-statement construct and consider another example.
age = 18 if age == 18: print('Adult')
Talk about the syntax.
- The keyword used for the conditional operator is
if
. It's essential to note that it is case-sensitive. Therefore, usingIf
with a capitalI
will result in an error since it is incorrect; - 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; - Then, after the condition, we put a colon
:
; - 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}!"
.
Swipe to show code editor
Let's play the game. Guess the number between 1 and 10.
- You have to implement
if
statement which will check that the number is equal to the number that I thought of; - If this condition is
True
you have to display the next text:Right, You guessed my number! I have in my mind number ...
.
Thanks for your feedback!