Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Introdução à Instrução If | Instrução if no Python
Estruturas Condicionais no Python
course content

Conteúdo do Curso

Estruturas Condicionais no Python

Estruturas Condicionais no Python

1. Instrução if no Python
2. Instrução if-else no Python
3. Instrução if-elif-else no Python
4. Operador ternário no Python

book
Introdução à Instrução If

Olá a todos! É um prazer dar as boas-vindas a este curso. Aqui, nos familiarizaremos com o operador condicional, às vezes chamado de operador de ramificação.

What Are Conditional Statements?

A conditional statement allows your program to make decisions by executing different blocks of code based on whether a specific condition is True or False. Think of it as answering a "yes or no" question in your code: "If this condition is true, do this."

For example, imagine building a Fitness Tracker. If a user's step count reaches their daily goal, you might display a congratulatory message. Otherwise, you might encourage them to take more steps.

Syntax of a Conditional Statement

Here's the basic syntax of an if statement:

python

Explanation of the Syntax:

  1. if: this keyword begins the conditional statement;

  2. condition: this is a logical expression that evaluates to True or False;

  3. Code Block: the indented code below the if statement runs only when the condition is True.

Example: Checking Step Count in a Fitness Tracker

Let's write a simple example using a Fitness Tracker. We'll check if the user has achieved their step goal for the day.

12345
steps_taken = 12000 step_goal = 10000 if steps_taken >= step_goal: print("Congratulations! You've reached your daily step goal.")
copy

Explanation

  1. Condition: the if statement checks if the number of steps taken (steps_taken) is greater than or equal to the step goal (step_goal);

  2. Result: if the condition is True (e.g., 12000 >= 10000), the message "Congratulations! You've reached your daily step goal." is printed. But if the condition is False (e.g., 7500 >= 10000), nothing happens, and the program moves on.

1. What does this code do if steps_taken = 7500 and step_goal = 10000?

2. What does this code do if steps_taken = 7500 and step_goal = 10000?

question mark

What does this code do if steps_taken = 7500 and step_goal = 10000?

Select the correct answer

question mark

What does this code do if steps_taken = 7500 and step_goal = 10000?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

course content

Conteúdo do Curso

Estruturas Condicionais no Python

Estruturas Condicionais no Python

1. Instrução if no Python
2. Instrução if-else no Python
3. Instrução if-elif-else no Python
4. Operador ternário no Python

book
Introdução à Instrução If

Olá a todos! É um prazer dar as boas-vindas a este curso. Aqui, nos familiarizaremos com o operador condicional, às vezes chamado de operador de ramificação.

What Are Conditional Statements?

A conditional statement allows your program to make decisions by executing different blocks of code based on whether a specific condition is True or False. Think of it as answering a "yes or no" question in your code: "If this condition is true, do this."

For example, imagine building a Fitness Tracker. If a user's step count reaches their daily goal, you might display a congratulatory message. Otherwise, you might encourage them to take more steps.

Syntax of a Conditional Statement

Here's the basic syntax of an if statement:

python

Explanation of the Syntax:

  1. if: this keyword begins the conditional statement;

  2. condition: this is a logical expression that evaluates to True or False;

  3. Code Block: the indented code below the if statement runs only when the condition is True.

Example: Checking Step Count in a Fitness Tracker

Let's write a simple example using a Fitness Tracker. We'll check if the user has achieved their step goal for the day.

12345
steps_taken = 12000 step_goal = 10000 if steps_taken >= step_goal: print("Congratulations! You've reached your daily step goal.")
copy

Explanation

  1. Condition: the if statement checks if the number of steps taken (steps_taken) is greater than or equal to the step goal (step_goal);

  2. Result: if the condition is True (e.g., 12000 >= 10000), the message "Congratulations! You've reached your daily step goal." is printed. But if the condition is False (e.g., 7500 >= 10000), nothing happens, and the program moves on.

1. What does this code do if steps_taken = 7500 and step_goal = 10000?

2. What does this code do if steps_taken = 7500 and step_goal = 10000?

question mark

What does this code do if steps_taken = 7500 and step_goal = 10000?

Select the correct answer

question mark

What does this code do if steps_taken = 7500 and step_goal = 10000?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt