Contenido del Curso
Python Tutorial for Beginners
Python Tutorial for Beginners
if statement
The if-elif-else conditional statement (sometimes called the branching statement) is the main selection tool in Python. In simple terms, it chooses which action to take based on the value of the variables at the time the condition is checked. This statement is divided into the following three separate statements:
- if statement
- if-else statement
- if-else-elif statement. We will consider each of them separately, starting with the first.
The if branching operator allows you to execute a specific set of instructions depending on a certain condition. For example, you want to know if the string assigned to a species variable is "kitty".This is the code.
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
If the string "kitten" has been assigned to the variable species, Python displays the message Yes, it is kitten. If the string "kitten" has not been assigned to the variable species, nothing happens.
Let's break down the code.
- The code starts with the if keyword.
- Note that when we write a condition, we are writing two equal signs ==, not one (since one equal sign = is only used to assign a value to some variable).
- Then first line ends with a colon.
- Then comes a block of instructions that will be executed if the condition is correct. Note that this block is separated by spaces.
Note that if is all in lowercase, but if you write IF instead of if, you will get an error.
Let's summarize how it works and move on to practice: When the if statement is executed, the condition is checked. If the condition is true, then all statements in the if block are executed. But if the condition turns out to be wrong, then all instructions within this block are skipped.
Tarea
If price has the value of 77, new_price is twice as high as the previous one. Code both lines.
¡Gracias por tus comentarios!
if statement
The if-elif-else conditional statement (sometimes called the branching statement) is the main selection tool in Python. In simple terms, it chooses which action to take based on the value of the variables at the time the condition is checked. This statement is divided into the following three separate statements:
- if statement
- if-else statement
- if-else-elif statement. We will consider each of them separately, starting with the first.
The if branching operator allows you to execute a specific set of instructions depending on a certain condition. For example, you want to know if the string assigned to a species variable is "kitty".This is the code.
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
If the string "kitten" has been assigned to the variable species, Python displays the message Yes, it is kitten. If the string "kitten" has not been assigned to the variable species, nothing happens.
Let's break down the code.
- The code starts with the if keyword.
- Note that when we write a condition, we are writing two equal signs ==, not one (since one equal sign = is only used to assign a value to some variable).
- Then first line ends with a colon.
- Then comes a block of instructions that will be executed if the condition is correct. Note that this block is separated by spaces.
Note that if is all in lowercase, but if you write IF instead of if, you will get an error.
Let's summarize how it works and move on to practice: When the if statement is executed, the condition is checked. If the condition is true, then all statements in the if block are executed. But if the condition turns out to be wrong, then all instructions within this block are skipped.
Tarea
If price has the value of 77, new_price is twice as high as the previous one. Code both lines.
¡Gracias por tus comentarios!
if statement
The if-elif-else conditional statement (sometimes called the branching statement) is the main selection tool in Python. In simple terms, it chooses which action to take based on the value of the variables at the time the condition is checked. This statement is divided into the following three separate statements:
- if statement
- if-else statement
- if-else-elif statement. We will consider each of them separately, starting with the first.
The if branching operator allows you to execute a specific set of instructions depending on a certain condition. For example, you want to know if the string assigned to a species variable is "kitty".This is the code.
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
If the string "kitten" has been assigned to the variable species, Python displays the message Yes, it is kitten. If the string "kitten" has not been assigned to the variable species, nothing happens.
Let's break down the code.
- The code starts with the if keyword.
- Note that when we write a condition, we are writing two equal signs ==, not one (since one equal sign = is only used to assign a value to some variable).
- Then first line ends with a colon.
- Then comes a block of instructions that will be executed if the condition is correct. Note that this block is separated by spaces.
Note that if is all in lowercase, but if you write IF instead of if, you will get an error.
Let's summarize how it works and move on to practice: When the if statement is executed, the condition is checked. If the condition is true, then all statements in the if block are executed. But if the condition turns out to be wrong, then all instructions within this block are skipped.
Tarea
If price has the value of 77, new_price is twice as high as the previous one. Code both lines.
¡Gracias por tus comentarios!
The if-elif-else conditional statement (sometimes called the branching statement) is the main selection tool in Python. In simple terms, it chooses which action to take based on the value of the variables at the time the condition is checked. This statement is divided into the following three separate statements:
- if statement
- if-else statement
- if-else-elif statement. We will consider each of them separately, starting with the first.
The if branching operator allows you to execute a specific set of instructions depending on a certain condition. For example, you want to know if the string assigned to a species variable is "kitty".This is the code.
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
If the string "kitten" has been assigned to the variable species, Python displays the message Yes, it is kitten. If the string "kitten" has not been assigned to the variable species, nothing happens.
Let's break down the code.
- The code starts with the if keyword.
- Note that when we write a condition, we are writing two equal signs ==, not one (since one equal sign = is only used to assign a value to some variable).
- Then first line ends with a colon.
- Then comes a block of instructions that will be executed if the condition is correct. Note that this block is separated by spaces.
Note that if is all in lowercase, but if you write IF instead of if, you will get an error.
Let's summarize how it works and move on to practice: When the if statement is executed, the condition is checked. If the condition is true, then all statements in the if block are executed. But if the condition turns out to be wrong, then all instructions within this block are skipped.
Tarea
If price has the value of 77, new_price is twice as high as the previous one. Code both lines.