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

Glissez pour afficher le menu

book
Syntax of if Statement

The if statement is a foundational tool in Python that allows us to make decisions in our code.

Let's break down the key components of the if statement syntax:

  • Keywordif: this keyword begins the conditional statement. Remember, it is case-sensitive—using If instead of if will cause an error;

  • Condition: the condition is an expression that evaluates to True or False. In this example, we check if a variable meets a certain requirement;

  • Colon:: the colon signals that the following indented block belongs to the if statement;

  • Indented Code Block: this block contains the code that runs only if the condition is True.

Example 1: Standart Execution

12345
steps_taken = 7500 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
copy

Explanation

  1. The conditionsteps_taken < step_goal checks if the number of steps taken is less than the daily goal;

  2. If the condition is True, the code inside the if block is executed;

  3. The message uses an f-string to dynamically include the number of steps remaining, making the output user-friendly and precise.

Example 2: When Nothing Executed

12345
steps_taken = 10000 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
copy

In this case, the condition steps_taken < step_goal evaluates to False because steps_taken is equal to step_goal. Since the condition is not met, the code block inside the if statement is not executed, and nothing is printed to the console. This demonstrates that the code only runs when the condition evaluates to True.

The image depicts the flow of an if statement:

  • Condition Check: the program evaluates whether the condition is True or False;

  • Execution: if the condition is True, the indented code block runs. Otherwise, the program skips it.

Tâche

Swipe to start coding

Your Fitness Tracker needs to do more than just check steps! This time, it will motivate users to complete their workout by comparing their calories burned against daily goals.

Fill in the blanks in the code you've already been given.

Once you've completed this task, click the button below the code to check your solution.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

book
Syntax of if Statement

The if statement is a foundational tool in Python that allows us to make decisions in our code.

Let's break down the key components of the if statement syntax:

  • Keywordif: this keyword begins the conditional statement. Remember, it is case-sensitive—using If instead of if will cause an error;

  • Condition: the condition is an expression that evaluates to True or False. In this example, we check if a variable meets a certain requirement;

  • Colon:: the colon signals that the following indented block belongs to the if statement;

  • Indented Code Block: this block contains the code that runs only if the condition is True.

Example 1: Standart Execution

12345
steps_taken = 7500 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
copy

Explanation

  1. The conditionsteps_taken < step_goal checks if the number of steps taken is less than the daily goal;

  2. If the condition is True, the code inside the if block is executed;

  3. The message uses an f-string to dynamically include the number of steps remaining, making the output user-friendly and precise.

Example 2: When Nothing Executed

12345
steps_taken = 10000 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
copy

In this case, the condition steps_taken < step_goal evaluates to False because steps_taken is equal to step_goal. Since the condition is not met, the code block inside the if statement is not executed, and nothing is printed to the console. This demonstrates that the code only runs when the condition evaluates to True.

The image depicts the flow of an if statement:

  • Condition Check: the program evaluates whether the condition is True or False;

  • Execution: if the condition is True, the indented code block runs. Otherwise, the program skips it.

Tâche

Swipe to start coding

Your Fitness Tracker needs to do more than just check steps! This time, it will motivate users to complete their workout by comparing their calories burned against daily goals.

Fill in the blanks in the code you've already been given.

Once you've completed this task, click the button below the code to check your solution.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt