Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
if statement | If...Else Statement
Python Tutorial for Beginners
course content

Course Content

Python Tutorial for Beginners

Python Tutorial for Beginners

1. Introduction to Programming
2. Variables in Python
3. Comments
4. If...Else Statement

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

123
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
copy

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.

  1. The code starts with the if keyword.
  2. 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).
  3. Then first line ends with a colon.
  4. 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.

Task

If price has the value of 77, new_price is twice as high as the previous one. Code both lines.

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 4. Chapter 1
toggle bottom row

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

123
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
copy

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.

  1. The code starts with the if keyword.
  2. 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).
  3. Then first line ends with a colon.
  4. 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.

Task

If price has the value of 77, new_price is twice as high as the previous one. Code both lines.

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 4. Chapter 1
toggle bottom row

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

123
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
copy

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.

  1. The code starts with the if keyword.
  2. 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).
  3. Then first line ends with a colon.
  4. 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.

Task

If price has the value of 77, new_price is twice as high as the previous one. Code both lines.

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!

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.

123
species = "kitten" if species == "kitten": print("Yes, it's kitten.")
copy

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.

  1. The code starts with the if keyword.
  2. 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).
  3. Then first line ends with a colon.
  4. 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.

Task

If price has the value of 77, new_price is twice as high as the previous one. Code both lines.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt